changes
This commit is contained in:
1 parent
a99af426db
commit
8886aa8640
91 files changed
+2823
-773
No files matched your search
@@ -0,0 +1,148 @@
|
||||
-- Course taxonomy: admin-managed categories (topics) + tags (types), many-to-many.
|
||||
-- Backfills from legacy courses.category (enum) and courses.tags (text[]), then drops them.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS course_categories (
|
||||
id TEXT PRIMARY KEY,
|
||||
slug TEXT UNIQUE NOT NULL,
|
||||
label TEXT NOT NULL,
|
||||
sort_order INT NOT NULL DEFAULT 0,
|
||||
show_in_filter BOOLEAN NOT NULL DEFAULT true,
|
||||
published BOOLEAN NOT NULL DEFAULT true,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS course_tags (
|
||||
id TEXT PRIMARY KEY,
|
||||
slug TEXT UNIQUE NOT NULL,
|
||||
label TEXT NOT NULL,
|
||||
sort_order INT NOT NULL DEFAULT 0,
|
||||
show_in_nav BOOLEAN NOT NULL DEFAULT true,
|
||||
show_in_filter BOOLEAN NOT NULL DEFAULT true,
|
||||
published BOOLEAN NOT NULL DEFAULT true,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS course_category_links (
|
||||
course_id TEXT NOT NULL REFERENCES courses(id) ON DELETE CASCADE,
|
||||
category_id TEXT NOT NULL REFERENCES course_categories(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (course_id, category_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS course_tag_links (
|
||||
course_id TEXT NOT NULL REFERENCES courses(id) ON DELETE CASCADE,
|
||||
tag_id TEXT NOT NULL REFERENCES course_tags(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (course_id, tag_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_course_category_links_category
|
||||
ON course_category_links (category_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_course_tag_links_tag
|
||||
ON course_tag_links (tag_id);
|
||||
|
||||
-- Seed canonical categories (stable ids = cat-<slug>)
|
||||
INSERT INTO course_categories (id, slug, label, sort_order, show_in_filter, published)
|
||||
VALUES
|
||||
('cat-technology', 'technology', 'هوش مصنوعی و تحول سازمان', 10, true, true),
|
||||
('cat-digital', 'digital', 'برند و بازاریابی', 20, true, true),
|
||||
('cat-innovation', 'innovation', 'برندینگ', 30, true, true),
|
||||
('cat-leadership', 'leadership', 'هماندیشی مدیریتی', 40, true, true),
|
||||
('cat-energy', 'energy', 'انرژی', 50, true, true),
|
||||
('cat-design', 'design', 'طراحی', 60, true, true)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
slug = EXCLUDED.slug,
|
||||
label = EXCLUDED.label,
|
||||
sort_order = EXCLUDED.sort_order;
|
||||
|
||||
-- Seed known nav tags
|
||||
INSERT INTO course_tags (id, slug, label, sort_order, show_in_nav, show_in_filter, published)
|
||||
VALUES
|
||||
('tag-workshops', 'workshops', 'کارگاهها', 10, true, true, true),
|
||||
('tag-corporate', 'corporate', 'دورههای سازمانی', 20, true, true, true),
|
||||
('tag-archive', 'archive', 'گزیده دورههای برگزار شده', 30, true, true, true)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
slug = EXCLUDED.slug,
|
||||
label = EXCLUDED.label,
|
||||
sort_order = EXCLUDED.sort_order;
|
||||
|
||||
-- Any extra free-text tags from legacy column → new tag rows (slug derived)
|
||||
DO $$
|
||||
DECLARE
|
||||
r RECORD;
|
||||
new_id TEXT;
|
||||
new_slug TEXT;
|
||||
n INT := 100;
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'courses' AND column_name = 'tags'
|
||||
) THEN
|
||||
FOR r IN
|
||||
SELECT DISTINCT trim(t) AS label
|
||||
FROM courses, UNNEST(tags) AS t
|
||||
WHERE trim(t) <> ''
|
||||
AND trim(t) NOT IN (
|
||||
SELECT label FROM course_tags
|
||||
)
|
||||
LOOP
|
||||
new_slug := 'tag-' || substr(md5(r.label), 1, 12);
|
||||
new_id := 'tag-auto-' || substr(md5(r.label), 1, 12);
|
||||
INSERT INTO course_tags (id, slug, label, sort_order, show_in_nav, show_in_filter, published)
|
||||
VALUES (new_id, new_slug, r.label, n, true, true, true)
|
||||
ON CONFLICT (slug) DO NOTHING;
|
||||
n := n + 10;
|
||||
END LOOP;
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- Link courses → categories from legacy category column
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'courses' AND column_name = 'category'
|
||||
) THEN
|
||||
INSERT INTO course_category_links (course_id, category_id)
|
||||
SELECT c.id, cat.id
|
||||
FROM courses c
|
||||
JOIN course_categories cat ON cat.slug = c.category
|
||||
ON CONFLICT DO NOTHING;
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- Link courses → tags from legacy tags array (match by label)
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'courses' AND column_name = 'tags'
|
||||
) THEN
|
||||
INSERT INTO course_tag_links (course_id, tag_id)
|
||||
SELECT DISTINCT c.id, t.id
|
||||
FROM courses c
|
||||
CROSS JOIN LATERAL UNNEST(c.tags) AS raw(label)
|
||||
JOIN course_tags t ON t.label = trim(raw.label)
|
||||
WHERE trim(raw.label) <> ''
|
||||
ON CONFLICT DO NOTHING;
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- Drop legacy columns
|
||||
ALTER TABLE courses DROP CONSTRAINT IF EXISTS courses_category_check;
|
||||
ALTER TABLE courses DROP COLUMN IF EXISTS category;
|
||||
ALTER TABLE courses DROP COLUMN IF EXISTS tags;
|
||||
|
||||
-- updated_at triggers
|
||||
DROP TRIGGER IF EXISTS trg_course_categories_set_updated_at ON course_categories;
|
||||
CREATE TRIGGER trg_course_categories_set_updated_at
|
||||
BEFORE UPDATE ON course_categories
|
||||
FOR EACH ROW EXECUTE FUNCTION set_updated_at();
|
||||
|
||||
DROP TRIGGER IF EXISTS trg_course_tags_set_updated_at ON course_tags;
|
||||
CREATE TRIGGER trg_course_tags_set_updated_at
|
||||
BEFORE UPDATE ON course_tags
|
||||
FOR EACH ROW EXECUTE FUNCTION set_updated_at();
|
||||
+64
-1
@@ -20,6 +20,10 @@ import {
|
||||
stats,
|
||||
testimonials,
|
||||
} from "../src/lib/mock/site.ts";
|
||||
import {
|
||||
SEED_COURSE_CATEGORIES,
|
||||
SEED_COURSE_TAGS,
|
||||
} from "../src/lib/taxonomy.ts";
|
||||
|
||||
import {
|
||||
alumniStoryToRow,
|
||||
@@ -81,8 +85,42 @@ async function main() {
|
||||
results.push([table, n]);
|
||||
});
|
||||
|
||||
await upsert(
|
||||
"course_categories",
|
||||
SEED_COURSE_CATEGORIES.map((c) => ({
|
||||
id: c.id,
|
||||
slug: c.slug,
|
||||
label: c.label,
|
||||
sort_order: c.sortOrder,
|
||||
show_in_filter: true,
|
||||
published: true,
|
||||
})),
|
||||
["id", "slug", "label", "sort_order", "show_in_filter", "published"],
|
||||
);
|
||||
await upsert(
|
||||
"course_tags",
|
||||
SEED_COURSE_TAGS.map((t) => ({
|
||||
id: t.id,
|
||||
slug: t.slug,
|
||||
label: t.label,
|
||||
sort_order: t.sortOrder,
|
||||
show_in_nav: t.showInNav,
|
||||
show_in_filter: true,
|
||||
published: true,
|
||||
})),
|
||||
[
|
||||
"id",
|
||||
"slug",
|
||||
"label",
|
||||
"sort_order",
|
||||
"show_in_nav",
|
||||
"show_in_filter",
|
||||
"published",
|
||||
],
|
||||
);
|
||||
|
||||
await upsert("courses", courses.map(courseToRow), [
|
||||
"id", "slug", "title", "summary", "category", "duration_hours", "format", "price",
|
||||
"id", "slug", "title", "summary", "duration_hours", "format", "price",
|
||||
"registration_url", "poster_image_url", "brochure_image_url", "image_url", "seo_description", "published",
|
||||
]);
|
||||
const courseIds = courses.map((c) => c.id);
|
||||
@@ -91,6 +129,31 @@ async function main() {
|
||||
[courseIds],
|
||||
);
|
||||
results.push(["courses_pruned", courseIds.length]);
|
||||
|
||||
await client.query(`DELETE FROM course_category_links`);
|
||||
await client.query(`DELETE FROM course_tag_links`);
|
||||
let catLinks = 0;
|
||||
let tagLinks = 0;
|
||||
for (const course of courses) {
|
||||
for (const cat of course.categories ?? []) {
|
||||
await client.query(
|
||||
`INSERT INTO course_category_links (course_id, category_id)
|
||||
VALUES ($1, $2) ON CONFLICT DO NOTHING`,
|
||||
[course.id, cat.id],
|
||||
);
|
||||
catLinks += 1;
|
||||
}
|
||||
for (const tag of course.tags ?? []) {
|
||||
await client.query(
|
||||
`INSERT INTO course_tag_links (course_id, tag_id)
|
||||
VALUES ($1, $2) ON CONFLICT DO NOTHING`,
|
||||
[course.id, tag.id],
|
||||
);
|
||||
tagLinks += 1;
|
||||
}
|
||||
}
|
||||
results.push(["course_category_links", catLinks]);
|
||||
results.push(["course_tag_links", tagLinks]);
|
||||
await upsert("programs", programs.map(programToRow), [
|
||||
"id", "slug", "title", "blurb", "group_key", "href", "tagline", "audience",
|
||||
"duration_label", "format_label", "highlights", "body", "published",
|
||||
|
||||
Reference in new issue
Block a user