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() { for migration in migrator.iter().rev() {
if !migration.migration_type.is_down_migration() { if !migration.migration_type.is_down_migration() {
// Skipping non 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; continue;
} }
if migration.version > version { if migration.version > version {

View File

@ -1,12 +1,29 @@
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::{quote, ToTokens, TokenStreamExt}; use quote::{quote, ToTokens, TokenStreamExt};
use sha2::{Digest, Sha384}; use sha2::{Digest, Sha384};
use sqlx_core::migrate::MigrationType;
use std::fs; use std::fs;
use syn::LitStr; 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 { struct QuotedMigration {
version: i64, version: i64,
description: String, description: String,
migration_type: QuotedMigrationType,
sql: String, sql: String,
checksum: Vec<u8>, checksum: Vec<u8>,
} }
@ -16,6 +33,7 @@ impl ToTokens for QuotedMigration {
let QuotedMigration { let QuotedMigration {
version, version,
description, description,
migration_type,
sql, sql,
checksum, checksum,
} = &self; } = &self;
@ -24,6 +42,7 @@ impl ToTokens for QuotedMigration {
sqlx::migrate::Migration { sqlx::migrate::Migration {
version: #version, version: #version,
description: std::borrow::Cow::Borrowed(#description), description: std::borrow::Cow::Borrowed(#description),
migration_type: #migration_type,
sql: std::borrow::Cow::Borrowed(#sql), sql: std::borrow::Cow::Borrowed(#sql),
checksum: std::borrow::Cow::Borrowed(&[ checksum: std::borrow::Cow::Borrowed(&[
#(#checksum),* #(#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 version: i64 = parts[0].parse()?;
let migration_type = MigrationType::from_filename(parts[1]);
// remove the `.sql` and replace `_` with ` ` // remove the `.sql` and replace `_` with ` `
let description = parts[1] let description = parts[1]
.trim_end_matches(".sql") .trim_end_matches(migration_type.suffix())
.replace('_', " ") .replace('_', " ")
.to_owned(); .to_owned();
@ -72,6 +92,7 @@ pub(crate) fn expand_migrator_from_dir(dir: LitStr) -> crate::Result<proc_macro2
migrations.push(QuotedMigration { migrations.push(QuotedMigration {
version, version,
description, description,
migration_type: QuotedMigrationType(migration_type),
sql, sql,
checksum, checksum,
}) })