diff options
-rw-r--r-- | examples/tutorial01/polls/mod.rs | 1 | ||||
-rw-r--r-- | examples/tutorial02/polls/migrations/m_0001_auto.rs | 34 | ||||
-rw-r--r-- | examples/tutorial02/polls/mod.rs | 1 | ||||
-rw-r--r-- | src/apps.rs | 1 | ||||
-rw-r--r-- | src/cli/make_migrations.rs | 13 | ||||
-rw-r--r-- | src/contrib/admin/mod.rs | 1 |
6 files changed, 44 insertions, 7 deletions
diff --git a/examples/tutorial01/polls/mod.rs b/examples/tutorial01/polls/mod.rs index 7f02b17..44db04b 100644 --- a/examples/tutorial01/polls/mod.rs +++ b/examples/tutorial01/polls/mod.rs @@ -10,6 +10,7 @@ pub use urls::urls; #[allow(dead_code)] pub const APP: AppConfig = AppConfig { name: module_path!(), + mod_rs_path: file!(), models: models::ALL, migrations: migrations::ALL, }; diff --git a/examples/tutorial02/polls/migrations/m_0001_auto.rs b/examples/tutorial02/polls/migrations/m_0001_auto.rs new file mode 100644 index 0000000..7fa7de0 --- /dev/null +++ b/examples/tutorial02/polls/migrations/m_0001_auto.rs @@ -0,0 +1,34 @@ +use tosin::db::migration::Migration; + +pub const MIGRATION: Migration = Migration { + id: 1usize, + name: "auto", + prereqs: &[], + changes: &[ + DatabaseChange::CreateModel { + name: "Question", + fields: &[ + Field::IntField { name: "id" }, + Field::CharField { + name: "question_text", + max_length: Some(200usize), + }, + Field::DateTimeField { name: "pub_date" }, + ], + options: &[], + }, + DatabaseChange::CreateModel { + name: "Choice", + fields: &[ + Field::IntField { name: "id" }, + Field::IntField { name: "question" }, + Field::CharField { + name: "choice_text", + max_length: Some(200usize), + }, + Field::IntField { name: "votes" }, + ], + options: &[], + }, + ], +}; diff --git a/examples/tutorial02/polls/mod.rs b/examples/tutorial02/polls/mod.rs index 2c50e84..0e81062 100644 --- a/examples/tutorial02/polls/mod.rs +++ b/examples/tutorial02/polls/mod.rs @@ -9,6 +9,7 @@ pub use urls::urls; pub const APP: AppConfig = AppConfig { name: module_path!(), + mod_rs_path: file!(), models: models::ALL, migrations: migrations::ALL, }; diff --git a/src/apps.rs b/src/apps.rs index 8c16136..c2fe099 100644 --- a/src/apps.rs +++ b/src/apps.rs @@ -3,6 +3,7 @@ use crate::db::models::ModelMeta; pub struct AppConfig { pub name: &'static str, + pub mod_rs_path: &'static str, pub models: &'static [ModelMeta], pub migrations: &'static [Migration], } diff --git a/src/cli/make_migrations.rs b/src/cli/make_migrations.rs index e0653b6..d4ddcbf 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::PathBuf; +use std::path::{Path, PathBuf}; use quote::{quote, ToTokens}; use structopt::StructOpt; @@ -131,12 +131,11 @@ impl MakeMigrations { }; let file_name = format!("m_{:04}_{}.rs", next_id, name); let file_text = file.into_token_stream().to_string(); - let app_folder = app.name.split("::").skip(1).collect::<PathBuf>(); - // TODO don't explode if running in a weird place - let file_path = PathBuf::from("src") - .join(app_folder) - .join("migrations") - .join(file_name); + let app_mod_rs = Path::new(app.mod_rs_path); + 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); println!("Saving migration to {}...", file_path.display()); fs::write(file_path, file_text).unwrap(); // TODO cargo fmt diff --git a/src/contrib/admin/mod.rs b/src/contrib/admin/mod.rs index 7b442b2..300eeac 100644 --- a/src/contrib/admin/mod.rs +++ b/src/contrib/admin/mod.rs @@ -6,6 +6,7 @@ pub mod site; pub const APP: AppConfig = AppConfig { name: module_path!(), + mod_rs_path: file!(), models: models::ALL, migrations: migrations::ALL, }; |