Alembic revision - error of several heads (taking into account branching)

I have an application and I wanted to create a new migration for it today. When i run

$ alembic revision -m "__name__" 

I received a message

 Only a single head is supported. The script directory has multiple heads (due branching), which must be resolved by manually editing the revision files to form a linear sequence. Run 'alembic branches' to see the divergence(s). 

Run

 alembic branches 

gives nothing

I am new to Alembic. 2 developers are working on this application, and we have 2 git branches - master & development (I'm not sure if this is somehow connected with this).

Any clue what is this about?

+21
git python flask alembic
source share
4 answers

I launched

 $ python manage.py db history 

And as a result, I got

 vagrant@precise64:/vagrant$ python manage.py db history Rev: 29c319804087 (head) Parent: 313837798149 Path: migrations/versions/29c319804087_.py empty message Revision ID: 29c319804087 Revises: 313837798149 Create Date: 2014-03-05 21:26:32.538027 Rev: 313837798149 Parent: 280061454d2a Path: migrations/versions/313837798149_.py empty message Revision ID: 313837798149 Revises: 280061454d2a Create Date: 2014-01-10 03:19:39.838932 Rev: 280061454d2a Parent: None Path: migrations/versions/280061454d2a_.py empty message Revision ID: 280061454d2a Revises: None Create Date: 2013-12-08 03:04:55.885033 Rev: 2e74f61d3b80 (head) Parent: 49501407aec9 Path: migrations/versions/2e74f61d3b80_o2_lease.py o2 lease Revision ID: 2e74f61d3b80 Revises: 49501407aec9 Create Date: 2014-02-28 10:38:06.187420 Rev: 49501407aec9 Parent: None Path: migrations/versions/49501407aec9_.py empty message Revision ID: 49501407aec9 Revises: None Create Date: 2014-01-22 11:27:08.002187 

What you can see here are two different branches. One begins with 49501407aec9 and the second with 280061454d2a. I moved 49501407aec9 and the next 2e74f61d3b80 from the / versions directory, launched

 $ python manage.py db revision 

and he created a new migration file.

+5
source share

This problem occurs when two migrations of alebs come from the same migration. This usually happens when several people make changes to the scheme. To fix this, you just need to configure down_revision your migration so that it matches the latter. alembic history shows us this:

 2f4682466279 -> f34e92e9dc54 (head), Fifth revision (on a separate branch) 2f4682466279 -> f673ac37b34a (head), Fifth revision (local) 2dc9337c3987 -> 2f4682466279, Fourth revision 0fa2aed0866a -> 2dc9337c3987, Third revision 22af4a75cf06 -> 0fa2aed0866a, Second revision 9a8942e953eb -> 22af4a75cf06, First revision 

You can see that one of the Fifth revisions was done locally, and this is a subordinate revision - 2f4682466279 but the one who made the other fifth revision also received the same subsequent revision.

Go to one of the fifth revision files and update the down_revision variable to reference the other fifth revision, for example like this:

 f673ac37b34a -> f34e92e9dc54 (head), Fifth revision (on a separate branch) 2f4682466279 -> f673ac37b34a, Fifth revision (local) 2dc9337c3987 -> 2f4682466279, Fourth revision 0fa2aed0866a -> 2dc9337c3987, Third revision 22af4a75cf06 -> 0fa2aed0866a, Second revision 9a8942e953eb -> 22af4a75cf06, First revision 

In this case, I updated the migration f34e92e9dc54 to have down_revision='f673ac37b34a' .

+23
source share

Perhaps the most common (and reliable) solution is to use alembic merge heads . In the same way, when you have two branches in Git, you can merge them together with the merge commit, in Alembic, when you have two branches, you can return them together with the merge revision.

For example, suppose we have revision 1a6b1a4a0574, which adds table A, and revision 2e49118db057, which adds table B. We can see these revisions (both marked as (head) ) in alembic history :

 $ alembic history <base> -> 2e49118db057 (head), Add table B <base> -> 1a6b1a4a0574 (head), Add table A 

Then we can combine them by running alembic merge heads :

 $ alembic merge heads Generating /Users/markamery/alembictest/alembic/versions/409782f4c459_.py ... done $ alembic history 2e49118db057, 1a6b1a4a0574 -> 409782f4c459 (head) (mergepoint), empty message <base> -> 2e49118db057, Add table B <base> -> 1a6b1a4a0574, Add table A 

If one of your revisions may already have been started somewhere (including on the developer's machine of one of your colleagues), then you probably want to use alembic merge instead of messing with down_revision one of the revisions, as others suggest answers here, The danger of tinkering with the downgrade is that it can cause the revision to not be applied. For example, suppose your colleague Bob has already removed a branch with revision 2e49118db057 and launched alembic upgrade head , creating table B. Then you decided to change down_revision 2e49118db057 so that it points to 1a6b1a4a0574, which Bob has never seen or run before. Bob removes your changes, launches the alembic upgrade head , and ... nothing happens, because with regard to Alembic he is already in the head and he does not need to run 1a6b1a4a0574. And so Bob never gets table A and probably never finds out why his database is in a bad state.

Don’t break Bob’s database - instead do a merge revision.

+15
source share

To combine heads, you can create an empty “merge migration” through

 alembic history | grep '(head)' | cut -d' ' -f3 | xargs alembic merge -m "merging heads" 

This will create a new mirror image in which both (or any other number) of the head will be directed down. It was mainly described here: https://blog.jerrycodes.com/multiple-heads-in-alembic-migrations/

0
source share

All Articles