reversible migrations for cli

- adds a -r flag whihc will create a reversible migration
- add revert subcommand, which reverts the last migration
- add --dry-run flag to migration run command, which list the migrations that will be applied
- updates add migration to check if all migration are of same type, i.e cannot mix and match reversible and simple migrations
This commit is contained in:
sid
2020-08-22 23:58:07 +05:30
committed by Ryan Leckey
parent 70fa667063
commit f41551f3ad
14 changed files with 331 additions and 21 deletions

View File

@@ -40,5 +40,5 @@ pub async fn reset(migration_source: &str, uri: &str, confirm: bool) -> anyhow::
pub async fn setup(migration_source: &str, uri: &str) -> anyhow::Result<()> {
create(uri).await?;
migrate::run(migration_source, uri).await
migrate::run(migration_source, uri, false).await
}

View File

@@ -31,8 +31,16 @@ hint: This command only works in the manifest directory of a Cargo package."#
match opt.command {
Command::Migrate(migrate) => match migrate.command {
MigrateCommand::Add { description } => migrate::add(&migrate.source, &description)?,
MigrateCommand::Run => migrate::run(&migrate.source, &database_url).await?,
MigrateCommand::Add {
description,
reversible,
} => migrate::add(&migrate.source, &description, reversible).await?,
MigrateCommand::Run { dry_run } => {
migrate::run(&migrate.source, &database_url, dry_run).await?
}
MigrateCommand::Revert { dry_run } => {
migrate::revert(&migrate.source, &database_url, dry_run).await?
}
MigrateCommand::Info => migrate::info(&migrate.source, &database_url).await?,
},

View File

@@ -1,22 +1,25 @@
use anyhow::{bail, Context};
use chrono::Utc;
use console::style;
use sqlx::migrate::{Migrate, MigrateError, Migrator};
use sqlx::migrate::{Migrate, MigrateError, MigrationType, Migrator};
use sqlx::{AnyConnection, Connection};
use std::fs::{self, File};
use std::io::Write;
use std::path::Path;
use std::time::Duration;
pub fn add(migration_source: &str, description: &str) -> anyhow::Result<()> {
use chrono::prelude::*;
fn create_file(
migration_source: &str,
file_prefix: &str,
description: &str,
migration_type: MigrationType,
) -> anyhow::Result<()> {
use std::path::PathBuf;
fs::create_dir_all(migration_source).context("Unable to create migrations directory")?;
let dt = Utc::now();
let mut file_name = dt.format("%Y%m%d%H%M%S").to_string();
let mut file_name = file_prefix.to_string();
file_name.push_str("_");
file_name.push_str(&description.replace(' ', "_"));
file_name.push_str(".sql");
file_name.push_str(migration_type.suffix());
let mut path = PathBuf::new();
path.push(migration_source);
@@ -26,7 +29,49 @@ pub fn add(migration_source: &str, description: &str) -> anyhow::Result<()> {
let mut file = File::create(&path).context("Failed to create migration file")?;
file.write_all(b"-- Add migration script here\n")?;
file.write_all(migration_type.file_content().as_bytes())?;
Ok(())
}
pub async fn add(
migration_source: &str,
description: &str,
reversible: bool,
) -> anyhow::Result<()> {
fs::create_dir_all(migration_source).context("Unable to create migrations directory")?;
let migrator = Migrator::new(Path::new(migration_source)).await?;
// This checks if all existing migrations are of the same type as the reverisble flag passed
for migration in migrator.iter() {
if migration.migration_type.is_reversible() != reversible {
bail!(MigrateError::InvalidMixReversibleAndSimple);
}
}
let dt = Utc::now();
let file_prefix = dt.format("%Y%m%d%H%M%S").to_string();
if reversible {
create_file(
migration_source,
&file_prefix,
description,
MigrationType::ReversibleUp,
)?;
create_file(
migration_source,
&file_prefix,
description,
MigrationType::ReversibleDown,
)?;
} else {
create_file(
migration_source,
&file_prefix,
description,
MigrationType::Simple,
)?;
}
Ok(())
}
@@ -55,7 +100,7 @@ pub async fn info(migration_source: &str, uri: &str) -> anyhow::Result<()> {
Ok(())
}
pub async fn run(migration_source: &str, uri: &str) -> anyhow::Result<()> {
pub async fn run(migration_source: &str, uri: &str, dry_run: bool) -> anyhow::Result<()> {
let migrator = Migrator::new(Path::new(migration_source)).await?;
let mut conn = AnyConnection::connect(uri).await?;
@@ -68,13 +113,23 @@ pub async fn run(migration_source: &str, uri: &str) -> anyhow::Result<()> {
}
for migration in migrator.iter() {
if migration.migration_type.is_down_migration() {
// Skipping down migrations
continue;
}
if migration.version > version {
let elapsed = conn.apply(migration).await?;
let elapsed = if dry_run {
Duration::new(0, 0)
} else {
conn.apply(migration).await?
};
let text = if dry_run { "Can apply" } else { "Applied" };
println!(
"{}/{} {} {}",
"{} {}/{} {} {}",
text,
style(migration.version).cyan(),
style("migrate").green(),
style(migration.migration_type.label()).green(),
migration.description,
style(format!("({:?})", elapsed)).dim()
);
@@ -85,3 +140,54 @@ pub async fn run(migration_source: &str, uri: &str) -> anyhow::Result<()> {
Ok(())
}
pub async fn revert(migration_source: &str, uri: &str, dry_run: bool) -> anyhow::Result<()> {
let migrator = Migrator::new(Path::new(migration_source)).await?;
let mut conn = AnyConnection::connect(uri).await?;
conn.ensure_migrations_table().await?;
let (version, dirty) = conn.version().await?.unwrap_or((0, false));
if dirty {
bail!(MigrateError::Dirty(version));
}
let mut is_applied = false;
for migration in migrator.iter().rev() {
if !migration.migration_type.is_down_migration() {
// Skipping non down migration
// This will skip any standard or up migration file
continue;
}
if migration.version > version {
// Skipping unapplied migrations
continue;
}
let elapsed = if dry_run {
Duration::new(0, 0)
} else {
conn.revert(migration).await?
};
let text = if dry_run { "Can apply" } else { "Applied" };
println!(
"{} {}/{} {} {}",
text,
style(migration.version).cyan(),
style(migration.migration_type.label()).green(),
migration.description,
style(format!("({:?})", elapsed)).dim()
);
is_applied = true;
// Only a single migration will be reverted at a time, so we break
break;
}
if !is_applied {
println!("No migrations available to revert");
}
Ok(())
}

View File

@@ -93,10 +93,28 @@ pub struct MigrateOpt {
pub enum MigrateCommand {
/// Create a new migration with the given description,
/// and the current time as the version.
Add { description: String },
Add {
description: String,
/// If true, creates a pair of up and down migration files with same version
/// else creates a single sql file
#[clap(short)]
reversible: bool,
},
/// Run all pending migrations.
Run,
Run {
/// List all the migrations to be run without applying
#[clap(long)]
dry_run: bool,
},
/// Revert the latest migration with a down file.
Revert {
/// List the migration to be reverted without applying
#[clap(long)]
dry_run: bool,
},
/// List all available migrations.
Info,