Skip to content

Commit

Permalink
foreign_keys_off
Browse files Browse the repository at this point in the history
  • Loading branch information
epompeii committed Mar 9, 2024
1 parent 9045030 commit 718f228
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion services/api/src/config/config_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,19 @@ fn diesel_database_url(log: &Logger, database_path: &str) {
}

fn run_migrations(database: &mut DbConnection) -> Result<(), ConfigTxError> {
// It is not possible to enable or disable foreign key constraints in the middle of a multi-statement transaction
// (when SQLite is not in autocommit mode).
// Attempting to do so does not return an error; it simply has no effect.
// https://www.sqlite.org/foreignkeys.html#fk_enable
// Therefore, we must run all migrations with foreign key constraints disabled.
// Still use `PRAGMA foreign_keys = OFF` in the migration scripts to disable foreign key constraints when using the CLI.
database
.batch_execute("PRAGMA foreign_keys = OFF")
.map_err(ConfigTxError::Pragma)?;
database
.run_pending_migrations(MIGRATIONS)
.map(|_| ())
.map_err(ConfigTxError::Migrations)?;
// https://www.sqlite.org/foreignkeys.html#fk_enable
database
.batch_execute("PRAGMA foreign_keys = ON")
.map_err(ConfigTxError::Pragma)?;
Expand Down

0 comments on commit 718f228

Please sign in to comment.