diff options
Diffstat (limited to 'src/cli')
| -rw-r--r-- | src/cli/make_migrations.rs | 32 | 
1 files changed, 29 insertions, 3 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          }      }  |