diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2024-10-05 00:36:53 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2024-10-05 00:48:12 -0400 |
| commit | 142690963c0b297e7e76bd6aca170d016ea261ff (patch) | |
| tree | 20e21f60117c7abcdee8f77cc7ffa6514de2a395 /src/db/mod.rs | |
| parent | e1551113323d5a496b826d7b0265b1be6235f45c (diff) | |
Use sqlx's API, not SQL groveling, to find unwanted migrations.
Diffstat (limited to 'src/db/mod.rs')
| -rw-r--r-- | src/db/mod.rs | 46 |
1 files changed, 12 insertions, 34 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs index 61d5c18..090fa38 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -2,8 +2,9 @@ mod backup; use std::str::FromStr; +use hex_literal::hex; use sqlx::{ - migrate::MigrateDatabase as _, + migrate::{Migrate as _, MigrateDatabase as _}, sqlite::{Sqlite, SqliteConnectOptions, SqlitePool, SqlitePoolOptions}, }; @@ -16,7 +17,7 @@ pub async fn prepare(url: &str, backup_url: &str) -> Result<SqlitePool, Error> { // First migration of original migration series, from commit // 9bd6d9862b1c243def02200bca2cfbf578ad2a2f or earlier. - reject_migration(&pool, "20240831024047", "login", "9949D238C4099295EC4BEE734BFDA8D87513B2973DFB895352A11AB01DD46CB95314B7F1B3431B77E3444A165FE3DC28").await?; + reject_migration(&pool, "20240831024047", "login", &hex!("9949D238C4099295EC4BEE734BFDA8D87513B2973DFB895352A11AB01DD46CB95314B7F1B3431B77E3444A165FE3DC28")).await?; let backup_pool = create(backup_url).await?; backup::Backup::from(&pool) @@ -57,42 +58,19 @@ async fn reject_migration( pool: &SqlitePool, version: &str, description: &str, - checksum_hex: &str, + checksum: &[u8], ) -> Result<(), Error> { - if !sqlx::query_scalar!( - r#" - select count(*) as "exists: bool" - from sqlite_master - where name = '_sqlx_migrations' - "# - ) - .fetch_one(pool) - .await? - { - // No migrations table; this is a fresh DB. - return Ok(()); - } + let mut conn = pool.acquire().await?; + conn.ensure_migrations_table().await?; + let applied = conn.list_applied_migrations().await?; - if !sqlx::query_scalar!( - r#" - select count(*) as "exists: bool" - from _sqlx_migrations - where version = $1 - and description = $2 - and hex(checksum) = $3 - "#, - version, - description, - checksum_hex, - ) - .fetch_one(pool) - .await? - { - // Rejected migration does not exist; this DB never ran it. - return Ok(()); + for migration in applied { + if migration.checksum == checksum { + return Err(Error::Rejected(version.into(), description.into())); + } } - Err(Error::Rejected(version.into(), description.into())) + Ok(()) } /// Errors occurring during database setup. |
