Workflow for handling sequelize migrations and initialization?

心已入冬 提交于 2020-01-16 09:11:09

问题


I don’t get how sequelize migrations workflow works. I have a greenfield project, the database is designed using SQL scripts. We used sequelize auto to generate the models thereafter.

I need to now generate a migrations file starting out, so I can run the CLI to run SQL queries to create new tables with columns etc for locally provisioned databases.

There’s no 3rd party tools afaik to convert the SQL script I have into a Sequelize migrations file. I have to do this manually for 60 tables. There is a script method of auto-creating it, but it uses a seperate format that doesn’t have backward compatability for undoing migrations (sequelize-auto-migrations).

When I update the sequelize model schema in my file, there’s not a one off way command to have migrations file synchronize to those changes, via a diffing mechanisms based off the last migration execution ran.

This means I have to modify the model, AND modify the migrations file, leading to potential human error and duplicate redundant work.

so my issues are

  1. does sequelize have a way to initialize the first migrations file?
  2. is there a tool I can use when I modify models in sequelize, and generate the new migrations file automatically based off some diffing mechanism?

回答1:


okay I found out

For step 1, you can run a queryInterface.sequelize.query(<query>) and take the SQL scripts / run them right in sequelize for initialization. https://sequelize.org/master/class/lib/query-interface.js~QueryInterface.html. This short code references the sql script as the first migration

For step 2, you can modify the file and update the migrations manually, and run sequelize-auto to pull the latest changes from the remote/local db



来源:https://stackoverflow.com/questions/58003107/workflow-for-handling-sequelize-migrations-and-initialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!