Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions cot-cli/src/migration_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,16 +601,29 @@ impl MigrationGenerator {

#[must_use]
fn model_to_migration_model(model: &ModelInSource) -> TokenStream {
// keep the table name if it has been set explicitly, as it would
// otherwise be derived from the (prefixed) name of the migration model
let table_name = model
.model_item
.attrs
.iter()
.find(|attr| is_model_attr(attr))
.and_then(|attr| ModelArgs::from_meta(&attr.meta).ok())
.and_then(|args| args.table_name);

let mut model_source = model.model_item.clone();
model_source.vis = syn::Visibility::Inherited;
model_source.ident = format_ident!("_{}", model_source.ident);
model_source.attrs.clear();
model_source
.attrs
.push(syn::parse_quote! {#[derive(::core::fmt::Debug)]});
model_source
.attrs
.push(syn::parse_quote! {#[::cot::db::model(model_type = "migration")]});
model_source.attrs.push(match table_name {
Some(table_name) => syn::parse_quote! {
#[::cot::db::model(model_type = "migration", table_name = #table_name)]
},
None => syn::parse_quote! {#[::cot::db::model(model_type = "migration")]},
});
quote! {
#model_source
}
Expand Down
68 changes: 68 additions & 0 deletions cot-cli/tests/migration_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,74 @@ fn create_model_state_test() {
assert!(field.foreign_key.clone().is_some());
}

/// Test that the migration generator will generate the correct create model
/// migration if the model has a custom table name, and will also correctly
/// generate the add field migration
#[test]
fn custom_table_name_test() {
let generator = test_generator();
let src = include_str!("migration_generator/custom_table_name/step_1.rs");
let source_files = vec![SourceFile::parse(PathBuf::from("main.rs"), src).unwrap()];

let migration = generator
.generate_migrations_as_generated_from_files(source_files.clone())
.unwrap()
.unwrap();

let migration_file = generator
.generate_migrations_as_source_from_files(source_files)
.unwrap()
.unwrap();

assert_eq!(migration.migration_name, "m_0001_initial");
assert_eq!(migration.dependencies, []);

let (table_name, fields) = unwrap_create_model(&migration.operations[0]);
assert_eq!(table_name, "cot__myamazing_table_name");
assert_eq!(fields.len(), 2);

let field = &fields[0];
assert_eq!(field.column_name, "id");
assert!(field.primary_key);
assert!(field.auto_value);
assert!(field.foreign_key.clone().is_none());

let field = &fields[1];
assert_eq!(field.column_name, "field_1");
assert!(!field.primary_key);
assert!(!field.auto_value);
assert!(field.foreign_key.clone().is_none());

let src = include_str!("migration_generator/custom_table_name/step_2.rs");
let source_files = vec![
SourceFile::parse(PathBuf::from("main.rs"), src).unwrap(),
SourceFile::parse(PathBuf::from(&migration_file.name), &migration_file.content).unwrap(),
];

let migration = generator
.generate_migrations_as_generated_from_files(source_files)
.unwrap()
.unwrap();

assert_eq!(migration.dependencies.len(), 2);
assert!(migration.dependencies.contains(&DynDependency::Migration {
app: "cot".to_string(),
migration: "m_0001_initial".to_string()
}));
assert!(migration.dependencies.contains(&DynDependency::Model {
model_type: parse_quote!(crate::CustomModel),
}));

assert_eq!(migration.operations.len(), 1);

let (table_name, field) = unwrap_add_field(&migration.operations[0]);
assert_eq!(table_name, "cot__myamazing_table_name");
assert_eq!(field.column_name, "field_2");
assert!(!field.primary_key);
assert!(!field.auto_value);
assert!(field.foreign_key.clone().is_none());
}

#[test]
fn create_models_foreign_key() {
let generator = test_generator();
Expand Down
9 changes: 9 additions & 0 deletions cot-cli/tests/migration_generator/custom_table_name/step_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use cot::db::{Auto, LimitedString, model};

#[derive(Debug)]
#[model(table_name = "myamazing_table_name")]
struct CustomModel {
#[model(primary_key)]
id: Auto<i32>,
field_1: String,
}
10 changes: 10 additions & 0 deletions cot-cli/tests/migration_generator/custom_table_name/step_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use cot::db::{Auto, LimitedString, model};

#[derive(Debug)]
#[model(table_name = "myamazing_table_name")]
struct CustomModel {
#[model(primary_key)]
id: Auto<i32>,
field_1: String,
field_2: i32,
}
Loading