`initialize_column_rename` results in different IDs in tests
noahfpf opened this issue · 0 comments
After running a migration that uses the initialize_column_rename
helper method, our unit tests that get the most recently created record with code like SomeModel.last
started failing because they were returning fixture records that were inserted before the tests.
Some speculation: perhaps Rails (or maybe minitest) resets Postgres sequence numbers when setting up tests. I'm guessing that normally Rails uses the max ID value for a table to set its related sequence. When the table has been temporarily renamed and a view instead represents the table this mechanism appears to be interfered with. Instead of the table's sequence's next value being set to 593363171 (or whatever, based on the test fixtures already inserted into the table), it's set to 1.
Note that we do have this configuration code:
OnlineMigrations.configure do |config|
...
config.column_renames = {
'some_models' => {
'foo' => 'bar',
},
}
We've fixed this problem by replacing SomeModel.last
with SomeModel.order(:created_at).last
in our tests, but a solution that doesn't require changing test code would be nice. If that makes sense and I can provide any extra information, please let me know. I'd also be open to looking into a fix, though I'm currently unclear on the mechanics behind both the column_renames
support and Rails' modifications to the sequence value.