Skip to content

Commit

Permalink
Feedback fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb committed Mar 31, 2024
1 parent a70a86e commit 0f331c3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
32 changes: 15 additions & 17 deletions butane_core/src/migrations/fsmigrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ 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<String, String>,
table_bases: BTreeMap<String, String>,
/// List of backends supported by this migration.
backends: Vec<String>,
}
impl MigrationInfo {
fn new() -> Self {
MigrationInfo {
from_name: None,
existing_schema: BTreeMap::new(),
table_bases: BTreeMap::new(),
backends: Vec::new(),
}
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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"));
Expand Down
8 changes: 8 additions & 0 deletions butane_core/src/migrations/memmigrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ impl MigrationMut for MemMigration {
self.db.resolve_types()?;
Ok(())

Check warning on line 72 in butane_core/src/migrations/memmigrations.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Diff in /home/runner/work/butane/butane/butane_core/src/migrations/memmigrations.rs
}
#[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(())
Expand Down
6 changes: 2 additions & 4 deletions butane_core/src/migrations/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion butane_core/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ where
if modified_tables.contains(&table.name) {

Check warning on line 212 in butane_core/src/migrations/mod.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Diff in /home/runner/work/butane/butane/butane_core/src/migrations/mod.rs
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())?;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 0f331c3

Please sign in to comment.