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

[cli] Add -u, --universal-time option in generate to use UTC Time instead of Local Time for migration filename. #947

Merged
merged 1 commit into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
[cli] Add -u, --universal-time option in generate to use Utc instea…
…d of Local
  • Loading branch information
Animeshz committed Aug 8, 2022
commit 89206d37ff772e8ec1e1029eea7f9d950977db93
8 changes: 8 additions & 0 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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,
Expand Down
19 changes: 13 additions & 6 deletions sea-orm-cli/src/commands/migrate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chrono::Local;
use chrono::{Local, Utc};
use regex::Regex;
use std::{error::Error, fs, io::Write, path::Path, process::Command};

Expand All @@ -11,9 +11,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),
Expand Down Expand Up @@ -104,12 +105,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)?;
Expand Down
7 changes: 4 additions & 3 deletions sea-orm-migration/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
};

Expand Down