Search code examples
node.jsmodelmigrationsequelize.jsumzug

Making reproducible string of migrations for Sequelize with models changing over time?


I'm building my first migration system ever with Umzug and Sequelize for a node app which uses sequelize and express on the backend.

My issue is that as the back end models change with commits over time (models being deleted, some changing, some being added) this breaks old migrations that happen to use the models from sequelize. Example:

Lets say I have a migration #1 that deals with "UserStats". Five releases later model UserStats needs to be removed from the app, so the model is deleted and a new migration is made to drop the table.

Now attempting to startup a new dev environment breaks because when the new server tries to run through all the old migrations it tries to find the model UserStats for the first migration, but the model no longer exists.

So the root problem is that versions of models get out of sync with the app migration state. Each migration requires the sequelize models to look like they did when that migration was initially created. What is the best way to deal with this?


Solution

  • Thanks Josh for the pointers.

    My final solution was to continue using Umzug and the same setup I've been using, and to replace any model based queries with raw queries using sequelize.query.

    Sequelize provides some nice features for automatically formatting results like your model (JS object), so the only thing you have to do is write the queries. Mine are pretty simple inserts/updates/deletes.

    This allows me to use the standard migration pattern for node, while still having reproducible migration history from app start to present, and not rely on changing models, and also use JS for my migrations instead of manipulating data in the SQL function language.

    Hope this is helpful.