Skip to content

Commit

Permalink
dbutil: export method for doing SQLite transaction without foreign keys
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jul 13, 2024
1 parent 03648b3 commit fe2e452
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions dbutil/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (db *Database) setVersion(ctx context.Context, version, compat int) error {
return err
}

func (db *Database) doSQLiteFKeyOffTxn(ctx context.Context, doUpgrade func(context.Context) error) error {
func (db *Database) DoSQLiteTransactionWithoutForeignKeys(ctx context.Context, doUpgrade func(context.Context) error) error {
conn, err := db.AcquireConn(ctx)
if err != nil {
return fmt.Errorf("failed to acquire connection: %w", err)
Expand All @@ -178,6 +178,7 @@ func (db *Database) doSQLiteFKeyOffTxn(ctx context.Context, doUpgrade func(conte
return nil
})
if err != nil {
_, _ = conn.ExecContext(ctx, "PRAGMA foreign_keys=ON")
return err
}
_, err = conn.ExecContext(ctx, "PRAGMA foreign_keys=ON")
Expand Down Expand Up @@ -236,7 +237,7 @@ func (db *Database) Upgrade(ctx context.Context) error {
case TxnModeSQLiteForeignKeysOff:
switch db.Dialect {
case SQLite:
err = db.doSQLiteFKeyOffTxn(ctx, doUpgrade)
err = db.DoSQLiteTransactionWithoutForeignKeys(ctx, doUpgrade)
default:
err = db.DoTxn(ctx, nil, doUpgrade)
}
Expand Down

0 comments on commit fe2e452

Please sign in to comment.