mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-03 15:55:45 +00:00
Automatically infer migration type (#2664)
This commit is contained in:
parent
487b89a4b6
commit
d0fbe7feff
@ -72,7 +72,7 @@ sqlx migrate info --source ../relative/migrations
|
|||||||
|
|
||||||
### Reverting Migrations
|
### Reverting Migrations
|
||||||
|
|
||||||
If you would like to create _reversible_ migrations with corresponding "up" and "down" scripts, you use the `-r` flag when creating new migrations:
|
If you would like to create _reversible_ migrations with corresponding "up" and "down" scripts, you use the `-r` flag when creating the first migration:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ sqlx migrate add -r <name>
|
$ sqlx migrate add -r <name>
|
||||||
@ -94,14 +94,12 @@ $ sqlx migrate revert
|
|||||||
Applied 20211001154420/revert <name>
|
Applied 20211001154420/revert <name>
|
||||||
```
|
```
|
||||||
|
|
||||||
**Note**: attempting to mix "simple" migrations with reversible migrations with result in an error.
|
**Note**: All the subsequent migrations will be reversible as well.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ sqlx migrate add <name1>
|
$ sqlx migrate add <name1>
|
||||||
Creating migrations/20211001154420_<name>.sql
|
Creating migrations/20211001154420_<name>.up.sql
|
||||||
|
Creating migrations/20211001154420_<name>.down.sql
|
||||||
$ sqlx migrate add -r <name2>
|
|
||||||
error: cannot mix reversible migrations with simple migrations. All migrations should be reversible or simple migrations
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Enable building in "offline mode" with `query!()`
|
### Enable building in "offline mode" with `query!()`
|
||||||
|
@ -115,17 +115,14 @@ pub async fn add(
|
|||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
|
|
||||||
let migrator = Migrator::new(Path::new(migration_source)).await?;
|
let migrator = Migrator::new(Path::new(migration_source)).await?;
|
||||||
// This checks if all existing migrations are of the same type as the reversible flag passed
|
// Type of newly created migration will be the same as the first one
|
||||||
for migration in migrator.iter() {
|
// or reversible flag if this is the first migration
|
||||||
if migration.migration_type.is_reversible() != reversible {
|
let migration_type = MigrationType::infer(&migrator, reversible);
|
||||||
bail!(MigrateError::InvalidMixReversibleAndSimple);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let ordering = MigrationOrdering::infer(sequential, timestamp, &migrator);
|
let ordering = MigrationOrdering::infer(sequential, timestamp, &migrator);
|
||||||
let file_prefix = ordering.file_prefix();
|
let file_prefix = ordering.file_prefix();
|
||||||
|
|
||||||
if reversible {
|
if migration_type.is_reversible() {
|
||||||
create_file(
|
create_file(
|
||||||
migration_source,
|
migration_source,
|
||||||
&file_prefix,
|
&file_prefix,
|
||||||
|
@ -24,6 +24,7 @@ pub enum MigrateError {
|
|||||||
#[error("migration {0} is newer than the latest applied migration {1}")]
|
#[error("migration {0} is newer than the latest applied migration {1}")]
|
||||||
VersionTooNew(i64, i64),
|
VersionTooNew(i64, i64),
|
||||||
|
|
||||||
|
#[deprecated = "migration types are now inferred"]
|
||||||
#[error("cannot mix reversible migrations with simple migrations. All migrations should be reversible or simple migrations")]
|
#[error("cannot mix reversible migrations with simple migrations. All migrations should be reversible or simple migrations")]
|
||||||
InvalidMixReversibleAndSimple,
|
InvalidMixReversibleAndSimple,
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
use super::Migrator;
|
||||||
|
|
||||||
/// Migration Type represents the type of migration
|
/// Migration Type represents the type of migration
|
||||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
pub enum MigrationType {
|
pub enum MigrationType {
|
||||||
@ -71,4 +73,17 @@ impl MigrationType {
|
|||||||
MigrationType::ReversibleDown => "-- Add down migration script here\n",
|
MigrationType::ReversibleDown => "-- Add down migration script here\n",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn infer(migrator: &Migrator, reversible: bool) -> MigrationType {
|
||||||
|
match migrator.iter().next() {
|
||||||
|
Some(first_migration) => first_migration.migration_type,
|
||||||
|
None => {
|
||||||
|
if reversible {
|
||||||
|
MigrationType::ReversibleUp
|
||||||
|
} else {
|
||||||
|
MigrationType::Simple
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user