-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtest_stairway.py
36 lines (27 loc) · 1.2 KB
/
test_stairway.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Test can find forgotten downgrade methods, undeleted data types in downgrade
methods, typos and many other errors.
Does not require any maintenance - you just add it once to check 80% of typos
and mistakes in migrations forever.
"""
import pytest
from alembic.command import downgrade, upgrade
from alembic.config import Config
from alembic.script import Script, ScriptDirectory
from staff.utils import alembic_config_from_url
def get_revisions():
# Create Alembic configuration object
# (we don't need database for getting revisions list)
config = alembic_config_from_url()
# Get directory object with Alembic migrations
revisions_dir = ScriptDirectory.from_config(config)
# Get & sort migrations, from first to last
revisions = list(revisions_dir.walk_revisions('base', 'heads'))
revisions.reverse()
return revisions
@pytest.mark.parametrize('revision', get_revisions())
def test_migrations_stairway(alembic_config: Config, revision: Script):
upgrade(alembic_config, revision.revision)
# We need -1 for downgrading first migration (its down_revision is None)
downgrade(alembic_config, revision.down_revision or '-1')
upgrade(alembic_config, revision.revision)