diff --git a/backend/alembic/versions/3a40fe11b427_create_global_content_tables.py b/backend/alembic/versions/3a40fe11b427_create_global_content_tables.py index 01dbac47..0eafb7da 100644 --- a/backend/alembic/versions/3a40fe11b427_create_global_content_tables.py +++ b/backend/alembic/versions/3a40fe11b427_create_global_content_tables.py @@ -105,6 +105,26 @@ def upgrade() -> None: AND account_id IS NULL """) + # ── Assign remaining trees to their author's account ───────────────────── + # Handles trees with no team_id that aren't is_default (e.g. inactive test + # trees, trees created before the team system existed). + op.execute(""" + UPDATE trees + SET account_id = u.account_id + FROM users u + WHERE trees.author_id = u.id + AND trees.account_id IS NULL + AND u.account_id IS NOT NULL + """) + + # ── Final fallback: any still-NULL trees go to platform account ─────────── + # Covers trees whose author has no account (seeded content, system rows). + op.execute(""" + UPDATE trees + SET account_id = '00000000-0000-0000-0000-000000000001' + WHERE account_id IS NULL + """) + # ── Assign global categories/tags/steps to platform account ───────────── op.execute(""" UPDATE tree_categories