Reputation: 740
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.
Upvotes: 8
Views: 21863
Reputation: 9903
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
).
Upvotes: 0
Reputation: 371
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
Upvotes: 1
Reputation: 31
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.
Upvotes: 3
Reputation: 584
If you are really sure with reverting specific migration before some others. You might try tweaking its id value on the migration Table.
Upvotes: 1