diff --git a/migrations/versions/2025_01_21_0820-4dec3e456c9e_add_on_delete_cascade.py b/migrations/versions/2025_01_21_0820-4dec3e456c9e_add_on_delete_cascade.py index 99a98404..bb02d8cd 100644 --- a/migrations/versions/2025_01_21_0820-4dec3e456c9e_add_on_delete_cascade.py +++ b/migrations/versions/2025_01_21_0820-4dec3e456c9e_add_on_delete_cascade.py @@ -18,6 +18,12 @@ def upgrade() -> None: + # Turn off foreign key constraints for this migration + op.execute("PRAGMA foreign_keys=off;") + + # Begin transaction + op.execute("BEGIN TRANSACTION;") + # To add ON DELETE CASCADE to the foreign key constraint, we need to # rename the table, create a new table with the constraint, and copy # the data over. @@ -101,6 +107,13 @@ def upgrade() -> None: op.execute("CREATE INDEX idx_prompts_workspace_id ON prompts (workspace_id);") op.execute("CREATE INDEX idx_sessions_workspace_id ON sessions (active_workspace_id);") + # Finish transaction + op.execute("COMMIT;") + + # Turn on foreign key constraints after the migration. Just to be sure. This shouldn't + # be necessary, since it should be specified at the beginning of every connection, doesn't hurt. + op.execute("PRAGMA foreign_keys=on;") + def downgrade() -> None: # Settings table