Update DB with migration

I have a general question about how to properly use migrations. I have recently implemented migrations on my site. Now I want to update a table column. What is the best way to do that? It seems that I have to create a new migration, but is this the only way? After potentially many updates, there will be many migration files with one or a few updates each resulting in a fragmented mess for lack of a better term.

Each migration related to it realeses. When previous release didn’t have a column in a table, it’s model didn’t have that column either.

So after you create your migration, you modified model so it have a column you added. With this way you can have versioning both in your application and your database/s.

I understand versioning and the purpose of migrations. I was just hoping to be able to update the existing migration the way every other file is updated, but apparently not.

@mcki0127 if you’re still developing the application, and it’s only on your own computer, you could rollback all the migrations (yii migrate/down) until you rollback the migration you want to modify, modify it, then play the migrations again.

BUT, there is absolutely no benefits in doing that, you’ll be just wasting some of your time. And if you’re working in a team, or the application is already deployed, this will surely provoque a mess.

On the contrary, you’ll be defeating the purpose of migrations by doing that, that is: migrations represent changes to your data structure which happened during time. By modifying an existing migration, you’re “cheating” history.

Just create a new migration and make the desired changes in it, run it, and be done with it.

Some user recently reported on slack that he has more than 200 migrations, and this is perfectly fine. They do not impact your website performances. (slack thread)

1 Like

This is going on a production server. I’m working solo right now, but trying to use best practices in preparation for the possibility of bringing in other developers in the future. I guess I don’t mind having many migration files as long as I know that’s the way it’s done. Wow…200. :open_mouth: Thanks for your help.

1 Like