Skip to content

Commit

Permalink
Fix: add a merge migration to solve multi head issue (getredash#5364)
Browse files Browse the repository at this point in the history
* Add unit test to test for multi-head migrations issue

* Add merge migration
  • Loading branch information
arikfr authored Jan 21, 2021
1 parent bb42e92 commit e71ccf5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
24 changes: 24 additions & 0 deletions migrations/versions/89bc7873a3e0_fix_multiple_heads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""fix_multiple_heads
Revision ID: 89bc7873a3e0
Revises: 0ec979123ba4, d7d747033183
Create Date: 2021-01-21 18:11:04.312259
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '89bc7873a3e0'
down_revision = ('0ec979123ba4', 'd7d747033183')
branch_labels = None
depends_on = None


def upgrade():
pass


def downgrade():
pass
20 changes: 20 additions & 0 deletions tests/test_migrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
from alembic.config import Config
from alembic.script import ScriptDirectory


def test_only_single_head_revision_in_migrations():
"""
If multiple developers are working on migrations and one of them is merged before the
other you might end up with multiple heads (multiple revisions with the same down_revision).
This makes sure that there is only a single head revision in the migrations directory.
Adopted from https://blog.jerrycodes.com/multiple-heads-in-alembic-migrations/.
"""
config = Config(os.path.join("migrations", 'alembic.ini'))
config.set_main_option('script_location', "migrations")
script = ScriptDirectory.from_config(config)

# This will raise if there are multiple heads
script.get_current_head()

0 comments on commit e71ccf5

Please sign in to comment.