Skip to content

Commit d0fbe7f

Browse files
authored
Automatically infer migration type (#2664)
1 parent 487b89a commit d0fbe7f

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

sqlx-cli/README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ sqlx migrate info --source ../relative/migrations
7272

7373
### Reverting Migrations
7474

75-
If you would like to create _reversible_ migrations with corresponding "up" and "down" scripts, you use the `-r` flag when creating new migrations:
75+
If you would like to create _reversible_ migrations with corresponding "up" and "down" scripts, you use the `-r` flag when creating the first migration:
7676

7777
```bash
7878
$ sqlx migrate add -r <name>
@@ -94,14 +94,12 @@ $ sqlx migrate revert
9494
Applied 20211001154420/revert <name>
9595
```
9696

97-
**Note**: attempting to mix "simple" migrations with reversible migrations with result in an error.
97+
**Note**: All the subsequent migrations will be reversible as well.
9898

9999
```bash
100100
$ sqlx migrate add <name1>
101-
Creating migrations/20211001154420_<name>.sql
102-
103-
$ sqlx migrate add -r <name2>
104-
error: cannot mix reversible migrations with simple migrations. All migrations should be reversible or simple migrations
101+
Creating migrations/20211001154420_<name>.up.sql
102+
Creating migrations/20211001154420_<name>.down.sql
105103
```
106104

107105
### Enable building in "offline mode" with `query!()`

sqlx-cli/src/migrate.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,14 @@ pub async fn add(
115115
.unwrap_or(false);
116116

117117
let migrator = Migrator::new(Path::new(migration_source)).await?;
118-
// This checks if all existing migrations are of the same type as the reversible flag passed
119-
for migration in migrator.iter() {
120-
if migration.migration_type.is_reversible() != reversible {
121-
bail!(MigrateError::InvalidMixReversibleAndSimple);
122-
}
123-
}
118+
// Type of newly created migration will be the same as the first one
119+
// or reversible flag if this is the first migration
120+
let migration_type = MigrationType::infer(&migrator, reversible);
124121

125122
let ordering = MigrationOrdering::infer(sequential, timestamp, &migrator);
126123
let file_prefix = ordering.file_prefix();
127124

128-
if reversible {
125+
if migration_type.is_reversible() {
129126
create_file(
130127
migration_source,
131128
&file_prefix,

sqlx-core/src/migrate/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub enum MigrateError {
2424
#[error("migration {0} is newer than the latest applied migration {1}")]
2525
VersionTooNew(i64, i64),
2626

27+
#[deprecated = "migration types are now inferred"]
2728
#[error("cannot mix reversible migrations with simple migrations. All migrations should be reversible or simple migrations")]
2829
InvalidMixReversibleAndSimple,
2930

sqlx-core/src/migrate/migration_type.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use super::Migrator;
2+
13
/// Migration Type represents the type of migration
24
#[derive(Debug, Copy, Clone, PartialEq)]
35
pub enum MigrationType {
@@ -71,4 +73,17 @@ impl MigrationType {
7173
MigrationType::ReversibleDown => "-- Add down migration script here\n",
7274
}
7375
}
76+
77+
pub fn infer(migrator: &Migrator, reversible: bool) -> MigrationType {
78+
match migrator.iter().next() {
79+
Some(first_migration) => first_migration.migration_type,
80+
None => {
81+
if reversible {
82+
MigrationType::ReversibleUp
83+
} else {
84+
MigrationType::Simple
85+
}
86+
}
87+
}
88+
}
7489
}

0 commit comments

Comments
 (0)