Skip to content

Commit

Permalink
Add a butane clean command (#46)
Browse files Browse the repository at this point in the history
Clean current migration state. Deletes the current migration working
state which is generated on each build. This can be used as a
workaround to remove stale tables from the schema, as Butane does not
currently auto-detect model removals. The next build will recreate
with only tables for the extant models.

This is a workaround for #44
  • Loading branch information
Electron100 authored Feb 6, 2023
1 parent c7ad3ba commit e920672
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions butane_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn main() {
.version(env!("CARGO_PKG_VERSION"))
.author("James Oakley <james@electronstudio.org>")
.about("Manages butane database migrations")
.max_term_width(80)
.subcommand(
clap::SubCommand::with_name("init")
.about("Initialize the database")
Expand Down Expand Up @@ -85,6 +86,9 @@ fn main() {
),
),
)
.subcommand(
clap::SubCommand::with_name("clean")
.about("Clean current migration state. Deletes the current migration working state which is generated on each build. This can be used as a workaround to remove stale tables from the schema, as Butane does not currently auto-detect model removals. The next build will recreate with only tables for the extant models."))
.setting(clap::AppSettings::ArgRequiredElseHelp);
let args = app.get_matches();
match args.subcommand() {
Expand All @@ -107,6 +111,7 @@ fn main() {
}
(_, _) => eprintln!("Unknown delete command. Try: delete table"),
},
("clean", _) => handle_error(clean()),
(cmd, _) => eprintln!("Unknown command {cmd}"),
}
}
Expand Down Expand Up @@ -352,6 +357,11 @@ fn clear_data() -> Result<()> {
Ok(())
}

fn clean() -> Result<()> {
get_migrations()?.clear_current()?;
Ok(())
}

fn get_migrations() -> Result<FsMigrations> {
let root = base_dir()?.join("migrations");
if !root.exists() {
Expand Down
4 changes: 4 additions & 0 deletions butane_core/src/migrations/fsmigrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ impl MigrationsMut for FsMigrations {
fn current(&mut self) -> &mut Self::M {
&mut self.current
}
fn clear_current(&mut self) -> Result<()> {
std::fs::remove_dir_all(&self.current.root)?;
Ok(())
}
fn new_migration(&self, name: &str) -> Self::M {
let mut dir = self.root.clone();
dir.push(name);
Expand Down
5 changes: 5 additions & 0 deletions butane_core/src/migrations/memmigrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ impl MigrationsMut for MemMigrations {
&mut self.current
}

fn clear_current(&mut self) -> Result<()> {
self.current = MemMigration::new("current".to_string());
Ok(())
}

fn new_migration(&self, name: &str) -> Self::M {
MemMigration::new(name.to_string())
}
Expand Down
3 changes: 3 additions & 0 deletions butane_core/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ where
/// - it will never be returned by `latest`, `migrations_since`, `all_migrations` or other similar methods.
fn current(&mut self) -> &mut Self::M;

/// Clears the current state (as would be returned by the `current` method).
fn clear_current(&mut self) -> Result<()>;

/// Create a migration `from` -> `current` named `name`. From may be None, in which
/// case the migration is created from an empty database.
/// Returns true if a migration was created, false if `from` and `current` represent identical states.
Expand Down

0 comments on commit e920672

Please sign in to comment.