Add default value support for rename_column_concurrently
What does this MR do and why?
This makes it possible to use rename_column_concurrently
for columns with a default value. !52032 (merged) explains the problem in more detail.
This MR aims to implement the solution outlined in !52032 (merged):
the default is a constant value - add additional checks in the insert trigger, where we inspect incoming values. If one column is set to the default, and the other is not, we can assume the non-defaulted value is the correct value.
How to set up and validate locally
- Check out the
andysoiron/rename_column_concurrently-with-default-test-migrations
branch. This includes some migrations to test the changes. You can compare the changes here. - Run
rails db:migrate
- The last migration is expected to fail with
rename_column_concurrently does not currently support columns with default functions
- In rails console, create a test record
t = DefaultTest.create
- All columns should have the default value:
> id: 1, test_function: Fri, 05 Aug 2022 14:38:51.231085000 UTC +00:00, test_string: "test", test_integer: 1, test_string_new: "test", test_integer_new: 1
- Update the old and new columns and reload.
t.update(test_integer: 2, test_string_new: 'new') t.reload
- All columns should now have the updated values
> id: 1, test_function: Fri, 05 Aug 2022 14:38:51.231085000 UTC +00:00, test_string: "new", test_integer: 2, test_string_new: "new", test_integer_new: 2
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Related issue: #339948 (closed)
Edited by Andy Schoenen