question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Revert migrations running in reverse order

See original GitHub issue

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [x] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [x] 0.2.18 (or put your version here)

Steps to reproduce or a small repository showing the problem:

I created two migrations, one on August and the other today. After running this last migration I wanted to check that the down would be applied correctly. However I run into the following:

Before running revert:

> db.migrations.find()
{ "_id" : ObjectId("5d7118c2bfc3a0f771f3ea4a"), "timestamp" : 1566560354098, "name" : "UpdateContacts1566560354098" }
{ "_id" : ObjectId("5d7118c2bfc3a0f771f3ea4b"), "timestamp" : 1567689639607, "name" : "MergeConfigs1567689639607" }

Running revert:

2 migrations are already loaded in the database.
UpdateContacts1566560354098 is the last executed migration. It was executed on Fri Aug 23 2019 13:39:14 GMT+0200 (Central European Summer Time).
Now reverting it...
Migration UpdateContacts1566560354098 has been reverted successfully.

After running revert:

> db.migrations.find()
{ "_id" : ObjectId("5d7118c2bfc3a0f771f3ea4b"), "timestamp" : 1567689639607, "name" : "MergeConfigs1567689639607" }

Is this a known issue of some sort? Is there any flag or anything which I might be missing?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
RecuencoJonescommented, Sep 12, 2019

@DimaIT, I like option 1. We can add a comment to getLatestExecutedMigration with a precondition like Migration array must be sorted by descending ID.

1reaction
DimaITcommented, Sep 11, 2019

@RecuencoJones, thanks for clarification. Now your solution looks much more reasonable to me. I think it’s makes sense to add comment describing situation with mongo ids to your code.

We could probably try to parse the timestamp or the incremental part and sort by it, but not sure if that’s the right way to fix this.

That may work, but seems kind of tricky.

I have couple more ideas:

  1. We can sort migrations by id in loadExecutedMigrations. Than getLatestExecutedMigration needs just peek the last one.
  2. Or if we want to keep logic related to relational databases untouched, than we can only sort migrations for mongo by _id in reverse order. getLatestExecutedMigration will pick the first migration, wich will be the latest one. I think it’s the simpliest fix that can be made.
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to revert the last migration? - python - Stack Overflow
To reverse all migrations for an app, you can run: ... Solution: Write a Database router with method allow_migrate in order to differentiate...
Read more >
Undo Migrations - Tutorial - Flyway
This brief tutorial will teach how to use undo migrations. It will take you through the steps on how to create and use...
Read more >
What order does migrate:rollback execute? - Laracasts
The answer is, migrations rollback in the order they are created based on the date in the filename... which very well may be...
Read more >
Why Reversible Migrations Are Important and How to Write ...
Yes, migrations are reversible! What does it mean? When you run a migration, it runs by a default the up method which is...
Read more >
Rolling Back Migrations with Flyway - Baeldung
In our undo migration, notice how the filename-prefix is 'U' compared with the normal migration prefix of 'V'. Also, in our undo migration...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found