From 53c6ec5aa99be0b0a79e61d6293c6c9bf5330b93 Mon Sep 17 00:00:00 2001 From: Kaxil Naik Date: Tue, 22 Oct 2024 02:51:42 +0100 Subject: [PATCH] Fix alembic template & make pre-commit more resilient MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While working on this https://github.com/apache/airflow/pull/43243, I was following https://github.com/apache/airflow/blob/main/contributing-docs/13_metadata_database_updates.rst and I ran into an error with pre-commit hook. When running the revision command as follows: ``` root@f1f78138ad78:/opt/airflow/airflow# alembic revision -m "New revision" Generating /opt/airflow/airflow/migrations/versions/01b38be821e9_new_revision.py ... done ``` It creates a file as follows: ```python """New revision Revision ID: cd7be1ae8b80 Revises: 05234396c6fc Create Date: 2024-10-22 01:44:17.873864 """ import sqlalchemy as sa from alembic import op # revision identifiers, used by Alembic. revision = 'cd7be1ae8b80' down_revision = '05234396c6fc' branch_labels = None depends_on = None ``` Notice single quotes in `revision` & `down_revision`. Now if I just run that single pre-commit hook (`update-migration-references`), it fails ``` ❯ pre-commit run "update-migration-references" --all-files Update migration ref doc.................................................Failed - hook id: update-migration-references - exit code: 1 Using 'uv' to install Airflow Using airflow version from current sources Updating migration reference for airflow Making sure airflow version updated Making sure there's no mismatching revision numbers Traceback (most recent call last): File "/opt/airflow/scripts/in_container/run_migration_reference.py", line 246, in correct_mismatching_revision_nums(revisions=revisions) File "/opt/airflow/scripts/in_container/run_migration_reference.py", line 230, in correct_mismatching_revision_nums new_content = content.replace(revision_id_match.group(1), revision_match.group(1), 1) AttributeError: 'NoneType' object has no attribute 'group' Error 1 returned If you see strange stacktraces above, run `breeze ci-image build --python 3.9` and try again. ``` That isn't a problem generally as `ruff` will fail before and convert it into double quotes. But rather than doing that, we fix it at source. --- airflow/migrations/script.py.mako | 4 ++-- docs/apache-airflow/img/airflow_erd.sha256 | 2 +- scripts/in_container/run_migration_reference.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/airflow/migrations/script.py.mako b/airflow/migrations/script.py.mako index 664b20b60bf8b..81181d9d631b8 100644 --- a/airflow/migrations/script.py.mako +++ b/airflow/migrations/script.py.mako @@ -29,8 +29,8 @@ from alembic import op ${imports if imports else ""} # revision identifiers, used by Alembic. -revision = ${repr(up_revision)} -down_revision = ${repr(down_revision)} +revision = "${up_revision}" +down_revision = "${down_revision}" branch_labels = ${repr(branch_labels)} depends_on = ${repr(depends_on)} diff --git a/docs/apache-airflow/img/airflow_erd.sha256 b/docs/apache-airflow/img/airflow_erd.sha256 index 9386cf49a0442..7adc054f61db4 100644 --- a/docs/apache-airflow/img/airflow_erd.sha256 +++ b/docs/apache-airflow/img/airflow_erd.sha256 @@ -1 +1 @@ -a874f49d63f37d46c47de8e473aa87b8877b0c5a9f670722017cfd0ea9aeec51 \ No newline at end of file +e1e32de9983ee02f268010485363ec222927c64e13be142761fc2b9f9cf33090 \ No newline at end of file diff --git a/scripts/in_container/run_migration_reference.py b/scripts/in_container/run_migration_reference.py index 2322de9d50e3c..71b0040dc6864 100755 --- a/scripts/in_container/run_migration_reference.py +++ b/scripts/in_container/run_migration_reference.py @@ -213,8 +213,8 @@ def ensure_filenames_are_sorted(revisions, app): def correct_mismatching_revision_nums(revisions: Iterable[Script]): - revision_pattern = r'revision = "([a-fA-F0-9]+)"' - down_revision_pattern = r'down_revision = "([a-fA-F0-9]+)"' + revision_pattern = r'revision = ["\']([a-fA-F0-9]+)["\']' + down_revision_pattern = r'down_revision = ["\']([a-fA-F0-9]+)["\']' revision_id_pattern = r"Revision ID: ([a-fA-F0-9]+)" revises_id_pattern = r"Revises: ([a-fA-F0-9]+)" for rev in revisions: