From 0f331c33fe2d702b77c0d2c45de3d9818dc790f2 Mon Sep 17 00:00:00 2001 From: John Vandenberg Date: Sun, 31 Mar 2024 13:24:08 +0800 Subject: [PATCH] Feedback fixes --- butane_core/src/migrations/fsmigrations.rs | 32 +++++++++---------- butane_core/src/migrations/memmigrations.rs | 8 +++++ butane_core/src/migrations/migration.rs | 6 ++-- butane_core/src/migrations/mod.rs | 3 +- .../20201229_171630604_likes/info.json | 2 +- 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/butane_core/src/migrations/fsmigrations.rs b/butane_core/src/migrations/fsmigrations.rs index fadf31e7..c59fbbc9 100644 --- a/butane_core/src/migrations/fsmigrations.rs +++ b/butane_core/src/migrations/fsmigrations.rs @@ -27,7 +27,7 @@ struct MigrationInfo { /// last modified, and therefore where the last .table file for /// it exists. #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] - existing_schema: BTreeMap, + table_bases: BTreeMap, /// List of backends supported by this migration. backends: Vec, } @@ -35,7 +35,7 @@ impl MigrationInfo { fn new() -> Self { MigrationInfo { from_name: None, - existing_schema: BTreeMap::new(), + table_bases: BTreeMap::new(), backends: Vec::new(), } } @@ -185,31 +185,29 @@ impl MigrationMut for FsMigration { fn add_unmodified_table( &mut self, table: &ATable, - from_migration: &impl Migration, + from_migration_name: &str, ) -> Result<()> { // It isnt possible to use from_migration.info() as that isn't part of `Migration` trait. // So first step is to convert to FsMutation. let migrations_dir = self.root.parent().unwrap(); let migrations = crate::migrations::from_root(migrations_dir); let migration_list = migrations.all_migrations()?; - let from_mutation = migration_list + let from_info = migration_list .iter() - .find(|&m| m.name() == from_migration.name()) - .unwrap(); - let from_info = from_mutation - .info() - .unwrap_or_else(|err| panic!("Failed to read info of {}: {err}", from_mutation.name())); - let from_existing_schema = from_info.existing_schema; + .find(|&m| m.name() == *from_migration_name) + .ok_or(Error::MigrationError("Migration not found".into()))? + .info()?; + let from_table_bases = from_info.table_bases; // In the previous migration, either the 'source' migration is in the // pre-existing schema info, or the previous migration is the 'source'. - let migration_name = if let Some(migration_name) = from_existing_schema.get(&table.name) { - migration_name.to_string() + let migration_name = if let Some(migration_name) = from_table_bases.get(&table.name) { + migration_name } else { - from_migration.name().to_string() + from_migration_name }; let mut info = self.info()?; - info.existing_schema - .insert(table.name.clone(), migration_name); + info.table_bases + .insert(table.name.clone(), migration_name.to_owned()); self.write_info(&info) } @@ -279,8 +277,8 @@ impl Migration for FsMigration { self.ensure_dir()?; let _lock = self.lock_shared()?; let mut db = ADB::new(); - let existing_schema = self.info()?.existing_schema; - for (table_name, migration_name) in existing_schema { + let table_bases = self.info()?.table_bases; + for (table_name, migration_name) in table_bases { let mut filename = PathBuf::from(self.root.parent().unwrap()); filename.push(migration_name); filename.push(format!("{table_name}.table")); diff --git a/butane_core/src/migrations/memmigrations.rs b/butane_core/src/migrations/memmigrations.rs index ddad0d35..b67a766c 100644 --- a/butane_core/src/migrations/memmigrations.rs +++ b/butane_core/src/migrations/memmigrations.rs @@ -71,6 +71,14 @@ impl MigrationMut for MemMigration { self.db.resolve_types()?; Ok(()) } + #[allow(unused_variables)] + fn add_unmodified_table( + &mut self, + table: &ATable, + from_migration_name: &str, + ) -> Result<()> { + self.add_modified_table(table) + } fn delete_table(&mut self, table: &str) -> Result<()> { self.db.remove_table(table); Ok(()) diff --git a/butane_core/src/migrations/migration.rs b/butane_core/src/migrations/migration.rs index d4f5a67f..879d137e 100644 --- a/butane_core/src/migrations/migration.rs +++ b/butane_core/src/migrations/migration.rs @@ -95,10 +95,8 @@ pub trait MigrationMut: Migration { fn add_unmodified_table( &mut self, table: &ATable, - from_migration: &impl Migration, - ) -> Result<()> { - self.add_modified_table(table) - } + from_migration_name: &str, + ) -> Result<()>; /// Delete the table with the given name. Note that simply /// deleting a table in code does not work -- it will remain with diff --git a/butane_core/src/migrations/mod.rs b/butane_core/src/migrations/mod.rs index 5bfa2ddc..22c4e084 100644 --- a/butane_core/src/migrations/mod.rs +++ b/butane_core/src/migrations/mod.rs @@ -212,7 +212,8 @@ where if modified_tables.contains(&table.name) { m.add_modified_table(table)?; } else { - m.add_unmodified_table(table, from.expect("unmodified requires a from"))?; + let from = from.ok_or(Error::MigrationError("unmodified requires a from".into()))?; + m.add_unmodified_table(table, &from.name())?; } } diff --git a/examples/getting_started/.butane/migrations/20201229_171630604_likes/info.json b/examples/getting_started/.butane/migrations/20201229_171630604_likes/info.json index 92a6c2fe..473354cf 100644 --- a/examples/getting_started/.butane/migrations/20201229_171630604_likes/info.json +++ b/examples/getting_started/.butane/migrations/20201229_171630604_likes/info.json @@ -1,6 +1,6 @@ { "from_name": "20201229_144636751_init", - "existing_schema": { + "table_bases": { "Blog": "20201229_144636751_init", "Post_tags_Many": "20201229_144636751_init", "Tag": "20201229_144636751_init"