LendingHome / zero_downtime_migrations

Zero downtime migrations with ActiveRecord 3+ and PostgreSQL
MIT License
561 stars 27 forks source link

Suggestions for ZeroDowntimeMigrations::Validation::AddColumn #5

Closed kronn closed 6 years ago

kronn commented 7 years ago

I see that you recommend splitting one (potentially unsafe) change over several migrations, in the case of ZeroDowntimeMigrations::Validation::AddColumn.

I think it would be just as good to have the change just split into the suggested statements. This would leave just one point of truth for the change and also make rollback easier as a consequence.

Also, I would suggest to use say or even better say_with_time for the find_in_batches-output.

What are your thoughts on this?

shuber commented 7 years ago

Hey @kronn - the issue for the add_column case is that ActiveRecord wraps each migration in one big DDL transaction. Even if we split apart into the suggested statements, if they all run in the same DDL transaction then splitting them has no effect. It will lock the table just like it does when we call add_column with the :default option.

By splitting these statements into separate migrations, each transaction is committed before moving onto the next one. This way the column can be added quickly without holding up the transaction and locking the table while thousands of existing records are updated with the new column default.

I like your suggestion for using say_with_time for the batches output, I'll leave this issue open until we make that change. Thanks for the feedback!