Skip to content

Commit

Permalink
added status command
Browse files Browse the repository at this point in the history
  • Loading branch information
emilpriver committed Dec 29, 2023
1 parent 18283a2 commit d2ee246
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 50 deletions.
16 changes: 9 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ async fn main() {
Command::new("status")
.about("Show current migrations to apply")
.arg(
Arg::new("amount")
.short('a')
.long("amount")
.help("Amount of migrations to rollback")
Arg::new("verbose")
.short('v')
.long("verbose")
.help("Include migration content for the non applied migrations")
.action(ArgAction::Set)
.num_args(0..=1),
),
Expand Down Expand Up @@ -106,12 +106,14 @@ async fn main() {
Ok(_) => info!("Success"),
};
}
Some(("status", ..)) => {
match migrate::up().await {
Some(("status", query_matches)) => {
let verbose = query_matches.contains_id("verbose");

match status::status(verbose).await {
Err(err) => {
error!("{:?}", err)
}
Ok(_) => info!("Success"),
Ok(_) => {}
};
}
_ => unreachable!(), // If all subcommands are defined above, anything else is unreachable
Expand Down
65 changes: 22 additions & 43 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,39 @@ use crate::{
use anyhow::{bail, Result};
use log::info;

pub async fn status() -> Result<()> {
pub async fn status(verbose: bool) -> Result<()> {
let database_url = database_url()?;
let mut database = database_drivers::new(&database_url, true).await?;
let folder = migration_folder();

let path = PathBuf::from(&folder);
let files = match get_local_migrations(&path, "down") {
let files = match get_local_migrations(&path, "up") {
Ok(f) => f,
Err(err) => {
bail!("Couldn't read migration folder: {:?}", err)
}
};

let mut migrations = get_local_migrations(folder, ending).await?;
migrations.sort_by(|a, b| a.version.cmp(&b.version));

let mut applied_migrations = get_applied_migrations().await?;
applied_migrations.sort_by(|a, b| a.version.cmp(&b.version));

let mut migrations_to_apply = Vec::new();
let mut migrations_to_revert = Vec::new();

for migration in migrations {
if applied_migrations.contains(&migration) {
continue;
}

migrations_to_apply.push(migration);
}

for applied_migration in applied_migrations {
if migrations.contains(&applied_migration) {
continue;
}

migrations_to_revert.push(applied_migration);
}

if migrations_to_apply.is_empty() && migrations_to_revert.is_empty() {
info!("No migrations to apply or revert");
return Ok(());
}

if !migrations_to_apply.is_empty() {
info!("Migrations to apply:");
for migration in migrations_to_apply {
info!(" {}", migration.name);
}
}

if !migrations_to_revert.is_empty() {
info!("Migrations to revert:");
for migration in migrations_to_revert {
info!(" {}", migration.name);
let migrations: Vec<String> = database
.get_or_create_schema_migrations()
.await?
.into_iter()
.map(|s| s.into_boxed_str())
.collect::<Vec<Box<str>>>()
.into_iter()
.map(|s| s.into())
.collect();

for f in files {
let id = Box::new(f.0.to_string());

if !migrations.contains(&id) {
if verbose {
let query = read_file_content(&f.1);
info!("Pending migration {}: \n {}", id, query);
} else {
info!("Pending {}", id);
}
}
}

Expand Down

0 comments on commit d2ee246

Please sign in to comment.