Allow `sea-orm-cli generate entity` to preserve custom code
Hi, I'd like to suggest a feature that would make entity auto-generation more user-friendly.
Motivation
Right now, entity generation via CLI will overwrite any custom code (enums, #[derive()], impl Related {}, for example) previously written for an Entity Model. This is especially problematic when I create a new table + entity that has a relation with an existing entity in whose file I've written custom code.
For a simple example, suppose we create an application that has the User and UserRegistration entities:
// entities/user.rs
#[sea_orm(table_name = "user")]
pub struct Model {
#[serde(skip_deserializing)] // <-- this gets lost upon deserialization
#[sea_orm(primary_key)]
pub id: i64,
}
// entities/user_registration.rs
#[sea_orm(table_name = "user_registration")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
pub id: i64,
pub user_id: i64,
}
If I later add a Team that belongs_to a User and I generate entities via sea-orm-cli generate entity, the user.rs file will now describe the new relationship between User and Team, but I lose my custom #[serde(skip_deserializing)] code.
Proposed Solutions
The "easier" solution
The "easy" solution would be to allow specific comments to denote which custom code should be preserved in the existing entity file when running sea-orm-cli generate entity. Using the User model in the example above:
// entities/user.rs
#[sea_orm(table_name = "user")]
pub struct Model {
// sea-orm-keep-start
#[serde(skip_deserializing)] // <-- this line is now preserved
// sea-orm-keep-end
#[sea_orm(primary_key)]
pub id: i64,
}
This would allow the line with #[serde(skip_deserializing)] to be preserved between migrations. The hard part about this is figuring out the correct diffing algorithm, as it would be naive to preserve custom code based on line numbers (because what if the code that's generated had now shifted down a few lines?).
The harder solution
The solution that's harder to implement would probably involve using a diffing algorithm to determine what is custom vs auto-generated, and injecting the custom code from the existing file into the newly auto-generated code.
Hey @kaibyao, welcome!! Thanks for the feature request with a concrete proposal!
I assumed your code is tracked by a Git? Then, you can recover the lines you wish to keep while checking in useful lines that generated by the codegen. That would be the easiest solution that I can think of.
The proposed solution doesn't seems trivial at all hahaa. I don't think we have a all-rounded "diff algorithm" can handle it well in all cases. I guess if we're going to implement this "diff" approach, we might need user to resolve the conflict manually.
I assumed your code is tracked by a Git? Then, you can recover the lines you wish to keep while checking in useful lines that generated by the codegen. That would be the easiest solution that I can think of.
Yes, I did end up doing just that.
I figured this was a pretty specific use-case that's high-complexity in its implementation. I figured it might be worth bringing up anyway.
I can't commit to anything, but I may take a look at this problem when I have time. Thanks!
Instead of preserving changes, it might be interesting to able to specify additional traits to be derived when defining the tables in migration files, same goes for adding annotations on columns. You might be able to (ab)use table and column comments to accomplish this, which are supported by both MySQL and Postgres.
so what is the final solution?