aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md5
-rw-r--r--src/apps.rs4
-rw-r--r--src/contrib/admin/migrations/m_0001_initial.rs10
-rw-r--r--src/contrib/admin/migrations/mod.rs3
-rw-r--r--src/contrib/admin/mod.rs3
-rw-r--r--src/db/migration/mod.rs12
-rw-r--r--src/db/mod.rs1
-rw-r--r--tosin-macros/Cargo.toml2
-rw-r--r--tosin-macros/src/lib.rs31
9 files changed, 68 insertions, 3 deletions
diff --git a/README.md b/README.md
index 870d4e4..8834f67 100644
--- a/README.md
+++ b/README.md
@@ -6,6 +6,11 @@ it's like Django, but for Rust.
painfully WIP, not usable.
+## stable rust pls
+
+needs [proc_macro_span](https://github.com/rust-lang/rust/issues/54725) (or at least `SourceFile`) stabilized before it'll work on stable Rust.
+that could probably be worked around with a build script, but i do not want to do that.
+
## name
Django is named after a guitarist that one of the original developers liked, so i've named Tosin similarly.
diff --git a/src/apps.rs b/src/apps.rs
index 26c6124..785ce8c 100644
--- a/src/apps.rs
+++ b/src/apps.rs
@@ -1,3 +1,5 @@
-pub struct AppConfig {
+use crate::db::migration::Migration;
+pub struct AppConfig {
+ pub migrations: &'static [Migration],
}
diff --git a/src/contrib/admin/migrations/m_0001_initial.rs b/src/contrib/admin/migrations/m_0001_initial.rs
new file mode 100644
index 0000000..6fed631
--- /dev/null
+++ b/src/contrib/admin/migrations/m_0001_initial.rs
@@ -0,0 +1,10 @@
+use crate::db::migration::{Migration, DatabaseChange};
+
+pub const MIGRATION: Migration = Migration {
+ id: 1,
+ name: "initial",
+ prereqs: &[],
+ changes: &[
+ DatabaseChange::CreateModel,
+ ],
+};
diff --git a/src/contrib/admin/migrations/mod.rs b/src/contrib/admin/migrations/mod.rs
new file mode 100644
index 0000000..5ecbb82
--- /dev/null
+++ b/src/contrib/admin/migrations/mod.rs
@@ -0,0 +1,3 @@
+use crate::db::migration::{Migration, gather};
+
+gather!();
diff --git a/src/contrib/admin/mod.rs b/src/contrib/admin/mod.rs
index 0f7e880..5098a4a 100644
--- a/src/contrib/admin/mod.rs
+++ b/src/contrib/admin/mod.rs
@@ -1,7 +1,8 @@
use crate::apps::AppConfig;
+mod migrations;
pub mod site;
pub const APP: AppConfig = AppConfig {
-
+ migrations: migrations::migrations,
};
diff --git a/src/db/migration/mod.rs b/src/db/migration/mod.rs
new file mode 100644
index 0000000..d53f46b
--- /dev/null
+++ b/src/db/migration/mod.rs
@@ -0,0 +1,12 @@
+pub use tosin_macros::gather_migrations as gather;
+
+pub struct Migration {
+ pub id: usize,
+ pub name: &'static str,
+ pub prereqs: &'static [(&'static str, usize)],
+ pub changes: &'static [DatabaseChange],
+}
+
+pub enum DatabaseChange {
+ CreateModel,
+}
diff --git a/src/db/mod.rs b/src/db/mod.rs
index b6528f5..05c38df 100644
--- a/src/db/mod.rs
+++ b/src/db/mod.rs
@@ -1,2 +1,3 @@
pub mod backend;
+pub mod migration;
pub mod models;
diff --git a/tosin-macros/Cargo.toml b/tosin-macros/Cargo.toml
index 4a58a20..836461c 100644
--- a/tosin-macros/Cargo.toml
+++ b/tosin-macros/Cargo.toml
@@ -10,5 +10,5 @@ edition = "2018"
proc-macro = true
[dependencies]
-syn = "1.0"
+syn = { version = "1.0", features = ["full", "extra-traits"] }
quote = "1.0"
diff --git a/tosin-macros/src/lib.rs b/tosin-macros/src/lib.rs
index c0651fa..7efbac6 100644
--- a/tosin-macros/src/lib.rs
+++ b/tosin-macros/src/lib.rs
@@ -1,3 +1,5 @@
+#![feature(proc_macro_span)]
+
extern crate proc_macro;
use proc_macro::TokenStream;
@@ -24,3 +26,32 @@ fn impl_model(ast: &syn::DeriveInput) -> TokenStream {
};
gen.into()
}
+
+#[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 migrations: &[Migration] = &[
+ #(#migrations::MIGRATION),*
+ ];
+ };
+
+ gen.into()
+}