The previous implementation issued PRAGMA foreign_keys toggles inside transactions (after tx.Begin()), but SQLite ignores pragmas once a transaction is open with modernc.org/sqlite, making those calls no-ops. This fix relocates the toggle to autocommit mode (db.Exec, not tx.Exec) and limits it to only the three migrations that need it (0023/0024/0025), which rebuild tables with incoming foreign keys (kind_assignments/ workout_type_paces reference workout_kinds; activities/sync_runs reference sync_state). The connection string now correctly restores ?_pragma=foreign_keys(1), ensuring FK enforcement is ON by default for normal runtime operation and during migrations that don't need the special handling. Each affected migration now: 1. Disables FK in autocommit mode before starting its transaction 2. Runs the migration's CREATE/INSERT/DROP/RENAME sequence 3. Re-enables FK in autocommit mode after the transaction commits All other migrations run normally with FK enforcement active throughout, protecting against cascading deletions silently failing if a future connection change causes FK enforcement to inadvertently remain off. Added TestForeignKeyEnforcementPostMigration to verify FK is correctly enforced after all migrations complete: valid FK references are accepted, and invalid ones are rejected. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
3.8 KiB
3.8 KiB