fix migrate marco to take migration type

This commit is contained in:
sid 2020-08-24 23:59:38 +05:30 committed by Ryan Leckey
parent 0921df44c1
commit ef313f0611
No known key found for this signature in database
GPG key ID: F8AA68C235AB08C9
2 changed files with 23 additions and 2 deletions

View file

@ -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 {

View file

@ -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,
})