[cli] Add -u, --universal-time option in generate
to use Utc instead of Local (#947)
This commit is contained in:
parent
6816e86f4d
commit
ad5e8c1264
@ -52,6 +52,14 @@ pub enum MigrateSubcommands {
|
||||
help = "Name of the new migration"
|
||||
)]
|
||||
migration_name: String,
|
||||
|
||||
#[clap(
|
||||
action,
|
||||
short,
|
||||
long,
|
||||
help = "Generate migration file based on Utc time instead of Local time"
|
||||
)]
|
||||
universal_time: bool,
|
||||
},
|
||||
#[clap(about = "Drop all tables from the database, then reapply all migrations")]
|
||||
Fresh,
|
||||
|
@ -1,4 +1,4 @@
|
||||
use chrono::Local;
|
||||
use chrono::{Local, Utc};
|
||||
use regex::Regex;
|
||||
use std::{
|
||||
error::Error,
|
||||
@ -17,9 +17,10 @@ pub fn run_migrate_command(
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
match command {
|
||||
Some(MigrateSubcommands::Init) => run_migrate_init(migration_dir)?,
|
||||
Some(MigrateSubcommands::Generate { migration_name }) => {
|
||||
run_migrate_generate(migration_dir, &migration_name)?
|
||||
}
|
||||
Some(MigrateSubcommands::Generate {
|
||||
migration_name,
|
||||
universal_time,
|
||||
}) => run_migrate_generate(migration_dir, &migration_name, universal_time)?,
|
||||
_ => {
|
||||
let (subcommand, migration_dir, steps, verbose) = match command {
|
||||
Some(MigrateSubcommands::Fresh) => ("fresh", migration_dir, None, verbose),
|
||||
@ -110,12 +111,18 @@ pub fn run_migrate_init(migration_dir: &str) -> Result<(), Box<dyn Error>> {
|
||||
pub fn run_migrate_generate(
|
||||
migration_dir: &str,
|
||||
migration_name: &str,
|
||||
universal_time: bool,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
println!("Generating new migration...");
|
||||
|
||||
// build new migration filename
|
||||
let now = Local::now();
|
||||
let migration_name = format!("m{}_{}", now.format("%Y%m%d_%H%M%S"), migration_name);
|
||||
const FMT: &str = "%Y%m%d_%H%M%S";
|
||||
let formatted_now = if universal_time {
|
||||
Utc::now().format(FMT)
|
||||
} else {
|
||||
Local::now().format(FMT)
|
||||
};
|
||||
let migration_name = format!("m{}_{}", formatted_now, migration_name);
|
||||
|
||||
create_new_migration(&migration_name, migration_dir)?;
|
||||
update_migrator(&migration_name, migration_dir)?;
|
||||
|
@ -65,9 +65,10 @@ where
|
||||
Some(MigrateSubcommands::Up { num }) => M::up(db, Some(num)).await?,
|
||||
Some(MigrateSubcommands::Down { num }) => M::down(db, Some(num)).await?,
|
||||
Some(MigrateSubcommands::Init) => run_migrate_init(MIGRATION_DIR)?,
|
||||
Some(MigrateSubcommands::Generate { migration_name }) => {
|
||||
run_migrate_generate(MIGRATION_DIR, &migration_name)?
|
||||
}
|
||||
Some(MigrateSubcommands::Generate {
|
||||
migration_name,
|
||||
universal_time,
|
||||
}) => run_migrate_generate(MIGRATION_DIR, &migration_name, universal_time)?,
|
||||
_ => M::up(db, None).await?,
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user