Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix alembic's "Requested revision overlaps with other requested revisions"?

Tags:

alembic

I work on a team using alembic to manage db migrations. I recently pulled master, and tried to run alembic upgrade heads. I got the following message;

INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
ERROR [alembic.util.messaging] Requested revision a04c53fd8c74 overlaps with other requested revisions 453d88f67d34
  FAILED: Requested revision a04c53fd8c74 overlaps with other requested revisions 453d88f67d34

I got the same message when I tried to run alembic downgrade -1. Running alembic history prints this;

453d88f67d34 -> a04c53fd8c74 (label_1, label_2) (head), Create such and such tables.
2f15c778e709, 9NZSZX -> 453d88f67d34 (label_1, label_2) (mergepoint), empty message
b1861bb8b23f, b8aa3acdf260 -> 2f15c778e709 (label_1, label_2) (mergepoint), Merge heads b18 and b8a
(...many more old revisions)

which to me looks like a perfectly fine history. alembic heads reports a04c53fd8c74 (label_1, label_2) (head).

The only thing that looks odd to me is that my alembic version db has two values in it;

my_postgres=# SELECT * FROM alembic_version;
 version_num  
--------------
 a04c53fd8c74
 453d88f67d34
(2 rows)

The only reference I can find from googling the exception is the source code, which I'd rather not read through.

How could this situation have come about? How should I fix it? What does "overlaps" mean?

like image 906
Alex Altair Avatar asked Feb 23 '17 19:02

Alex Altair


3 Answers

I "fixed" it by deleting the older version number in the database;

my_postgres=# DELETE FROM alembic_version WHERE version_num = '453d88f67d34';
DELETE 1
my_postgres=# SELECT * FROM alembic_version;
 version_num  
--------------
 a04c53fd8c74
(1 row)

I can now run upgrades and downgrades. My history and heads look the same. But I still don't know why it happened, or whether there is some db state that is subtly messed up, so if anyone has a better answer please post it!

like image 198
Alex Altair Avatar answered Nov 18 '22 19:11

Alex Altair


For those who find this, this happened to me as well because I tried restoring my database to an older version of it without first dropping it. I believe you should only ever have one row in your alembic_version table with the version_num of whatever version your database should currently be at.

So when I restored my database and did not drop it first, rather than replacing the current version number, it added a new row. In order to fix it I had to delete the incorrect version from my alembic table (whatever the version number of the database was that I should have dropped first). ... just to give a little more context to Altair's answer.

like image 5
Sir Neuman Avatar answered Nov 18 '22 20:11

Sir Neuman


  1. The alembic_version table should have only one row for consistency.
  2. If you restore a database data from another version, restore operation WILL ADD another row.
  3. After you restore the data, delete the row which was added from the restore operation.

This one worked for me:

delete from alembic_version where version_num='e69ad4aec63b';
like image 3
Zuman Avatar answered Nov 18 '22 20:11

Zuman