mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-03-28 06:01:14 +00:00
reversible migrations for cli
- adds a -r flag whihc will create a reversible migration - add revert subcommand, which reverts the last migration - add --dry-run flag to migration run command, which list the migrations that will be applied - updates add migration to check if all migration are of same type, i.e cannot mix and match reversible and simple migrations
This commit is contained in:
@@ -171,4 +171,23 @@ impl Migrate for AnyConnection {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn revert<'e: 'm, 'm>(
|
||||
&'e mut self,
|
||||
migration: &'m Migration,
|
||||
) -> BoxFuture<'m, Result<Duration, MigrateError>> {
|
||||
match &mut self.0 {
|
||||
#[cfg(feature = "postgres")]
|
||||
AnyConnectionKind::Postgres(conn) => conn.revert(migration),
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
AnyConnectionKind::Sqlite(conn) => conn.revert(migration),
|
||||
|
||||
#[cfg(feature = "mysql")]
|
||||
AnyConnectionKind::MySql(conn) => conn.revert(migration),
|
||||
|
||||
#[cfg(feature = "mssql")]
|
||||
AnyConnectionKind::Mssql(conn) => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@ pub enum MigrateError {
|
||||
#[error("migration {0} was previously applied but has been modified")]
|
||||
VersionMismatch(i64),
|
||||
|
||||
#[error("cannot mix reversible migrations with simple migrations. All migrations should be reversible or simple migrations")]
|
||||
InvalidMixReversibleAndSimple,
|
||||
|
||||
// NOTE: this will only happen with a database that does not have transactional DDL (.e.g, MySQL or Oracle)
|
||||
#[error(
|
||||
"migration {0} is partially applied; fix and remove row from `_sqlx_migrations` table"
|
||||
|
||||
@@ -50,4 +50,12 @@ pub trait Migrate {
|
||||
&'e mut self,
|
||||
migration: &'m Migration,
|
||||
) -> BoxFuture<'m, Result<Duration, MigrateError>>;
|
||||
|
||||
// run a revert SQL from migration in a DDL transaction
|
||||
// deletes the row in [_migrations] table with specified migration version on completion (success or failure)
|
||||
// returns the time taking to run the migration SQL
|
||||
fn revert<'e: 'm, 'm>(
|
||||
&'e mut self,
|
||||
migration: &'m Migration,
|
||||
) -> BoxFuture<'m, Result<Duration, MigrateError>>;
|
||||
}
|
||||
|
||||
@@ -2,21 +2,30 @@ use std::borrow::Cow;
|
||||
|
||||
use sha2::{Digest, Sha384};
|
||||
|
||||
use super::MigrationType;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Migration {
|
||||
pub version: i64,
|
||||
pub description: Cow<'static, str>,
|
||||
pub migration_type: MigrationType,
|
||||
pub sql: Cow<'static, str>,
|
||||
pub checksum: Cow<'static, [u8]>,
|
||||
}
|
||||
|
||||
impl Migration {
|
||||
pub fn new(version: i64, description: Cow<'static, str>, sql: Cow<'static, str>) -> Self {
|
||||
pub fn new(
|
||||
version: i64,
|
||||
description: Cow<'static, str>,
|
||||
migration_type: MigrationType,
|
||||
sql: Cow<'static, str>,
|
||||
) -> Self {
|
||||
let checksum = Cow::Owned(Vec::from(Sha384::digest(sql.as_bytes()).as_slice()));
|
||||
|
||||
Migration {
|
||||
version,
|
||||
description,
|
||||
migration_type,
|
||||
sql,
|
||||
checksum,
|
||||
}
|
||||
|
||||
66
sqlx-core/src/migrate/migration_type.rs
Normal file
66
sqlx-core/src/migrate/migration_type.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
/// Migration Type represents the type of migration
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub enum MigrationType {
|
||||
/// Simple migration are single file migrations with no up / down queries
|
||||
Simple,
|
||||
|
||||
/// ReversibleUp migrations represents the add or update part of a reversible migrations
|
||||
/// It is expected the every migration of this type will have a corresponding down file
|
||||
ReversibleUp,
|
||||
|
||||
/// ReversibleDown migrations represents the delete or downgrade part of a reversible migrations
|
||||
/// It is expected the every migration of this type will have a corresponding up file
|
||||
ReversibleDown,
|
||||
}
|
||||
|
||||
impl MigrationType {
|
||||
pub fn from_filename(filename: &str) -> Self {
|
||||
if filename.ends_with(MigrationType::ReversibleUp.suffix()) {
|
||||
MigrationType::ReversibleUp
|
||||
} else if filename.ends_with(MigrationType::ReversibleDown.suffix()) {
|
||||
MigrationType::ReversibleDown
|
||||
} else {
|
||||
MigrationType::Simple
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_reversible(&self) -> bool {
|
||||
match self {
|
||||
MigrationType::Simple => false,
|
||||
MigrationType::ReversibleUp => true,
|
||||
MigrationType::ReversibleDown => true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_down_migration(&self) -> bool {
|
||||
match self {
|
||||
MigrationType::Simple => false,
|
||||
MigrationType::ReversibleUp => false,
|
||||
MigrationType::ReversibleDown => true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn label(&self) -> &'static str {
|
||||
match self {
|
||||
MigrationType::Simple => "migrate",
|
||||
MigrationType::ReversibleUp => "migrate",
|
||||
MigrationType::ReversibleDown => "revert",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn suffix(&self) -> &'static str {
|
||||
match self {
|
||||
MigrationType::Simple => ".sql",
|
||||
MigrationType::ReversibleUp => ".up.sql",
|
||||
MigrationType::ReversibleDown => ".down.sql",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file_content(&self) -> &'static str {
|
||||
match self {
|
||||
MigrationType::Simple => "-- Add migration script here\n",
|
||||
MigrationType::ReversibleUp => "-- Add up migration script here\n",
|
||||
MigrationType::ReversibleDown => "-- Add down migration script here\n",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
mod error;
|
||||
mod migrate;
|
||||
mod migration;
|
||||
mod migration_type;
|
||||
mod migrator;
|
||||
mod source;
|
||||
|
||||
pub use error::MigrateError;
|
||||
pub use migrate::{Migrate, MigrateDatabase};
|
||||
pub use migration::Migration;
|
||||
pub use migration_type::MigrationType;
|
||||
pub use migrator::Migrator;
|
||||
pub use source::MigrationSource;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::error::BoxDynError;
|
||||
use crate::migrate::Migration;
|
||||
use crate::migrate::{Migration, MigrationType};
|
||||
use futures_core::future::BoxFuture;
|
||||
use futures_util::TryStreamExt;
|
||||
use sqlx_rt::fs;
|
||||
@@ -35,9 +35,10 @@ impl<'s> MigrationSource<'s> for &'s Path {
|
||||
|
||||
let version: i64 = parts[0].parse()?;
|
||||
|
||||
let migration_type = MigrationType::from_filename(parts[1]);
|
||||
// remove the `.sql` and replace `_` with ` `
|
||||
let description = parts[1]
|
||||
.trim_end_matches(".sql")
|
||||
.trim_end_matches(migration_type.suffix())
|
||||
.replace('_', " ")
|
||||
.to_owned();
|
||||
|
||||
@@ -46,6 +47,7 @@ impl<'s> MigrationSource<'s> for &'s Path {
|
||||
migrations.push(Migration::new(
|
||||
version,
|
||||
Cow::Owned(description),
|
||||
migration_type,
|
||||
Cow::Owned(sql),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -201,6 +201,27 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (
|
||||
Ok(elapsed)
|
||||
})
|
||||
}
|
||||
|
||||
fn revert<'e: 'm, 'm>(
|
||||
&'e mut self,
|
||||
migration: &'m Migration,
|
||||
) -> BoxFuture<'m, Result<Duration, MigrateError>> {
|
||||
Box::pin(async move {
|
||||
let start = Instant::now();
|
||||
|
||||
self.execute(&*migration.sql).await?;
|
||||
|
||||
let elapsed = start.elapsed();
|
||||
|
||||
// language=SQL
|
||||
let _ = query(r#"DELETE FROM _sqlx_migrations WHERE version = ?"#)
|
||||
.bind(migration.version)
|
||||
.execute(self)
|
||||
.await?;
|
||||
|
||||
Ok(elapsed)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async fn current_database(conn: &mut MySqlConnection) -> Result<String, MigrateError> {
|
||||
|
||||
@@ -211,6 +211,30 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (
|
||||
Ok(elapsed)
|
||||
})
|
||||
}
|
||||
|
||||
fn revert<'e: 'm, 'm>(
|
||||
&'e mut self,
|
||||
migration: &'m Migration,
|
||||
) -> BoxFuture<'m, Result<Duration, MigrateError>> {
|
||||
Box::pin(async move {
|
||||
let mut tx = self.begin().await?;
|
||||
let start = Instant::now();
|
||||
|
||||
let _ = tx.execute(&*migration.sql).await?;
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
let elapsed = start.elapsed();
|
||||
|
||||
// language=SQL
|
||||
let _ = query(r#"DELETE FROM _sqlx_migrations WHERE version = $1"#)
|
||||
.bind(migration.version)
|
||||
.execute(self)
|
||||
.await?;
|
||||
|
||||
Ok(elapsed)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async fn current_database(conn: &mut PgConnection) -> Result<String, MigrateError> {
|
||||
|
||||
@@ -150,4 +150,28 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (
|
||||
Ok(elapsed)
|
||||
})
|
||||
}
|
||||
|
||||
fn revert<'e: 'm, 'm>(
|
||||
&'e mut self,
|
||||
migration: &'m Migration,
|
||||
) -> BoxFuture<'m, Result<Duration, MigrateError>> {
|
||||
Box::pin(async move {
|
||||
let mut tx = self.begin().await?;
|
||||
let start = Instant::now();
|
||||
|
||||
let _ = tx.execute(&*migration.sql).await?;
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
let elapsed = start.elapsed();
|
||||
|
||||
// language=SQL
|
||||
let _ = query(r#"DELETE FROM _sqlx_migrations WHERE version = ?1"#)
|
||||
.bind(migration.version)
|
||||
.execute(self)
|
||||
.await?;
|
||||
|
||||
Ok(elapsed)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user