feat!: Add AlterField Operation to Migration Generator#368
feat!: Add AlterField Operation to Migration Generator#368kumarUjjawal wants to merge 8 commits into
AlterField Operation to Migration Generator#368Conversation
Codecov Report❌ Patch coverage is
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
|
@seqre any feedback for me? |
seqre
left a comment
There was a problem hiding this comment.
This operation has many edge cases, and it should be thoroughly tested. Can you please add more tests first?
Think about possible cases, such as: name change (this one can be skipped for now, it's tricky), type change, attributes change, etc. All those should be either handled or visibly err when a given case is unhandled or ambiguous.
It seems that cot-cli/src/migration_generator.rs does not have a test for alter model, would you be so kind and add that too?
|
Sure, Thank you! |
3dac2e1 to
76b9835
Compare
|
@seqre I added more tests can you check if they are what you are expecting and any feedbacks next steps. Thanks! |
1df5eb5 to
c97bc84
Compare
|
@m4tx the error doesn't seem to come from my PR, can you take a look? |
seqre
left a comment
There was a problem hiding this comment.
Thank you for your patience. I fixed the PR so it compiles and the tests pass! I left two comments and I'll try to test it myself tonight.
3f80674 to
3d80f57
Compare
seqre
left a comment
There was a problem hiding this comment.
Hey @kumarUjjawal, it seems you force-pushed your old changes which broke tests and removed the custom operations added recently to master. I've fixed those for you, please remember to git pull before doing any changes!
Regarding the PR, we're getting closer with every iteration! This feature is really important, but it's slightly complicated, that's why I'm pushing for it to be well tested. I appreciate your efforts!
| #[expect(unreachable_code)] | ||
| print_status_msg( | ||
| StatusType::Modified, | ||
| &format!( | ||
| "Field '{}' from Model '{}'", | ||
| &migration_field.name, migration_model.model.name | ||
| &migration_field.name, app_model.model.name | ||
| ), | ||
| ); |
| OperationInner::AlterField { | ||
| table_name, | ||
| old_field: _, | ||
| new_field, | ||
| } => { | ||
| let query = sea_query::Table::alter() | ||
| .table(*table_name) | ||
| .modify_column(new_field.as_column_def(database)) | ||
| .to_owned(); | ||
| database.execute_schema(query).await?; | ||
| } |
There was a problem hiding this comment.
Could you please write some tests that cover this part?
| OperationInner::AlterField { | ||
| table_name, | ||
| old_field, | ||
| new_field: _, | ||
| } => { | ||
| // To reverse an alteration, set the column back to the old definition | ||
| let query = sea_query::Table::alter() | ||
| .table(*table_name) | ||
| .modify_column(old_field.as_column_def(database)) | ||
| .to_owned(); | ||
| database.execute_schema(query).await?; | ||
| } |
There was a problem hiding this comment.
Could you please write some tests that cover this part?
| DynOperation::AlterField { | ||
| new_field, | ||
| model_ty, | ||
| .. | ||
| } => { | ||
| let mut ops = vec![(i, model_ty.clone())]; | ||
| // Only depend on the new foreign key, not the old one | ||
| if let Some(to_type) = foreign_key_for_field(new_field) { | ||
| ops.push((i, to_type)); | ||
| } | ||
| ops | ||
| } |
There was a problem hiding this comment.
Could you please write some tests that cover this part?
This one interests me, from what I've read this would only apply when changing constraint on the foreign key, but that's a 3-operation situation (remove constraint, modify, add constraint), which I'm not sure would be supported in our current setup.
| RemoveModelBuilder::new() | ||
| } | ||
|
|
||
| // TODO: docs |
There was a problem hiding this comment.
Could you please write the docs in similar fashion as other items above?
| } | ||
| } | ||
|
|
||
| // TODO: docs |
There was a problem hiding this comment.
Could you please write the docs in similar fashion as other items above?
AlterField Operation to Migration GeneratorAlterField Operation to Migration Generator
|
Sorry about the git issues, I had a dirty tree and I messed up merge. Thanks for cleaning this. |
|
Hey @kumarUjjawal, how's the work going on this PR? Do you need any help from us? |
|
Hi @seqre Sorry for the delay, I got busy with another project, doesn't seem like I can give time for the next few weeks Can you pick this up from here? |
This reverts commit 3d80f57.
- Restore the "Modified" status message after constructing the AlterField operation in make_alter_field_operation, mirroring the Adding/Added pattern used by make_add_field_operation. - Add rustdoc and examples to Operation::alter_field() and AlterFieldBuilder (and its methods), matching the style of remove_field/remove_model. Examples are marked no_run because SQLite does not support ALTER TABLE ... MODIFY COLUMN. - Add a unit test (test_operation_alter_field) and PostgreSQL/MySQL database tests for AlterField forwards/backwards. SQLite is skipped because sea_query's SQLite backend panics on modify_column; the postgres/mysql tests follow the pattern used by other ignored postgres/mysql tests. - Add unit tests covering the new AlterField branch in get_ops_adding_foreign_keys: one where the new field gains a foreign key and one where the new field has no foreign key.
Add
AlterFieldOperation to Migration GeneratorOverview
This PR introduces support for the
AlterFieldoperation in the migration generator.Related Issues
#205
Reviewer Notes