Is there a way to conditionally alter a table colu...
# squarelibraries
e
Is there a way to conditionally alter a table column in a SQLDelight migration file? So, if column
id
exists, rename it to
userId
but otherwise don’t.
m
conditionally no, but if you are working with version, you can create a new table with the new renamed column, copy all the records, and then drop the old one. only the new syntax (3.25+) supports renaming colums with the alter table rename column command
e
Thanks. I have a migration bug where we changed a column name without adding a migration and it went live. So some users have a column named id and some userId. I tried with ALTER TABLE but that command will throw an exception for the ones who have the new name on their column already. Creating a new table and copying will require me to be sure that the table I am copying from has a column with a specific name right ?
m
you can still create a migration for the old ones, normally if it errors out, a migration will not be applied. you can try something like the following:
Copy code
alter table current rename to old;
create table current.. (with all needed new  and renamed stuff);
insert or fail into (all new columns) select (all old columns) from old;
drop table old;
e
Thanks! I’ll see if I can turn this into something real 🙂
Thank you again for your help @Maarten Bruggeman. Your suggestion worked very well and fixed a problematic bug in production. 👍
👍 1