Skip to content

Commit 6def718

Browse files
committed
ci: integrate alembic check
1 parent 223a741 commit 6def718

File tree

6 files changed

+95
-5
lines changed

6 files changed

+95
-5
lines changed

.pre-commit-config.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ repos:
170170
additional_dependencies: ['rich>=12.4.4', 'ruff==0.11.2']
171171
files: ^providers/.*/src/airflow/providers/.*\.py$
172172
require_serial: true
173+
- id: check-model-changes
174+
name: Check if model changes require DB migration
175+
entry: ./scripts/ci/pre_commit/check_model_changes.py
176+
language: python
177+
additional_dependencies: ['rich>=12.4.4']
178+
files: ^airflow-core/src/airflow/models/.*\.py$
179+
require_serial: true
173180
- id: update-black-version
174181
name: Update black versions everywhere (manual)
175182
entry: ./scripts/ci/pre_commit/update_black_version.py

contributing-docs/08_static_code_checks.rst

+2
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ require Breeze Docker image to be built locally.
197197
+-----------------------------------------------------------+--------------------------------------------------------+---------+
198198
| check-min-python-version | Check minimum Python version | |
199199
+-----------------------------------------------------------+--------------------------------------------------------+---------+
200+
| check-model-changes | Check if model changes require DB migration | |
201+
+-----------------------------------------------------------+--------------------------------------------------------+---------+
200202
| check-newsfragments-are-valid | Check newsfragments are valid | |
201203
+-----------------------------------------------------------+--------------------------------------------------------+---------+
202204
| check-no-airflow-deprecation-in-providers | Do not use DeprecationWarning in providers | |

dev/breeze/doc/images/output_static-checks.svg

+4-4
Loading
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7f396adad6b3df5ee04aeef4fc62f6ad
1+
43950715b0d26262ee7f30c4cfa120d4

dev/breeze/src/airflow_breeze/pre_commit_ids.py

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"check-links-to-example-dags-do-not-use-hardcoded-versions",
6363
"check-merge-conflict",
6464
"check-min-python-version",
65+
"check-model-changes",
6566
"check-newsfragments-are-valid",
6667
"check-no-airflow-deprecation-in-providers",
6768
"check-no-providers-in-core-examples",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
"""
19+
Pre-commit hook to check if SQLAlchemy model changes require a database migration.
20+
This script uses alembic.command.check to detect if model changes require migrations.
21+
"""
22+
23+
from __future__ import annotations
24+
25+
import subprocess
26+
import sys
27+
28+
try:
29+
from rich import print
30+
from rich.console import Console
31+
from rich.panel import Panel
32+
except ImportError:
33+
print("ERROR: The rich package is required for this pre-commit hook.")
34+
print("Install it using: pip install rich")
35+
sys.exit(1)
36+
37+
console = Console(width=120)
38+
39+
40+
def check_for_migration_needs():
41+
"""Check if model changes require a migration using alembic.command.check."""
42+
43+
try:
44+
result = subprocess.run(
45+
[
46+
"breeze",
47+
"shell",
48+
"airflow db migrate --to-revision heads && cd airflow-core/src/airflow && alembic check",
49+
],
50+
check=True,
51+
capture_output=True,
52+
text=True,
53+
)
54+
55+
if result.returncode != 0:
56+
console.print(
57+
Panel.fit(
58+
"[bold red]Model changes detected that require a migration![/bold red]\n\n"
59+
"The alembic check command has detected model changes that would generate new migration operations.\n\n"
60+
"Please create a migration file by running:\n"
61+
'[bold yellow]cd airflow-core && alembic revision --autogenerate -m "your migration message"[/bold yellow]\n\n'
62+
"Then review the generated migration file to ensure it captures your changes correctly.",
63+
title="Migration Required",
64+
border_style="red",
65+
)
66+
)
67+
68+
return 1
69+
console.print("[green]No new upgrade operations detected. No migration needed.[/green]")
70+
return 0
71+
72+
except subprocess.CalledProcessError as e:
73+
console.print(f"[bold red]Error running alembic check:[/bold red] {e}")
74+
console.print(f"[bold red]Output:[/bold red] {e.stdout if hasattr(e, 'stdout') and e.stdout else ''}")
75+
console.print(f"[bold red]Error:[/bold red] {e.stderr if hasattr(e, 'stderr') and e.stderr else ''}")
76+
return 1
77+
78+
79+
if __name__ == "__main__":
80+
sys.exit(check_for_migration_needs())

0 commit comments

Comments
 (0)