diff options
| author | Melody Horn <melody@boringcactus.com> | 2021-06-30 19:55:47 -0600 | 
|---|---|---|
| committer | Melody Horn <melody@boringcactus.com> | 2021-06-30 19:55:47 -0600 | 
| commit | 36b8d3d0a8c97726d4413ae005b36e6288119ed4 (patch) | |
| tree | d52e5670cbf38e1fafdff2986543cae8495461ef | |
| parent | 85d53a3358819d15788e1196d110fb6fd7199c67 (diff) | |
| download | tosin-36b8d3d0a8c97726d4413ae005b36e6288119ed4.tar.gz tosin-36b8d3d0a8c97726d4413ae005b36e6288119ed4.zip  | |
use the table name matching what actually exists
| -rw-r--r-- | src/db/migration/change.rs | 1 | ||||
| -rw-r--r-- | tosin-macros/src/lib.rs | 29 | 
2 files changed, 30 insertions, 0 deletions
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::<Vec<_>>() +                .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,)*              }  |