Skip to content

Concern to fix columns not being persisted after a change

What does this MR do and why?

Describe in detail what your merge request does and why.

This MR adds a concern that prevents a problem when changing column defaults while creating models that explicitly use the old column default.

Without the SafelyChangeColumnDefault concern, when we run Model.create!(value: 1) when the value's default is 1, rails skips the value insert and inserts DEFAULT VALUES instead. Then, when the column default is updated in a migration, the schema cache is out of date, and the DEFAULT VALUES begins inserting the new default.

This concern fixes this by telling rails to explicitly insert columns that could have this problem.

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

The tests included show how this process happens with an arbitrary model. To validate locally you could change a column default with a rails console open and try inserting values with and without the concern.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Simon Tomlinson

Merge request reports