Drop unneeded column via codegen

Hi! I'm refactoring a resource and removed all references to office_id from my ParkingLot resource, replacing it with a belongs_to :site relationship. However, when I run mix ash.codegen, the migration generator is renaming the column instead of dropping the old one and creating a new one:
# Like this instead of drop/add
rename table(:parking_lots), :office_id, to: :site_id
# Like this instead of drop/add
rename table(:parking_lots), :office_id, to: :site_id
I responded yes during this question:
Are you renaming parking_lots.office_id to lots.site_id? [Yn] y
Are you renaming parking_lots.office_id to lots.site_id? [Yn] y
Is there a way to get the migration generator to properly drop the old column and create a fresh site_id column? Or do I need to manually write this migration? My current resource only has the belongs_to :site relationship with no traces of office_id anywhere.
Solution:
When I clicked no, the generator added the new column but left the old one, which is fine to be honest. What I ended up doing is first removing all references of the old relationship, then running codegen. This removed the old column. Then I added the new relationships to the new table and ran codegen again. Worked perfect, and I kinda like that it forced me to make more granular migrations to drop and then create.
Jump to solution
8 Replies
ZachDaniel
ZachDaniel3mo ago
If you rename a column it will do a rename command so no need to drop the old column.
Yahya Oxley
Yahya OxleyOP3mo ago
@Zach Daniel thank you and that is true. I wonder however if the column will maintain old data that no longer points to IDs in the correct table
ZachDaniel
ZachDaniel3mo ago
Yeah, it would likely Well, it wouldn't work in real life the mgiration would fail due to foreign keys etc. If you were to answer no to that prompt then it would drop the old column and create the new one instead of using rename
Solution
Yahya Oxley
Yahya Oxley3mo ago
When I clicked no, the generator added the new column but left the old one, which is fine to be honest. What I ended up doing is first removing all references of the old relationship, then running codegen. This removed the old column. Then I added the new relationships to the new table and ran codegen again. Worked perfect, and I kinda like that it forced me to make more granular migrations to drop and then create.
ZachDaniel
ZachDaniel3mo ago
It left the old one? Wait are you using ash_sqlite?
Yahya Oxley
Yahya OxleyOP3mo ago
yes it did, I am using ash_postgres
ZachDaniel
ZachDaniel3mo ago
Hmm...that's pretty strange Is it there in the migration but commented out perhaps?
Yahya Oxley
Yahya OxleyOP3mo ago
I no longer have it 🙁 Once I found the solution I deleted the unused migration

Did you find this page helpful?