@@ -272,6 +272,7 @@ impl Scheduler {
272272 job. job_id ( ) ,
273273 self . namespace_store . clone ( ) ,
274274 self . migration_db . clone ( ) ,
275+ job. disable_foreign_key ,
275276 ) ) ;
276277 // do not enqueue anything until the schema migration is complete
277278 self . has_work = false ;
@@ -374,6 +375,7 @@ impl Scheduler {
374375 job. migration . clone ( ) ,
375376 task,
376377 block_writes,
378+ job. disable_foreign_key ,
377379 ) ) ;
378380 } else {
379381 // there is still a job, but the queue is empty, it means that we are waiting for the
@@ -434,6 +436,7 @@ async fn try_step_task(
434436 migration : Arc < Program > ,
435437 mut task : MigrationTask ,
436438 block_writes : Arc < AtomicBool > ,
439+ disable_foreign_key : bool ,
437440) -> WorkResult {
438441 let old_status = * task. status ( ) ;
439442 let error = match try_step_task_inner (
@@ -443,6 +446,7 @@ async fn try_step_task(
443446 migration,
444447 & task,
445448 block_writes,
449+ disable_foreign_key,
446450 )
447451 . await
448452 {
@@ -485,6 +489,7 @@ async fn try_step_task_inner(
485489 migration : Arc < Program > ,
486490 task : & MigrationTask ,
487491 block_writes : Arc < AtomicBool > ,
492+ disable_foreign_key : bool ,
488493) -> Result < ( MigrationTaskStatus , Option < String > ) , Error > {
489494 let status = * task. status ( ) ;
490495 let mut db_connection = connection_maker
@@ -508,6 +513,9 @@ async fn try_step_task_inner(
508513 let job_id = task. job_id ( ) ;
509514 let ( status, error) = tokio:: task:: spawn_blocking ( move || -> Result < _ , Error > {
510515 db_connection. with_raw ( move |conn| {
516+ if disable_foreign_key {
517+ conn. execute ( "PRAGMA foreign_keys=off" , ( ) ) ?;
518+ }
511519 let mut txn = conn. transaction ( ) ?;
512520
513521 match status {
@@ -526,6 +534,10 @@ async fn try_step_task_inner(
526534 let ( new_status, error) = step_task ( & mut txn, job_id) ?;
527535 txn. commit ( ) ?;
528536
537+ if disable_foreign_key {
538+ conn. execute ( "PRAGMA foreign_keys=off" , ( ) ) ?;
539+ }
540+
529541 if new_status. is_finished ( ) {
530542 block_writes. store ( false , std:: sync:: atomic:: Ordering :: SeqCst ) ;
531543 }
@@ -737,6 +749,7 @@ async fn step_job_run_success(
737749 job_id : i64 ,
738750 namespace_store : NamespaceStore ,
739751 migration_db : Arc < Mutex < MetaStoreConnection > > ,
752+ disable_foreign_key : bool ,
740753) -> WorkResult {
741754 try_step_job ( MigrationJobStatus :: WaitingRun , async move {
742755 // TODO: check that all tasks actually reported success before migration
@@ -757,6 +770,9 @@ async fn step_job_run_success(
757770 . map_err ( |e| Error :: FailedToConnect ( schema. clone ( ) , e. into ( ) ) ) ?;
758771 tokio:: task:: spawn_blocking ( move || -> Result < ( ) , Error > {
759772 connection. with_raw ( |conn| -> Result < ( ) , Error > {
773+ if disable_foreign_key {
774+ conn. execute ( "PRAGMA foreign_keys=off" , ( ) ) ?;
775+ }
760776 let mut txn = conn. transaction ( ) ?;
761777 let schema_version =
762778 txn. query_row ( "PRAGMA schema_version" , ( ) , |row| row. get :: < _ , i64 > ( 0 ) ) ?;
@@ -774,6 +790,9 @@ async fn step_job_run_success(
774790 txn. pragma_update ( None , "schema_version" , job_id) ?;
775791 // update schema version to job_id?
776792 txn. commit ( ) ?;
793+ if disable_foreign_key {
794+ conn. execute ( "PRAGMA foreign_keys=on" , ( ) ) ?;
795+ }
777796 }
778797
779798 Ok ( ( ) )
0 commit comments