Skip to content

Commit 2604527

Browse files
guan404mingWesley Chiu
authored and
Wesley Chiu
committed
ci: add alembic check
1 parent fdf7f77 commit 2604527

File tree

6 files changed

+92
-5
lines changed

6 files changed

+92
-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,77 @@
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+
["breeze", "shell", "cd airflow-core/src/airflow && alembic check", "--answer", "n"],
46+
check=False,
47+
capture_output=True,
48+
text=True,
49+
)
50+
51+
if result.returncode != 0:
52+
console.print(
53+
Panel.fit(
54+
"[bold red]Model changes detected that require a migration![/bold red]\n\n"
55+
"The alembic check command has detected model changes that would generate new migration operations.\n\n"
56+
"Please create a migration file by running:\n"
57+
'[bold yellow]cd airflow-core && alembic revision --autogenerate -m "your migration message"[/bold yellow]\n\n'
58+
"Then review the generated migration file to ensure it captures your changes correctly.",
59+
title="Migration Required",
60+
border_style="red",
61+
)
62+
)
63+
64+
return 1
65+
else:
66+
console.print("[green]No new upgrade operations detected. No migration needed.[/green]")
67+
return 0
68+
69+
except subprocess.CalledProcessError as e:
70+
console.print(f"[bold red]Error running alembic check:[/bold red] {e}")
71+
console.print(f"[bold red]Output:[/bold red] {e.stdout.decode() if e.stdout else ''}")
72+
console.print(f"[bold red]Error:[/bold red] {e.stderr.decode() if e.stderr else ''}")
73+
return 1
74+
75+
76+
if __name__ == "__main__":
77+
sys.exit(check_for_migration_needs())

0 commit comments

Comments
 (0)