diff --git a/backend/alembic/versions/3a40fe11b427_create_global_content_tables.py b/backend/alembic/versions/3a40fe11b427_create_global_content_tables.py index 7bf8bbba..01dbac47 100644 --- a/backend/alembic/versions/3a40fe11b427_create_global_content_tables.py +++ b/backend/alembic/versions/3a40fe11b427_create_global_content_tables.py @@ -51,16 +51,26 @@ def upgrade() -> None: op.create_index('ix_platform_steps_step_type', 'platform_steps', ['step_type']) # ── Copy is_default=TRUE trees → template_trees ───────────────────────── + # Note: trees.tags is a relationship via tree_tags join table — no direct column. + # Aggregate tag names via a correlated subquery. op.execute(""" INSERT INTO template_trees (id, name, description, category, tree_type, tree_structure, tags, is_active, created_at, updated_at, source_tree_id) SELECT - gen_random_uuid(), name, description, category, tree_type, - tree_structure, COALESCE(tags, '[]'::jsonb), is_active, - COALESCE(created_at, NOW()), COALESCE(updated_at, NOW()), id - FROM trees - WHERE is_default = TRUE + gen_random_uuid(), t.name, t.description, t.category, t.tree_type, + t.tree_structure, + COALESCE( + (SELECT jsonb_agg(tt.name ORDER BY tt.name) + FROM tree_tag_assignments ta + JOIN tree_tags tt ON tt.id = ta.tag_id + WHERE ta.tree_id = t.id), + '[]'::jsonb + ), + t.is_active, + COALESCE(t.created_at, NOW()), COALESCE(t.updated_at, NOW()), t.id + FROM trees t + WHERE t.is_default = TRUE """) # ── Copy visibility='public' steps → platform_steps ─────────────────────