1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
// ┌───────────────────────────────────────────────────────────────────────────┐
// │ │
// │ ██████╗ ██████╗ ██████╗ Copyright (C) The Prospective Company │
// │ ██╔══██╗██╔══██╗██╔═══██╗ All Rights Reserved - April 2022 │
// │ ██████╔╝██████╔╝██║ ██║ │
// │ ██╔═══╝ ██╔══██╗██║ ██║ Proprietary and confidential. Unauthorized │
// │ ██║ ██║ ██║╚██████╔╝ copying of this file, via any medium is │
// │ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ strictly prohibited. │
// │ │
// └───────────────────────────────────────────────────────────────────────────┘
use crate::Migrator;
use crate::error::Result;
use clap::{Parser, Subcommand};
use colored::Colorize;
use prettytable::{format, row, Table};
#[derive(Debug, Parser)]
#[clap(about = "Promad migration tool")]
pub struct PromadCli {
#[clap(subcommand)]
pub subcmd: PromadSubcommand,
}
/// The subcommands of the migration CLI.
/// This can be embedded in other CLI tools so that
/// users can include migration commands in their server
/// binary, etc.
#[derive(Debug, Subcommand)]
pub enum PromadSubcommand {
#[clap(about = "Apply migrations up to a specific migrations")]
Apply {
#[clap(help = "The name of the migrations to apply to (inclusive)")]
name: Option<String>,
},
#[clap(about = "Revert up to a specific migrations")]
Revert {
#[clap(help = "The name of the migrations to revert to (inclusive)")]
name: String,
},
#[clap(about = "Revert all migrations")]
RevertAll,
#[clap(about = "List all changes")]
List,
}
/// Execute the subcommand given a migrator.
pub async fn interpreter<DB: sqlx::Database>(
subcmd: PromadSubcommand,
migrator: Migrator<DB>,
) -> Result<()> {
match subcmd {
PromadSubcommand::Apply { name } => match name {
Some(name) => {
migrator.apply_to_inclusive(&name).await?;
}
None => {
migrator.apply_all().await?;
}
},
PromadSubcommand::Revert { name } => {
migrator.revert_to_inclusive(&name).await?;
}
PromadSubcommand::List => {
let mut table = Table::new();
let format = format::FormatBuilder::new()
.column_separator('|')
.borders(' ')
.separators(
&[format::LinePosition::Title],
format::LineSeparator::new('-', '+', ' ', ' '),
)
.padding(1, 1)
.build();
table.set_format(format);
table.set_titles(row!["Name", "Ran", "Run Time"]);
migrator.list_migrations().await?.iter().for_each(|row| {
table.add_row(row![
row.name.bold(),
if row.run_at.is_some() {
"✓".bold().green()
} else {
"✗".bold().dimmed()
},
row.run_at.map(|x| x.to_string()).unwrap_or_default()
]);
});
// Print the table to stdout
table.printstd();
}
PromadSubcommand::RevertAll => {
migrator.revert_all().await?;
}
}
Ok(())
}