Run only the next migration file
Asked Answered
Y

6

22

Is it possible to run only the next migration file with the 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.

For instance, I have the following when running the sequelize db:migrate:status command;

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.

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.

Yim answered 24/4, 2017 at 14:46 Comment(3)
When you run a sequelize:migrate command, sequelize will only run the migrations that are still pending (i.e the one that were never run before). To keep track of what was run, sequelize uses a table in the database. However, there isnt a way to run just a specific migration file.Kishakishinev
@JulienKlepatch That is too bad. Sometimes this is exactly what you want to do, just run one at a time. I solved my issue by moving the unwanted ones out of the folder, and running "one-by-one" simply by copying over one at a time.Yim
I see. Another possibility is to specify a different migration folder with a flag --migrations-path to the sequelize db:migrate commandKishakishinev
K
23

I came here from Google and was not able to find any options in doc. However, the CLI sequelize-cli db:migrate --help shows an option to select a range of migrations you want to run.

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
Komsomol answered 6/2, 2020 at 15:19 Comment(2)
Only the --to option is needed: npx sequelize-cli db:migrate --to filename.jsEmigrant
@Emigrant yaps only --to thanks bro , u save my life.Antiicer
R
7

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.

But if the question was:

I would like to only run the 20170421112638-do-some-refactor.js

Then you would have to use the option --from to jump all the other migrations until the desired one. In this case:

npx sequelize-cli db:migrate --from 20170316142544-create-an-index.js --to 20170421112638-do-some-refactor.js
Revet answered 22/7, 2020 at 16:53 Comment(0)
B
2

The things is, Sequelize only executes those migrations which are not executed yet, Means which is pending.

It means when you run command sequelize:migrate it will only executes those who is still pending.

Brockington answered 22/9, 2018 at 4:32 Comment(0)
B
1

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. So ,if you change the first string of name for the other wanted file, e.i.

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.

Broomstick answered 13/8, 2019 at 22:40 Comment(0)
C
1

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. 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.

Catarrhine answered 13/3, 2020 at 2:29 Comment(0)
W
1

If you want to run a single migration at a time, you can use --name, here's an exemple:

npx sequelize-cli db:migrate --name XXXXXXXXXXXXXX-create-single-table.js

With that, you can run just the migrations you actually want to run. Hope it helps!

Windy answered 18/10, 2023 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.