fix: prevent parallel execution migration race conditions (GH#720)

When multiple bd commands are run in parallel, they can race during database
migrations, causing "duplicate column name" errors. This happens because:

1. Process A checks if column exists → false
2. Process B checks if column exists → false
3. Process A adds column → succeeds
4. Process B adds column → FAILS (duplicate column)

Changes:
- Wrap RunMigrations in BEGIN EXCLUSIVE transaction to serialize migrations
- Disable foreign keys BEFORE the transaction (PRAGMA must be called outside tx)
- Convert nested BEGIN/COMMIT in migrations 010, 022, 025 to use SAVEPOINTs
  (SQLite does not support nested transactions)
- Remove redundant PRAGMA foreign_keys calls from individual migrations

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-12-23 00:55:05 -08:00
parent e79558a972
commit d677554ef3
4 changed files with 102 additions and 54 deletions

View File

@@ -61,13 +61,20 @@ func MigrateContentHashColumn(db *sql.DB) error {
return fmt.Errorf("error iterating issues: %w", err)
}
tx, err := db.Begin()
// Use SAVEPOINT for atomicity (we're already inside an EXCLUSIVE transaction from RunMigrations)
// SQLite doesn't support nested transactions but SAVEPOINTs work inside transactions
_, err = db.Exec(`SAVEPOINT content_hash_migration`)
if err != nil {
return fmt.Errorf("failed to begin transaction: %w", err)
return fmt.Errorf("failed to create savepoint: %w", err)
}
defer tx.Rollback()
savepointReleased := false
defer func() {
if !savepointReleased {
_, _ = db.Exec(`ROLLBACK TO SAVEPOINT content_hash_migration`)
}
}()
stmt, err := tx.Prepare(`UPDATE issues SET content_hash = ? WHERE id = ?`)
stmt, err := db.Prepare(`UPDATE issues SET content_hash = ? WHERE id = ?`)
if err != nil {
return fmt.Errorf("failed to prepare update statement: %w", err)
}
@@ -79,9 +86,12 @@ func MigrateContentHashColumn(db *sql.DB) error {
}
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("failed to commit transaction: %w", err)
// Release savepoint (commits the changes within the outer transaction)
_, err = db.Exec(`RELEASE SAVEPOINT content_hash_migration`)
if err != nil {
return fmt.Errorf("failed to release savepoint: %w", err)
}
savepointReleased = true
return nil
}