From 36b8d3d0a8c97726d4413ae005b36e6288119ed4 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Wed, 30 Jun 2021 19:55:47 -0600 Subject: use the table name matching what actually exists --- src/db/migration/change.rs | 1 + tosin-macros/src/lib.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/db/migration/change.rs b/src/db/migration/change.rs index a07ef24..758e23a 100644 --- a/src/db/migration/change.rs +++ b/src/db/migration/change.rs @@ -37,6 +37,7 @@ impl DatabaseChange { Field::DateTimeField { name } => { (*name, types::text()) // TODO do smart things on non-sqlite } + Field::IntField { name: "id" } => ("id", types::primary()), Field::IntField { name } => (*name, types::integer()), }) .collect(); diff --git a/tosin-macros/src/lib.rs b/tosin-macros/src/lib.rs index f9d8fc0..9c1f650 100644 --- a/tosin-macros/src/lib.rs +++ b/tosin-macros/src/lib.rs @@ -148,6 +148,34 @@ fn impl_model(ast: &syn::DeriveInput) -> TokenStream { } else { panic!("not on a struct"); }; + let app_name = { + // ugly hack because span::module_path() doesn't exist + 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 app_dir = if call_site_path.ends_with("models.rs") { + call_site_path.parent().unwrap() + } else { + todo!("not in models.rs what is this") + }; + + // oh this is so fucking disgusting + if app_dir.starts_with("examples") { + app_dir + .components() + .skip(1) // drop the "examples/", keep the example name + .map(|c| c.as_os_str().to_string_lossy()) + .collect::>() + .join("::") + } else { + todo!("what's a {}", call_site_path.display()) + } + }; + let real_table_name = format!("{}-{}", app_name, lowercase_name); let real_db_types: Vec<_> = ast_data .fields .iter() @@ -277,6 +305,7 @@ fn impl_model(ast: &syn::DeriveInput) -> TokenStream { // this means users need #[macro_use] extern crate diesel; but fuck doing it ourselves table! { + #[sql_name = #real_table_name] #lowercase_name { #(#diesel_columns,)* } -- cgit v1.2.3