Question

I'm a bit confused about how migrations are handled in Sequelize. It is told in the documentation that

migrator.migrate ... will execute all the up methods within your pending migrations

Does pending mean that Sequelize keeps track of what migrations has been run on the database at hand and which has not in some persistent storage? If that's so, does it mean that I should probably just load all the migrations in the migration folder at the app startup and attempt to run them?

If not, what's the best practice to keep track of what migrations should be ran on the current server?

Was it helpful?

Solution

You are right when you say that pending refers to the migrations that have not yet been run on the database at hand.

If that app isn't yet deployed, you don't need to run migrations to update your database. You can instead update your models to reflect the changes you want. You can always drop/create a database locally for the changes to appear, but only if you are still in development.

If your app is deployed, the database already exists and you want to update it, then when you run the migration command, it will run each new file in your migrations folder. If you're dealing with multiple environments and collaborators on the application, I would just keep the migration files in the folder. Once you create and run a new migration, the previous migrations do not run again so you don't need to worry about them.

OTHER TIPS

Sequelize does keep track of which migrations have been run in a persistent store. You have some options about what that storage mechanism is.

  • Keep it in a JSON file. You can specify the path and file name.
  • Keep it in your database. By default it will be stored in a table called SequelizeMeta but you can change that.
  • Define a custom storage mechanism accessed through a module that you create. This module must expose an object with certain predefined methods such as

    logMigration: function (migrationName)

    unlogMigration: function (migrationName)

    The system will call these methods on your object and expect it to store the appropriate data somewhere.

Check this out for more info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top