I have a set of Sequelize models. I want to use migrations, not DB Sync.
Sequelize CLI seems to be able to do this, according to this article: \"When you use the CLI
It's 2020 and many of these answers no longer apply to the Sequelize v4/v5/v6 ecosystem.
The one good answer says to use sequelize-auto-migrations
, but probably is not prescriptive enough to use in your project. So here's a bit more color...
My team uses a fork of sequelize-auto-migrations because the original repo is has not been merged a few critical PRs. #56 #57 #58 #59
$ yarn add github:scimonster/sequelize-auto-migrations#a063aa6535a3f580623581bf866cef2d609531ba
Edit package.json:
"scripts": {
...
"db:makemigrations": "./node_modules/sequelize-auto-migrations/bin/makemigration.js",
...
}
Note: Make sure you’re using git (or some source control) and database backups so that you can undo these changes if something goes really bad.
.sync()
yarn db:makemigrations --name "mega-migration"
).01-mega-migration.js
and the _current.json
that is generated..sync()
or hand-written migrations, you need to “Fake” that mega-migration by inserting the name of it into your SequelizeMeta table. INSERT INTO SequelizeMeta Values ('01-mega-migration.js')
.$ yarn db:makemigrations --name whatever
02-whatever.js
migration and the changes to _current.json
, and _current.bak.json
.$ yarn sequelize db:migrate
.removeColumn
and addColumn
. This will lose data in production. You will need to modify the up and down actions to use renameColumn
instead.For those who confused how to use
renameColumn
, the snippet would look like this. (switch "column_name_before" and "column_name_after" for therollbackCommands
)
{
fn: "renameColumn",
params: [
"table_name",
"column_name_before",
"column_name_after",
{
transaction: transaction
}
]
}
If you have a lot of migrations, the down action may not perfectly remove items in an order consistent way.
The maintainer of this library does not actively check it. So if it doesn't work for you out of the box, you will need to find a different community fork or another solution.