Skip to content

Commit

Permalink
Adopt unmigrate for schema reverts (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb authored Apr 29, 2024
1 parent 307d839 commit ccf16bb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
15 changes: 7 additions & 8 deletions butane_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,17 @@ pub fn migrate(base_dir: &PathBuf, name: Option<String>) -> Result<()> {
Ok(())
}

pub fn rollback(base_dir: &PathBuf, name: Option<String>) -> Result<()> {
pub fn unmigrate(base_dir: &PathBuf, name: Option<String>) -> Result<()> {
let spec = load_connspec(base_dir)?;
let conn = butane::db::connect(&spec)?;

match name {
Some(to) => rollback_to(base_dir, conn, &to),
None => rollback_latest(base_dir, conn),
Some(to) => unmigrate_to(base_dir, conn, &to),
None => unmigrate_latest(base_dir, conn),
}
}

pub fn rollback_to(base_dir: &Path, mut conn: Connection, to: &str) -> Result<()> {
pub fn unmigrate_to(base_dir: &Path, mut conn: Connection, to: &str) -> Result<()> {
let ms = get_migrations(base_dir)?;
let to_migration = match ms.get_migration(to) {
Some(m) => m,
Expand All @@ -309,15 +309,14 @@ pub fn rollback_to(base_dir: &Path, mut conn: Connection, to: &str) -> Result<()
});

if to_migration == latest {
eprintln!("That is the latest applied migration, not rolling back to anything.");
eprintln!("That is the latest migration. No schema change required.");
std::process::exit(1);
}

let mut to_unapply = ms.migrations_since(&to_migration)?;
if to_unapply.is_empty() {
return Err(anyhow::anyhow!(
"That is the latest migration, not rolling back to anything.
If you expected something to happen, try specifying the migration to rollback to."
"That is the latest migration. No schema change required."
));
}

Expand All @@ -336,7 +335,7 @@ If you expected something to happen, try specifying the migration to rollback to
Ok(())
}

pub fn rollback_latest(base_dir: &Path, mut conn: Connection) -> Result<()> {
pub fn unmigrate_latest(base_dir: &Path, mut conn: Connection) -> Result<()> {
match get_migrations(base_dir)?.last_applied_migration(&conn)? {
Some(m) => {
println!("Rolling back migration {}", m.name());
Expand Down
9 changes: 5 additions & 4 deletions butane_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use butane_cli::{
add_backend, base_dir, clean, clear_data, collapse_migrations, delete_table,
describe_migration, detach_latest_migration, embed, get_migrations, handle_error, init,
list_backends, list_migrations, make_migration, migrate, regenerate_migrations, remove_backend,
rollback,
unmigrate,
};
use clap::{ArgAction, Parser, Subcommand};

Expand Down Expand Up @@ -73,8 +73,9 @@ However if the migration has been manually edited, it will need to be manually r
},
/// Embed migrations in the source code.
Embed,
/// Rollback migrations. With no arguments, undoes the latest migration. If the name of a migration is specified, rolls back until that migration is the latest applied migration.
Rollback {
/// Undo migrations. With no arguments, undoes the latest migration. If the name of a migration is specified, rolls back until that migration is the latest applied migration.
#[command(alias = "rollback")]
Unmigrate {
/// Migration to roll back to.
name: Option<String>,
},
Expand Down Expand Up @@ -177,7 +178,7 @@ fn main() {
Commands::Regenerate => handle_error(regenerate_migrations(&base_dir)),
Commands::DetachMigration => handle_error(detach_latest_migration(&base_dir)),
Commands::Migrate { name } => handle_error(migrate(&base_dir, name.to_owned())),
Commands::Rollback { name } => handle_error(rollback(&base_dir, name.to_owned())),
Commands::Unmigrate { name } => handle_error(unmigrate(&base_dir, name.to_owned())),
Commands::Embed => handle_error(embed(&base_dir)),
Commands::List => handle_error(list_migrations(&base_dir)),
Commands::Collapse { name } => handle_error(collapse_migrations(&base_dir, Some(name))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn insert_data(connection: &Connection) {
post.save(connection).unwrap();
}

fn migrate_and_rollback(mut connection: Connection) {
fn migrate_and_unmigrate(mut connection: Connection) {
// Migrate forward.
let base_dir = std::path::PathBuf::from(".butane");
let migrations = butane_cli::get_migrations(&base_dir).unwrap();
Expand All @@ -43,7 +43,7 @@ fn migrate_and_rollback(mut connection: Connection) {

insert_data(&connection);

// Rollback migrations.
// Undo migrations.
migrations.unmigrate(&mut connection).unwrap();
}
testall_no_migrate!(migrate_and_rollback);
testall_no_migrate!(migrate_and_unmigrate);
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn insert_data(connection: &Connection) {
post.save(connection).unwrap();
}

fn migrate_and_rollback(mut connection: Connection) {
fn migrate_and_unmigrate(mut connection: Connection) {
// Migrate forward.
let base_dir = std::path::PathBuf::from(".butane");
let migrations = butane_cli::get_migrations(&base_dir).unwrap();
Expand All @@ -36,7 +36,7 @@ fn migrate_and_rollback(mut connection: Connection) {

insert_data(&connection);

// Rollback migrations.
// Undo migrations.
migrations.unmigrate(&mut connection).unwrap();
}
testall_no_migrate!(migrate_and_rollback);
testall_no_migrate!(migrate_and_unmigrate);

0 comments on commit ccf16bb

Please sign in to comment.