diff --git a/db/upgrade/mod.rs b/db/upgrade/mod.rs index 082d339..c95b6ee 100644 --- a/db/upgrade/mod.rs +++ b/db/upgrade/mod.rs @@ -89,6 +89,12 @@ pub fn run(args: &Args, conn: &mut rusqlite::Connection) -> Result<(), Error> { } } + // Enforce foreign keys. This is on by default with --features=bundled (as rusqlite + // compiles the SQLite3 amalgamation with -DSQLITE_DEFAULT_FOREIGN_KEYS=1). Ensure it's + // always on. Note that our foreign keys are immediate rather than deferred, so we have to + // be careful about the order of operations during the upgrade. + conn.execute("pragma foreign_keys = on", &[])?; + // WAL is the preferred journal mode for normal operation; it reduces the number of syncs // without compromising safety. set_journal_mode(&conn, "wal").unwrap(); diff --git a/db/upgrade/v1_to_v2.rs b/db/upgrade/v1_to_v2.rs index dff1e94..9748bdd 100644 --- a/db/upgrade/v1_to_v2.rs +++ b/db/upgrade/v1_to_v2.rs @@ -244,7 +244,12 @@ pub fn run(args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> old_camera cross join sample_file_dir where old_camera.sub_rtsp_path != ''; + "#)?; + // Add the new video_sample_entry rows, before inserting the recordings referencing them. + fix_video_sample_entry(tx)?; + + tx.execute_batch(r#" insert into recording select r.composite_id, @@ -270,14 +275,6 @@ pub fn run(args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> old_recording r join recording_playback p on (r.composite_id = p.composite_id); "#)?; - fix_video_sample_entry(tx)?; - - tx.execute_batch(r#" - drop table old_camera; - drop table old_recording; - drop table old_video_sample_entry; - "#)?; - Ok(()) } diff --git a/db/upgrade/v2_to_v3.rs b/db/upgrade/v2_to_v3.rs index bb43352..4dfa50d 100644 --- a/db/upgrade/v2_to_v3.rs +++ b/db/upgrade/v2_to_v3.rs @@ -114,6 +114,9 @@ pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> from old_recording_playback; drop table old_recording_playback; + drop table old_recording; + drop table old_camera; + drop table old_video_sample_entry; "#)?; Ok(()) }