Skip to content

Commit

Permalink
Disable foreign key checks for running DB migrations
Browse files Browse the repository at this point in the history
Some migrations require disabling foreign key checks for advanced
table manipulation. Unfortunately, disabling foreign keys within
migration doesn't work correctly.

Signed-off-by: Adam Wierzbicki <[email protected]>
  • Loading branch information
Wiezzel committed Jan 28, 2021
1 parent 4d4fa94 commit db3f329
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/persistence/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ impl DbExecutor {
migration: T,
) -> anyhow::Result<()> {
let c = self.conn()?;
Ok(migration(&c, &mut std::io::stderr())?)
// Some migrations require disabling foreign key checks for advanced table manipulation.
// Unfortunately, disabling foreign keys within migration doesn't work correctly.
c.batch_execute("PRAGMA foreign_keys = OFF;")?;
migration(&c, &mut std::io::stderr())?;
c.batch_execute("PRAGMA foreign_keys = ON;")?;
Ok(())
}

pub async fn with_connection<R: Send + 'static, Error, F>(&self, f: F) -> Result<R, Error>
Expand Down

0 comments on commit db3f329

Please sign in to comment.