ilyakatz / data-migrate

Migrate and update data alongside your database structure.
MIT License
1.4k stars 192 forks source link

Reset column information in-between each migration #251

Open caius opened 1 year ago

caius commented 1 year ago

We ran into an issue where having the following run in the same rake db:migrate:with_data process caused an issue:

  1. Data migration that writes to a table
  2. Schema migration that adds a new column to the table
  3. Data migration that writes to the same table

We ended up needing to call ActiveRecord::Base#reset_column_information) as documented in the rails guides between amending the table and trying to write to it in the same ruby process. (If you split any of the above migration files into different runs, this issue goes away.)

Rather than have to remember to call reset_column_information every time we either amend a table or try to write to a table in either schema or data migrations respectively, lets just call it for all models before we run either migration. It's a very low cost call from testing and migrations aren't usually run all that often (certainly compared to web requests).

caius commented 9 months ago

@ilyakatz is there any appetite for accepting this patch upstream please?

ilyakatz commented 9 months ago

Hi @caius i'm not actively involved in this anymore. I see that @ngan assigned himself to this request though, thanks, Ngan!

ngan commented 8 months ago

Hey @caius -- we're in the middle of refactoring how migrations are run and will look to incorporate this after the dust settles.