avram icon indicating copy to clipboard operation
avram copied to clipboard

Alternate column names

Open jwoertink opened this issue 5 years ago • 6 comments

We have several apps built in different languages that use the same database. At some point in the last 15 years of this app running, some columns have been named wonky or misspelled. We can't just easily run a RENAME COLUMN migration because there's no telling where all this thing may be called.

What if we had a way to rename the method that calls a column?

table do
  column email : String, from: "employeemail"
end

This way I could also do UserQuery.new.email("[email protected]") which would still generate SELECT * FROM users WHERE employeemail = '[email protected]'

jwoertink avatar May 27 '20 16:05 jwoertink

That's going to be very useful I think. Not only for legacy databases but also as an alias for longer column names.

wout avatar May 27 '20 16:05 wout

I dig it 👍

paulcsmith avatar May 27 '20 18:05 paulcsmith

Oh this is a cool feature, I'd like to work on this.

rnice01 avatar Jun 01 '20 16:06 rnice01

Sure! You can have at it @rnice01

jwoertink avatar Jun 01 '20 16:06 jwoertink

We may be able to hook in to the @[DB::Field(key: "firstname")] annotation to handle this. https://github.com/crystal-lang/crystal-db/blob/master/src/db/serializable.cr#L41-L42

jwoertink avatar Aug 03 '20 16:08 jwoertink

Just looked into this since DB.mapping supports the key field as well. I learned that the key only applies to reading the result set back from the database so it would take quite an investment to update the other places to use the override as well.

matthewmcgarvey avatar Sep 06 '20 03:09 matthewmcgarvey