简体   繁体   English

仅运行下一个迁移文件

[英]Run only the next migration file

Is it possible to run only the next migration file with the sequelize-cli?是否可以使用 sequelize-cli 仅运行下一个迁移文件?

I have been looking through the docs and help-section of the cli, and it does not appear to be such a feature.我一直在查看 cli 的文档和帮助部分,它似乎不是这样的功能。

For instance, I have the following when running the sequelize db:migrate:status command;例如,我在运行sequelize db:migrate:status命令时有以下内容;

Loaded configuration file "config/config.js".
Using environment "development".
up   20170301090141-create-something.js
up   20170301133113-create-else.js
up   20170301133821-Update-some-stuff.js
up   20170301135339-create-some-model.js
up   20170307152706-update-some-stuff-two.js
down 20170316142544-create-an-index.js
down 20170421112638-do-some-refactor.js

I would like to only run the 20170316142544-create-an-index.js .我只想运行20170316142544-create-an-index.js

Of course, I can remove all the relevant files.当然,我可以删除所有相关文件。 Then I add each migration back one-by-one, running "all" migrations between each one.然后我将每个迁移一一添加回来,在每个迁移之间运行“所有”迁移。 But this seems so barbaric.但这似乎太野蛮了。

I came here from Google and was not able to find any options in doc.我是从 Google 来到这里的,但在 doc 中找不到任何选项。 However, the CLI sequelize-cli db:migrate --help shows an option to select a range of migrations you want to run.但是,CLI sequelize-cli db:migrate --help显示了一个选项,用于选择要运行的迁移范围。

They are:他们是:

--to Migration name to run migrations until
--from Migration name to start migrations from (excluding)

Apparently, you need to provide the exact filename not the migration name.显然,您需要提供确切的文件名而不是迁移名称。 (Found in source code) (在源代码中找到)

TLDR:域名注册地址:

npx sequelize-cli db:migrate --from will-not-be-included.js --to filename.js

I think the question was already answered, but some people still getting confused.我想这个问题已经回答了,但有些人仍然感到困惑。 I will try to give a straight answer for this specific question:我将尝试直接回答这个特定问题:

npx sequelize-cli db:migrate --to 20170316142544-create-an-index.js

In this specific case there's no need to add the option --from , because 20170316142544-create-an-index.js is the next migration to be applied.在这种特殊情况下没有必要添加的选项--from ,因为20170316142544-create-an-index.js是要应用的下一个迁移。

But if the question was:但如果问题是:

I would like to only run the 20170421112638-do-some-refactor.js我只想运行20170421112638-do-some-refactor.js

Then you would have to use the option --from to jump all the other migrations until the desired one.然后您将不得不使用选项--from跳转所有其他迁移,直到所需的迁移。 In this case:在这种情况下:

npx sequelize-cli db:migrate --from 20170316142544-create-an-index.js --to 20170421112638-do-some-refactor.js

The things is, Sequelize only executes those migrations which are not executed yet, Means which is pending.事情是,Sequelize 只执行那些尚未执行的迁移,即挂起的 Means。

It means when you run command sequelize:migrate it will only executes those who is still pending.这意味着当您运行命令sequelize:migrate 时,它只会执行那些仍在等待中的人。

I think you maked all models and afther that you're trying to run the migrations.我认为您制作了所有模型,然后您正在尝试运行迁移。

So, I found one solution, i don't know if this is good idea.所以,我找到了一个解决方案,我不知道这是否是个好主意。 Sequelize names the migration files with instant actual date. Sequelize 使用即时实际日期命名迁移文件。 So ,if you change the first string of name for the other wanted file, ei因此,如果您更改另一个想要的文件的第一个名称字符串,ei

20190717191628-create-team.js
20190717191909-create-player.js 
          **for**
20190717191909-create-team.js
20190717191628-create-player.js 

After that,order all files like you want , and run the db:migrate command.之后,根据需要对所有文件进行排序,然后运行db:migrate命令。

You can simply change the sequence of files in the migrations folder.您可以简单地更改迁移文件夹中的文件顺序。 As the filename always starts with timestamp so in order to change the sequence of migrations you just need to update the timestamp accordingly by renaming the file.由于文件名总是以时间戳开头,因此为了更改迁移顺序,您只需要通过重命名文件来相应地更新时间戳。

20170316142544-create-an-index.js. 20170316142544-create-an-index.js。 just see which migration ran latest and add rename this migration timestamp with the timestamp just next to the one that executed.只需查看最新运行的迁移并添加重命名此迁移时间戳,并在执行的时间戳旁边添加时间戳。 add some typos in other ones after it or move them out of the folder for a second and then migrate.在它之后添加一些错别字或将它们移出文件夹一秒钟然后迁移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM