Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb committed Mar 27, 2024
1 parent 3681774 commit 45ed975
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
28 changes: 24 additions & 4 deletions butane_core/src/migrations/fsmigrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ use crate::{ConnectionMethods, DataObject, Error, Result};
type SqlTypeMap = BTreeMap<TypeKey, DeferredSqlType>;
const TYPES_FILENAME: &str = "types.json";

#[derive(Debug, Deserialize, Serialize)]
/// Metadata stored in each migration in the filesystem.
#[derive(Debug, Default, Deserialize, Serialize)]
struct MigrationInfo {
/// The migration this one is based on, or None if this is the
/// first migration in the chain
#[serde(default, skip_serializing_if = "Option::is_none")]
from_name: Option<String>,
/// A mapping of table name to the prior migration where it was
/// 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>,
/// List of backends supported by this migration.
backends: Vec<String>,
}
impl MigrationInfo {
Expand All @@ -36,6 +41,7 @@ impl MigrationInfo {
}
}

/// Metadata about the migration series.
#[derive(Debug, Deserialize, Serialize)]
struct MigrationsState {
latest: Option<String>,
Expand All @@ -46,7 +52,7 @@ impl MigrationsState {
}
}

/// A migration stored in the filesystem
/// A migration stored in the filesystem.
#[derive(Clone, Debug)]
pub struct FsMigration {
fs: Rc<dyn Filesystem>,
Expand Down Expand Up @@ -140,14 +146,25 @@ impl FsMigration {
MigrationLock::new_shared(&self.root.join("lock"))
}

/// Delete all of the files except info.json which is recreated
/// with only `from_name` set to allow migration series traversal.
pub fn delete_db(&self) -> Result<()> {
let entries = self.fs.list_dir(&self.root)?;
for entry in entries {
match entry.file_name() {
None => continue,
Some(name) => {
let name = name.to_string_lossy();
if name != "info.json" {
if name == "info.json" {
// Re-create info.json using the minimum required to allow
// `all_migrations` to traverse the list.
let info = self.info()?;
let info = MigrationInfo {
from_name: info.from_name,
..Default::default()
};
self.write_info(&info)?;
} else {
self.fs.delete(&entry)?;
}
}
Expand Down Expand Up @@ -179,7 +196,10 @@ impl MigrationMut for FsMigration {
.iter()
.find(|&m| m.name() == from_migration.name())
.unwrap();
let from_existing_schema = from_mutation.info()?.existing_schema;
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;
// 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) {
Expand Down
5 changes: 3 additions & 2 deletions butane_core/src/migrations/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ pub trait MigrationMut: Migration {
/// Adds an abstract table to the migration. The table state should
/// represent the expected state after the migration has been
/// applied. It is expected that all tables will be added to the
/// migration in this fashion.
/// migration in this fashion, if they were modified in this migration.
fn add_modified_table(&mut self, table: &ATable) -> Result<()>;

/// Marks a table as not modified.
/// Marks a table as not modified in this migration.
/// Use instead of `add_modified_table`.
#[allow(unused_variables)]
fn add_unmodified_table(
&mut self,
Expand Down
3 changes: 0 additions & 3 deletions butane_core/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,13 @@ where
ops.push(Operation::AddTableIfNotExists(migrations_table()));
}

let mut unmodified_tables: Vec<String> = Vec::new();

let mut m = self.new_migration(name);
// Save the DB for use by other migrations from this one
for table in to_db.tables() {
if modified_tables.contains(&table.name) {
m.add_modified_table(table)?;
} else {
m.add_unmodified_table(table, from.expect("unmodified requires a from"))?;
unmodified_tables.push(table.name.clone());
}
}

Expand Down

0 comments on commit 45ed975

Please sign in to comment.