From cdb8af529333adfb07da0e36a0d29e78c0a15ead Mon Sep 17 00:00:00 2001 From: Kevin Hoerr Date: Mon, 4 May 2026 17:36:37 -0400 Subject: sqlite.rs: Split migration execution into separate function --- src/app/store/sqlite.rs | 50 +++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) (limited to 'src/app/store') diff --git a/src/app/store/sqlite.rs b/src/app/store/sqlite.rs index 631acc6..e124675 100644 --- a/src/app/store/sqlite.rs +++ b/src/app/store/sqlite.rs @@ -215,6 +215,31 @@ impl SQLiteStore { } } + /// Executes a migration against the database, and inserts a migration record into the migration_control table. + fn run_and_insert_migration(&mut self, id: usize, migration: &db::Migration) -> Result<(), ContextError> { + let conn = self.get_connection()?; + let timestamp = Local::now().to_string(); + tracing::event!( + tracing::Level::INFO, + id = id, + directory = migration.directory, + filename = migration.filename, + timestamp = timestamp, + "Performing migration." + ); + + conn.execute(migration.script_contents.clone())?; + let mut statement = conn.prepare(Self::MIGRATION_CONTROL_INSERT_QUERY)?; + statement.bind::<&[(_, sqlite::Value)]>(&[ + (":migration_id", i64::try_from(id)?.into()), + (":migration_filename", (&*migration.filename).into()), + (":migration_timestamp", (&*timestamp).into()), + ])?; + + statement.next()?; + Ok(()) + } + /// Gets list of available migrations, asserts whether they need to be run, and executes them against the sqlite db. fn run_migrations(&mut self) -> Result<(), ContextError> { // - get list of sql files @@ -228,33 +253,14 @@ impl SQLiteStore { seed_id ); - let conn = self.get_connection()?; - - // - against list of migration records, run any scripts absent + // Against list of migration records, run any scripts absent for migration in &migrations { let prev_migration = run_migrations .iter() .find(|m| m.migration_filename == migration.filename); if prev_migration.is_none() { - let timestamp = Local::now().to_string(); - tracing::event!( - tracing::Level::INFO, - migration_id = seed_id, - directory = migration.directory, - migration_filename = migration.filename, - migration_timestamp = timestamp, - "Performing migration." - ); - - conn.execute(migration.script_contents.clone())?; - let mut statement = conn.prepare(Self::MIGRATION_CONTROL_INSERT_QUERY)?; - statement.bind::<&[(_, sqlite::Value)]>(&[ - (":migration_id", i64::try_from(seed_id)?.into()), - (":migration_filename", (&*migration.filename).into()), - (":migration_timestamp", (&*timestamp).into()), - ])?; - - statement.next()?; + self.run_and_insert_migration(seed_id, migration)?; + seed_id += 1; } else { let actual_migration = prev_migration.unwrap(); -- cgit v1.3