Skip to content

Commit

Permalink
Support naming migrations sequentially
Browse files Browse the repository at this point in the history
  • Loading branch information
vmax committed Jul 8, 2023
1 parent 258eaca commit 157e673
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion sqlx-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ pub async fn run(opt: Opt) -> Result<()> {
source,
description,
reversible,
} => migrate::add(&source, &description, reversible).await?,
sequential,
} => migrate::add(&source, &description, reversible, sequential).await?,
MigrateCommand::Run {
source,
dry_run,
Expand Down
13 changes: 12 additions & 1 deletion sqlx-cli/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub async fn add(
migration_source: &str,
description: &str,
reversible: bool,
sequential: bool,
) -> anyhow::Result<()> {
fs::create_dir_all(migration_source).context("Unable to create migrations directory")?;

Expand All @@ -58,7 +59,17 @@ pub async fn add(
}

let dt = Utc::now();
let file_prefix = dt.format("%Y%m%d%H%M%S").to_string();
let file_prefix = if sequential {
format!(
"{:04}",
migrator
.iter()
.last()
.map_or(1, |last_migration| last_migration.version + 1)
)
} else {
dt.format("%Y%m%d%H%M%S").to_string()
};
if reversible {
create_file(
migration_source,
Expand Down
6 changes: 5 additions & 1 deletion sqlx-cli/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub struct MigrateOpt {
#[derive(Parser, Debug)]
pub enum MigrateCommand {
/// Create a new migration with the given description,
/// and the current time as the version.
/// and (by default) the current time as the version.
Add {
description: String,

Expand All @@ -121,6 +121,10 @@ pub enum MigrateCommand {
/// else creates a single sql file
#[clap(short)]
reversible: bool,

/// If true, migrations are named sequentially instead of timestamp
#[clap(short)]
sequential: bool,
},

/// Run all pending migrations.
Expand Down

0 comments on commit 157e673

Please sign in to comment.