diff options
-rw-r--r-- | examples/tutorial02/polls/migrations/m_0001_auto.rs | 4 | ||||
-rw-r--r-- | examples/tutorial02/polls/migrations/mod.rs | 6 | ||||
-rw-r--r-- | src/cli/make_migrations.rs | 32 | ||||
-rw-r--r-- | src/contrib/admin/migrations/mod.rs | 6 | ||||
-rw-r--r-- | src/db/migration/mod.rs | 3 | ||||
-rw-r--r-- | tosin-macros/src/lib.rs | 30 |
6 files changed, 39 insertions, 42 deletions
diff --git a/examples/tutorial02/polls/migrations/m_0001_auto.rs b/examples/tutorial02/polls/migrations/m_0001_auto.rs index 7fa7de0..2eef04a 100644 --- a/examples/tutorial02/polls/migrations/m_0001_auto.rs +++ b/examples/tutorial02/polls/migrations/m_0001_auto.rs @@ -1,5 +1,5 @@ -use tosin::db::migration::Migration; - +use tosin::db::migration::{DatabaseChange, Migration}; +use tosin::db::models::Field; pub const MIGRATION: Migration = Migration { id: 1usize, name: "auto", diff --git a/examples/tutorial02/polls/migrations/mod.rs b/examples/tutorial02/polls/migrations/mod.rs index 56fe76d..b254c33 100644 --- a/examples/tutorial02/polls/migrations/mod.rs +++ b/examples/tutorial02/polls/migrations/mod.rs @@ -1,3 +1,3 @@ -use tosin::db::migration::{gather, Migration}; - -gather!(); +use tosin::db::migration::Migration; +mod m_0001_auto; +pub const ALL: &[Migration] = &[m_0001_auto::MIGRATION]; diff --git a/src/cli/make_migrations.rs b/src/cli/make_migrations.rs index d4ddcbf..98aebeb 100644 --- a/src/cli/make_migrations.rs +++ b/src/cli/make_migrations.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; use std::fs; -use std::path::{Path, PathBuf}; +use std::path::Path; use quote::{quote, ToTokens}; use structopt::StructOpt; @@ -125,7 +125,8 @@ impl MakeMigrations { } }; let file = quote! { - use tosin::db::migration::Migration; + use tosin::db::migration::{DatabaseChange, Migration}; + use tosin::db::models::Field; pub const MIGRATION: Migration = #migration; }; @@ -135,9 +136,34 @@ impl MakeMigrations { let app_folder = app_mod_rs .parent() .expect("app mod.rs lives at filesystem root???"); - let file_path = app_folder.join("migrations").join(file_name); + let migrations_dir = app_folder.join("migrations"); + let file_path = migrations_dir.join(file_name); println!("Saving migration to {}...", file_path.display()); fs::write(file_path, file_text).unwrap(); + + // update mod.rs bc the fancy gather!() shit doesn't actually invalidate partial compilation + let migrations: Vec<syn::Ident> = migrations_dir + .read_dir() + .unwrap() + .map(Result::unwrap) + .map(|x| x.path().file_stem().unwrap().to_string_lossy().into_owned()) + .filter(|x| x != "mod") + .map(|x| syn::parse_str(&x).unwrap()) + .collect(); + + let mod_file = quote! { + use tosin::db::migration::Migration; + + #( mod #migrations; )* + + pub const ALL: &[Migration] = &[ + #(#migrations::MIGRATION),* + ]; + }; + let mod_file_path = migrations_dir.join("mod.rs"); + let mod_file_text = mod_file.into_token_stream().to_string(); + fs::write(mod_file_path, mod_file_text).unwrap(); + // TODO cargo fmt } } diff --git a/src/contrib/admin/migrations/mod.rs b/src/contrib/admin/migrations/mod.rs index 01e246b..b43fc44 100644 --- a/src/contrib/admin/migrations/mod.rs +++ b/src/contrib/admin/migrations/mod.rs @@ -1,3 +1,5 @@ -use crate::db::migration::{gather, Migration}; +use crate::db::migration::Migration; -gather!(); +mod m_0001_initial; + +pub const ALL: &[Migration] = &[m_0001_initial::MIGRATION]; diff --git a/src/db/migration/mod.rs b/src/db/migration/mod.rs index e6b3266..61cdcf9 100644 --- a/src/db/migration/mod.rs +++ b/src/db/migration/mod.rs @@ -1,5 +1,3 @@ -pub use tosin_macros::gather_migrations as gather; - use diesel::{result::Error as DieselError, Connection as _}; use crate::db::backend::Connection; @@ -20,6 +18,7 @@ impl Migration { // TODO prevent double-applying connection .transaction::<_, DieselError, _>(|| { + println!("{}:\tapplying {} (id {})", app_name, self.name, self.id); for change in self.changes { change.apply(app_name, connection); } diff --git a/tosin-macros/src/lib.rs b/tosin-macros/src/lib.rs index 5773075..f9d8fc0 100644 --- a/tosin-macros/src/lib.rs +++ b/tosin-macros/src/lib.rs @@ -295,36 +295,6 @@ fn impl_model(ast: &syn::DeriveInput) -> TokenStream { } #[proc_macro] -pub fn gather_migrations(_input: TokenStream) -> TokenStream { - let call_site = proc_macro::Span::call_site(); - let call_site_file = call_site.source_file(); - let call_site_path = call_site_file.path(); - if !call_site_file.is_real() { - panic!("call site does not have a real path"); - } - - let migrations_dir = call_site_path.parent().unwrap(); - let migrations: Vec<syn::Ident> = migrations_dir - .read_dir() - .unwrap() - .map(Result::unwrap) - .map(|x| x.path().file_stem().unwrap().to_string_lossy().into_owned()) - .filter(|x| x != "mod") - .map(|x| syn::parse_str(&x).unwrap()) - .collect(); - - let gen = quote! { - #( mod #migrations; )* - - pub const ALL: &[Migration] = &[ - #(#migrations::MIGRATION),* - ]; - }; - - gen.into() -} - -#[proc_macro] pub fn gather_models(_input: TokenStream) -> TokenStream { let call_site = proc_macro::Span::call_site(); let call_site_file = call_site.source_file(); |