Revert a specific migration in TypeORM
Asked Answered
S

4

7

Is it possible to revert a specific migration in Typeorm?, I want to only revert a particular migration and not all till I get to the migration I want to revert,

Because normally you just call typeorm migration: revert multiple times and it starts reverting from the last executed and removing it from the database, if you want to revert multiple migrations.

Sheffield answered 30/11, 2021 at 15:40 Comment(2)
Migrations are built up on each other so you have to walk back through them to revert to a prior state, you can't just jump back to a particular migration.Threephase
@Threephase so If I want to revert the first migration executed and I have 50 older migrations I would have to revert all of them first? What will be the consequence if a particular migration could be reverted?Sheffield
H
3

If you have a table update you want to change that is not related to the last migration committed then you should write a new migration to make the change.

Reverting any migration is a last resort operation that is available to you when things don't go as planned, but I find that most problems can be solved forward with new migrations rather than reverting back.

Also if you find your migrations are too large, rebase your migrations. You can remove all migrations and generate a single base migration that creates the database as is current. We find this useful to do after a long period of time as migrations become redundant overtime.

Homologue answered 25/10, 2022 at 19:48 Comment(0)
M
1

If you are really sure with reverting specific migration before some others. You might try tweaking its id value on the migration Table.

Manouch answered 11/5, 2022 at 15:4 Comment(0)
R
1

In my case, I knew that reverting the specific migration would not break anything, so I extracted all the text from the down script in the migration file and ran it directly in my SQL shell.

Here's the bash script I used to extract the SQL:

# assuming the text from the `down` script is being piped into stdin

while read -r line; do
    extracted_text=$(echo "$line" | awk -F '`' '{print $2}');

    echo "$extracted_text;"
done

# you can then pipe the output into a file for later use
Rhyolite answered 11/1 at 16:51 Comment(1)
Also, I would delete the last entry in the migrations tableEmbodiment
I
0

Conceptually, no, it is impossible, because each migration assumes all migrations before it had happened. For example, if your migration's up() script says:

ALTER TABLE
  article
ALTER COLUMN
  article_content nvarchar(4096)

… it assumes that there is a table article which has a column article_content, both of which had probably been defined in one or several previous migrations before it.

However, practically speaking, you can revert it separately if you can guarantee that no subsequent migration will ever depend on this one. Generally, you can't guarantee that, but there are exceptions. In this case, just execute the down() script, and then delete the entry from the migrations table (usually, it is called migrations).

Imperious answered 9/1 at 22:15 Comment(4)
How can I run the down() script?Rhyolite
By any means available to youImperious
Right, so I just grab the SQL and execute it directly in the shell.Rhyolite
Yes, that's what I had in mind. But you can always automate the process, depending on your setup.Imperious

© 2022 - 2024 — McMap. All rights reserved.