diff options
Diffstat (limited to 'src')
-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 |
3 files changed, 34 insertions, 7 deletions
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); } |