diff options
| -rw-r--r-- | src/app/store/sqlite.rs | 48 |
1 files changed, 27 insertions, 21 deletions
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()), - ])?; + self.run_and_insert_migration(seed_id, migration)?; - statement.next()?; seed_id += 1; } else { let actual_migration = prev_migration.unwrap(); |
