Electron100 / butane

An ORM for Rust with a focus on simplicity and on writing Rust, not SQL
Apache License 2.0
84 stars 12 forks source link

Column renaming #89

Open jayvdb opened 1 year ago

jayvdb commented 1 year ago

migrations::adb::Operation supports AddColumn, RemoveColumn and ChangeColumn, but has no RenameColumn.

Implementing the backend of rename column is easy for postgres which supports this, and looks like it might also be possible with sqlite per https://www.sqlite.org/lang_altertable.html

Then the question is how to expose that, so that the rename can be expressed in the Rust.

Maybe using serde's alias to indicate the old column name, like:

#[model]
#[derive(...)]
pub struct FooBar {
    #[serde(alias = "id")]
    pub foo_id: uuid::Uuid,
    ...
}

Once the rename has been stored in the migration, the alias should be able to be removed or kept for as long the code needs it. i.e. the migration system should recognise it has already done that migration, and not break anything if the alias is retained or removed at any later time. The system should also be able to handle alias being added to an existing, not renamed, field.

ugochukwu-850 commented 4 months ago

I like this topic , and your approach and Idea sounds right ... but how about just changing the Model / struct name and then running a special cli migration command that can now change the column name

I would love to contribute on this one

Electron100 commented 4 months ago

I don't think it's exclusive. I like the idea of having an attribute to indicate a rename AND being able to do it from a cli command. I'd probably lean slightly towards having a butane-custom attribute for the rename, though it could recognize the serde alias too.

@ugochukwu-850 thanks for your interest. I'd welcome a PR doing either or both of these things.