Archived
1
0
Fork 0

feat: add date format option

This commit is contained in:
Dmitriy Pleshevskiy 2021-05-16 16:55:59 +03:00
parent 11874bd8a4
commit def6534fd1
2 changed files with 15 additions and 4 deletions

View file

@ -6,7 +6,8 @@ use std::fs;
pub(crate) fn make_migration(app: &App, opts: MakeCommandOpt) -> StdResult<()> { pub(crate) fn make_migration(app: &App, opts: MakeCommandOpt) -> StdResult<()> {
let config = app.config()?; let config = app.config()?;
let now = Local::now().format("%y%m%d%H%M%S"); let date_format = config.migrations.date_format();
let formatted_current_timestamp = Local::now().format(&date_format);
let migration_name: String = opts let migration_name: String = opts
.migration_name .migration_name
@ -18,9 +19,10 @@ pub(crate) fn make_migration(app: &App, opts: MakeCommandOpt) -> StdResult<()> {
}) })
.collect(); .collect();
let migration_dir_path = config let migration_dir_path = config.migration_dir_path().join(format!(
.migration_dir_path() "{}_{}",
.join(format!("{}_{}", now, migration_name)); formatted_current_timestamp, migration_name
));
if !migration_dir_path.exists() { if !migration_dir_path.exists() {
fs::create_dir_all(&migration_dir_path)?; fs::create_dir_all(&migration_dir_path)?;
} }

View file

@ -148,6 +148,8 @@ pub(crate) struct MigrationsConfig {
#[serde(default = "default_migrations_table_name")] #[serde(default = "default_migrations_table_name")]
table_name: String, table_name: String,
date_format: Option<String>,
} }
impl Default for MigrationsConfig { impl Default for MigrationsConfig {
@ -155,6 +157,7 @@ impl Default for MigrationsConfig {
MigrationsConfig { MigrationsConfig {
directory: default_migrations_directory(), directory: default_migrations_directory(),
table_name: default_migrations_table_name(), table_name: default_migrations_table_name(),
date_format: None,
} }
} }
} }
@ -189,6 +192,12 @@ impl MigrationsConfig {
self.table_name.clone() self.table_name.clone()
} }
} }
pub fn date_format(&self) -> String {
self.date_format
.clone()
.unwrap_or_else(|| String::from("%y%m%d%H%M%S"))
}
} }
//===========================================================================// //===========================================================================//