I currently have a table that I use to log events that I track for users. It’s being used to track one model currently. I want to migrate this to have a polymorphic association to I can track and relate other models to it. I understand polymorphic associations, but I’m looking for some guidance on migrating and validating the historic data. Should this be in a migration? Is there a correct way to “test”?
The migration should be pretty simple. All the data in the table is of one polymorphic type. Let’s say your model is Log and currently a log belongs to an Event object.
To make that polymorphic belonging to, say Loggable, have the migration:
- Rename the event_id column to loggable_id
- Add the loggable_type column
- Update each row (with SQL) setting the loggable_type column to ‘Event’
- Index as appropriate
I’d test this by migrating up and down, running your suite after migrating down and again when you go back up. The failures after migrating should be what you expect. Fix them and get a green suite.