diff options
| author | Kevin Hoerr <kjhoerr@submelon.dev> | 2026-05-04 17:36:37 -0400 |
|---|---|---|
| committer | Kevin Hoerr <kjhoerr@submelon.dev> | 2026-05-04 17:36:37 -0400 |
| commit | cdb8af529333adfb07da0e36a0d29e78c0a15ead (patch) | |
| tree | 06429998dd0a32d24017bb1955f207fea71719ea /src/app/store | |
| parent | 50709b9525086548ef89760dd885228aa6410754 (diff) | |
| download | equity-tracker-cdb8af529333adfb07da0e36a0d29e78c0a15ead.tar.gz equity-tracker-cdb8af529333adfb07da0e36a0d29e78c0a15ead.tar.bz2 equity-tracker-cdb8af529333adfb07da0e36a0d29e78c0a15ead.zip | |
sqlite.rs: Split migration execution into separate function
Diffstat (limited to 'src/app/store')
| -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(); |
