Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt unmigrate for schema reverts #260

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. Not schema change required.");
jayvdb marked this conversation as resolved.
Show resolved Hide resolved
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. Not 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 {
jayvdb marked this conversation as resolved.
Show resolved Hide resolved
/// 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);