处理续集迁移和初始化的工作流程? [英] Workflow for handling sequelize migrations and initialization?
问题描述
我不明白 sequelize 迁移工作流程的工作原理.我有一个新建项目,数据库是使用 SQL 脚本设计的.之后我们使用 sequelize auto
生成模型.
我现在需要生成一个迁移文件,这样我就可以运行 CLI 来运行 SQL 查询,从而为本地配置的数据库创建带有列等的新表.
没有第三方工具 afaik 可以将我拥有的 SQL 脚本转换为 Sequelize 迁移文件.我必须为 60 个表手动执行此操作.有一种自动创建它的脚本方法,但它使用了一种单独的格式,该格式不具有用于撤消迁移的向后兼容性(sequelize-auto-migrations).
当我在我的文件中更新 sequelize 模型架构时,没有一个单独的命令可以通过基于上次迁移执行运行的差异机制使迁移文件与这些更改同步.
这意味着我必须修改模型,并修改迁移文件,从而导致潜在的人为错误和重复的冗余工作.
所以我的问题是
- sequelize 有办法初始化第一个迁移文件吗?
- 当我在 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
- does sequelize have a way to initialize the first migrations file?
- 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?
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
这篇关于处理续集迁移和初始化的工作流程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!