Skip to content

Commit 52bb9d0

Browse files
committed
fix: sqlx mig add behavior and tests
1 parent 1eb0f49 commit 52bb9d0

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

sqlx-cli/src/opt.rs

+25-26
Original file line numberDiff line numberDiff line change
@@ -435,11 +435,32 @@ impl AddMigrationOpts {
435435

436436
match (self.timestamp, self.sequential, default_versioning) {
437437
(true, false, _) | (false, false, DefaultVersioning::Timestamp) => next_timestamp(),
438-
(false, true, _) | (false, false, DefaultVersioning::Sequential) => {
439-
next_sequential(migrator).unwrap_or_else(|| fmt_sequential(1))
440-
}
438+
(false, true, _) | (false, false, DefaultVersioning::Sequential) => fmt_sequential(
439+
migrator
440+
.migrations
441+
.last()
442+
.map_or(1, |migration| migration.version + 1),
443+
),
441444
(false, false, DefaultVersioning::Inferred) => {
442-
next_sequential(migrator).unwrap_or_else(next_timestamp)
445+
migrator
446+
.migrations
447+
.rchunks(2)
448+
.next()
449+
.and_then(|migrations| {
450+
match migrations {
451+
[previous, latest] => {
452+
// If the latest two versions differ by 1, infer sequential.
453+
(latest.version - previous.version == 1)
454+
.then_some(latest.version + 1)
455+
}
456+
[latest] => {
457+
// If only one migration exists and its version is 0 or 1, infer sequential
458+
matches!(latest.version, 0 | 1).then_some(latest.version + 1)
459+
}
460+
_ => unreachable!(),
461+
}
462+
})
463+
.map_or_else(next_timestamp, fmt_sequential)
443464
}
444465
(true, true, _) => unreachable!("BUG: Clap should have rejected this case"),
445466
}
@@ -450,28 +471,6 @@ fn next_timestamp() -> String {
450471
Utc::now().format("%Y%m%d%H%M%S").to_string()
451472
}
452473

453-
fn next_sequential(migrator: &Migrator) -> Option<String> {
454-
let next_version = migrator
455-
.migrations
456-
.rchunks(2)
457-
.next()
458-
.and_then(|migrations| {
459-
match migrations {
460-
[previous, latest] => {
461-
// If the latest two versions differ by 1, infer sequential.
462-
(latest.version - previous.version == 1).then_some(latest.version + 1)
463-
}
464-
[latest] => {
465-
// If only one migration exists and its version is 0 or 1, infer sequential
466-
matches!(latest.version, 0 | 1).then_some(latest.version + 1)
467-
}
468-
_ => unreachable!(),
469-
}
470-
});
471-
472-
next_version.map(fmt_sequential)
473-
}
474-
475474
fn fmt_sequential(version: i64) -> String {
476475
format!("{version:04}")
477476
}

sqlx-cli/tests/add.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ impl PartialOrd<Self> for FileName {
3434

3535
impl FileName {
3636
fn assert_is_timestamp(&self) {
37-
//if the library is still used in 2050, this will need bumping ^^
38-
assert!(
39-
self.id < 20500101000000,
40-
"{self:?} is too high for a timestamp"
41-
);
4237
assert!(
4338
self.id > 20200101000000,
4439
"{self:?} is too low for a timestamp"
@@ -74,10 +69,12 @@ fn add_migration_sequential() -> anyhow::Result<()> {
7469
.run("hello world1", false, false, true, true)?
7570
.run("hello world2", true, false, true, true)?
7671
.fs_output()?;
77-
assert_eq!(files.len(), 2);
78-
files.assert_is_not_reversible();
72+
assert_eq!(files.len(), 3);
7973
assert_eq!(files.0[0].id, 1);
8074
assert_eq!(files.0[1].id, 2);
75+
assert_eq!(files.0[1].suffix, "down.sql");
76+
assert_eq!(files.0[2].id, 2);
77+
assert_eq!(files.0[2].suffix, "up.sql");
8178
}
8279
Ok(())
8380
}
@@ -126,11 +123,11 @@ fn add_migration_timestamp() -> anyhow::Result<()> {
126123
.run("hello world1", false, true, false, true)?
127124
.run("hello world2", true, false, true, true)?
128125
.fs_output()?;
129-
assert_eq!(files.len(), 2);
130-
files.assert_is_not_reversible();
126+
assert_eq!(files.len(), 3);
131127
files.0[0].assert_is_timestamp();
132128
// sequential -> timestamp is one way
133129
files.0[1].assert_is_timestamp();
130+
files.0[2].assert_is_timestamp();
134131
}
135132
Ok(())
136133
}

0 commit comments

Comments
 (0)