我不明白 sequelize 迁移工作流程的工作原理.我有一个新建项目,数据库是使用 SQL 脚本设计的.之后我们使用 sequelize auto 生成模型.
我现在需要生成一个迁移文件,这样我就可以运行 CLI 来运行 SQL 查询,从而为本地配置的数据库创建带有列等的新表.
没有第三方工具 afaik 可以将我拥有的 SQL 脚本转换为 Sequelize 迁移文件.我必须为 60 个表手动执行此操作.有一种自动创建它的脚本方法,但它使用了一种单独的格式,该格式不具有用于撤消迁移的向后兼容性(sequelize-auto-migrations).
当我在我的文件中更新 sequelize 模型架构时,没有一个单独的命令可以通过基于上次迁移执行运行的差异机制使迁移文件与这些更改同步.
这意味着我必须修改模型,并修改迁移文件,从而导致潜在的人为错误和重复的冗余工作.
所以我的问题是
好吧我知道了
对于第 1 步,您可以运行 queryInterface.sequelize.query(<query>) 并获取 SQL 脚本/在 sequelize 中直接运行它们以进行初始化.
对于第 2 步,您可以手动修改文件并更新迁移,然后运行 sequelize-auto 从远程/本地数据库中提取最新更改
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
解决方案
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. sequelize/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
更多推荐
处理续集迁移和初始化的工作流程?
发布评论