From 999996961e6e8ebcde125ff0022df875d62817b3 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Thu, 10 Oct 2024 20:52:46 -0400 Subject: Fix invalid migration. The original version of this migration happened to work correctly, by accident, for databases with exactly one login. I missed this, and so did Kit, because both of our test databases _actually do_ contain exactly one login, and because I didn't run the tests before committing the migration. The fixed version works correctly for all scenarios I tested (zero, one, and two users, not super thorough). I've added code to patch out the original migration hash in databases that have it; no further corrective work is needed, as if the migration failed, then it got backed out anyways, and if it succeeded, you fell into the "one user" case. --- src/db/mod.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src') diff --git a/src/db/mod.rs b/src/db/mod.rs index 36d888f..fa3d74e 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -19,6 +19,15 @@ pub async fn prepare(url: &str, backup_url: &str) -> Result { // 9bd6d9862b1c243def02200bca2cfbf578ad2a2f or earlier. reject_migration(&pool, "20240831024047", "login", &hex!("9949D238C4099295EC4BEE734BFDA8D87513B2973DFB895352A11AB01DD46CB95314B7F1B3431B77E3444A165FE3DC28")).await?; + // Original version of this migration was buggy, but didn't require a + // database reset to fix. + migration_replaced( + &pool, + "20241009031441", + &hex!("4B5873397C8BA9CFAF49172EE6DE455CD643A27BD71032ECD8EFA7684362FE620A8F6B27D493AF8D9A570C38CC1A6416"), + &hex!("E5CDEDA38F2BCE4C24A45E58D3BDE3FF2C30B1431C3B01870BB9DEB142E5A200B9C850C3C72A45D352C15D8DB51B8467"), + ).await?; + let backup_pool = create(backup_url).await?; backup::Backup::from(&pool) .to(&backup_pool) @@ -67,6 +76,31 @@ async fn reject_migration( Ok(()) } +async fn migration_replaced( + pool: &SqlitePool, + version: &str, + original: &[u8], + replacement: &[u8], +) -> Result<(), sqlx::Error> { + let mut conn = pool.acquire().await?; + conn.ensure_migrations_table().await?; + sqlx::query!( + r#" + update _sqlx_migrations + set checksum = $1 + where version = $2 + and checksum = $3 + "#, + replacement, + version, + original, + ) + .execute(&mut *conn) + .await?; + + Ok(()) +} + /// Errors occurring during database setup. #[derive(Debug, thiserror::Error)] pub enum Error { -- cgit v1.2.3