|
| 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