Skip to content

Commit

Permalink
Fix airflow db check-migrations (#19597)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashb authored Nov 18, 2021
1 parent 9a246d3 commit 5763065
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 9 additions & 5 deletions airflow/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,13 +627,17 @@ def check_migrations(timeout):
:param timeout: Timeout for the migration in seconds
:return: None
"""
from alembic.runtime.migration import MigrationContext
from alembic.runtime.environment import EnvironmentContext
from alembic.script import ScriptDirectory

config = _get_alembic_config()
script_ = ScriptDirectory.from_config(config)
with settings.engine.connect() as connection:
context = MigrationContext.configure(connection)
with EnvironmentContext(
config,
script_,
) as env, settings.engine.connect() as connection:
env.configure(connection)
context = env.get_context()
ticker = 0
while True:
source_heads = set(script_.get_heads())
Expand All @@ -642,8 +646,8 @@ def check_migrations(timeout):
break
if ticker >= timeout:
raise TimeoutError(
f"There are still unapplied migrations after {ticker} seconds. "
f"Migration Head(s) in DB: {db_heads} | Migration Head(s) in Source Code: {source_heads}"
f"There are still unapplied migrations after {ticker} seconds. Migration"
f"Head(s) in DB: {db_heads} | Migration Head(s) in Source Code: {source_heads}"
)
ticker += 1
time.sleep(1)
Expand Down
6 changes: 5 additions & 1 deletion tests/utils/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

from airflow.models import Base as airflow_base
from airflow.settings import engine
from airflow.utils.db import create_default_connections
from airflow.utils.db import check_migrations, create_default_connections


class TestDb(unittest.TestCase):
Expand Down Expand Up @@ -103,3 +103,7 @@ def test_default_connections_sort(self):
source = inspect.getsource(create_default_connections)
src = pattern.findall(source)
assert sorted(src) == src

def test_check_migrations(self):
# Should run without error. Can't easily test the behaviour, but we can check it works
check_migrations(1)

0 comments on commit 5763065

Please sign in to comment.