From 478205c208647ea901156bf9ac07d409a10bf6c0 Mon Sep 17 00:00:00 2001 From: chihlasm Date: Thu, 9 Apr 2026 06:41:00 +0000 Subject: [PATCH] fix: platform account fallback for script_templates seeded without team/user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Migration 057 inserts 6 AD script templates with NULL team_id and NULL created_by. Neither backfill path (created_by→users, team_id→team admin) could attribute them to an account, causing the verify check to fail. Fix: pre-create the platform sentinel account (ON CONFLICT DO NOTHING, safe since 3a40fe11b427 also creates it idempotently) and add a final fallback UPDATE assigning any remaining NULL script_templates to it. Co-Authored-By: Claude Opus 4.6 --- ...fc200abac1_add_account_id_script_tables.py | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/backend/alembic/versions/78fc200abac1_add_account_id_script_tables.py b/backend/alembic/versions/78fc200abac1_add_account_id_script_tables.py index 21cf8f2b..74116db6 100644 --- a/backend/alembic/versions/78fc200abac1_add_account_id_script_tables.py +++ b/backend/alembic/versions/78fc200abac1_add_account_id_script_tables.py @@ -13,8 +13,24 @@ down_revision: Union[str, None] = '7f136778f5a8' branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None +PLATFORM_ACCOUNT_ID = '00000000-0000-0000-0000-000000000001' + def upgrade() -> None: + # Ensure the platform sentinel account exists before any fallback assignments. + # Migration 3a40fe11b427 also inserts this with ON CONFLICT DO NOTHING — safe. + op.execute(f""" + INSERT INTO accounts (id, name, display_code, created_at, updated_at) + VALUES ( + '{PLATFORM_ACCOUNT_ID}', + 'ResolutionFlow Platform', + 'PLATFORM', + NOW(), + NOW() + ) + ON CONFLICT (id) DO NOTHING + """) + for table in ('script_builder_sessions', 'script_templates', 'script_generations'): op.add_column(table, sa.Column('account_id', sa.UUID(), nullable=True)) op.create_foreign_key( @@ -39,7 +55,7 @@ def upgrade() -> None: WHERE st.created_by = u.id AND st.account_id IS NULL """) - # Fallback for script_templates with NULL created_by: team_id → team admin user + # Fallback: team_id → team admin user op.execute(""" UPDATE script_templates st SET account_id = u.account_id @@ -49,6 +65,13 @@ def upgrade() -> None: AND u.account_id IS NOT NULL AND st.account_id IS NULL """) + # Final fallback: platform-seeded templates with NULL team_id AND NULL created_by + # (e.g. the 6 AD templates inserted by migration 057) → platform sentinel account + op.execute(f""" + UPDATE script_templates + SET account_id = '{PLATFORM_ACCOUNT_ID}' + WHERE account_id IS NULL + """) # script_generations: user_id → users.account_id op.execute("""