mirror of
https://github.com/launchbadge/sqlx
synced 2024-11-14 00:07:05 +00:00
fix migrate marco to take migration type
This commit is contained in:
parent
0921df44c1
commit
ef313f0611
2 changed files with 23 additions and 2 deletions
|
@ -157,7 +157,7 @@ pub async fn revert(migration_source: &str, uri: &str, dry_run: bool) -> anyhow:
|
|||
for migration in migrator.iter().rev() {
|
||||
if !migration.migration_type.is_down_migration() {
|
||||
// Skipping non down migration
|
||||
// This will skip any standard or up migration file
|
||||
// This will skip any simple or up migration file
|
||||
continue;
|
||||
}
|
||||
if migration.version > version {
|
||||
|
|
|
@ -1,12 +1,29 @@
|
|||
use proc_macro2::TokenStream;
|
||||
use quote::{quote, ToTokens, TokenStreamExt};
|
||||
use sha2::{Digest, Sha384};
|
||||
use sqlx_core::migrate::MigrationType;
|
||||
use std::fs;
|
||||
use syn::LitStr;
|
||||
|
||||
pub struct QuotedMigrationType(MigrationType);
|
||||
|
||||
impl ToTokens for QuotedMigrationType {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
let ts = match self.0 {
|
||||
MigrationType::Simple => quote! { sqlx::migrate::MigrationType::Simple },
|
||||
MigrationType::ReversibleUp => quote! { sqlx::migrate::MigrationType::ReversibleUp },
|
||||
MigrationType::ReversibleDown => {
|
||||
quote! { sqlx::migrate::MigrationType::ReversibleDown }
|
||||
}
|
||||
};
|
||||
tokens.append_all(ts.into_iter());
|
||||
}
|
||||
}
|
||||
|
||||
struct QuotedMigration {
|
||||
version: i64,
|
||||
description: String,
|
||||
migration_type: QuotedMigrationType,
|
||||
sql: String,
|
||||
checksum: Vec<u8>,
|
||||
}
|
||||
|
@ -16,6 +33,7 @@ impl ToTokens for QuotedMigration {
|
|||
let QuotedMigration {
|
||||
version,
|
||||
description,
|
||||
migration_type,
|
||||
sql,
|
||||
checksum,
|
||||
} = &self;
|
||||
|
@ -24,6 +42,7 @@ impl ToTokens for QuotedMigration {
|
|||
sqlx::migrate::Migration {
|
||||
version: #version,
|
||||
description: std::borrow::Cow::Borrowed(#description),
|
||||
migration_type: #migration_type,
|
||||
sql: std::borrow::Cow::Borrowed(#sql),
|
||||
checksum: std::borrow::Cow::Borrowed(&[
|
||||
#(#checksum),*
|
||||
|
@ -59,9 +78,10 @@ pub(crate) fn expand_migrator_from_dir(dir: LitStr) -> crate::Result<proc_macro2
|
|||
|
||||
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();
|
||||
|
||||
|
@ -72,6 +92,7 @@ pub(crate) fn expand_migrator_from_dir(dir: LitStr) -> crate::Result<proc_macro2
|
|||
migrations.push(QuotedMigration {
|
||||
version,
|
||||
description,
|
||||
migration_type: QuotedMigrationType(migration_type),
|
||||
sql,
|
||||
checksum,
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue