149 lines
5.0 KiB
SQL
149 lines
5.0 KiB
SQL
-- 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();
|