You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tooling.apache.org by sb...@apache.org on 2026/07/03 14:58:53 UTC
(tooling-trusted-releases) 01/01: Prevent published Alembic migrations from being modified
This is an automated email from the ASF dual-hosted git repository.
sbp pushed a commit to branch sbp
in repository https://gitbox.apache.org/repos/asf/tooling-trusted-releases.git
commit d318c03cb433e62f7be569a2a2b29dc6d46b77f9
Author: Sean B. Palmer <se...@miscoranda.com>
AuthorDate: Fri Jul 3 15:58:19 2026 +0100
Prevent published Alembic migrations from being modified
---
.github/workflows/analyze.yml | 18 +++++
.pre-commit-config.yaml | 8 +++
scripts/check_migrations_immutable.py | 120 ++++++++++++++++++++++++++++++++++
3 files changed, 146 insertions(+)
diff --git a/.github/workflows/analyze.yml b/.github/workflows/analyze.yml
index e24885eb..68b5ec31 100644
--- a/.github/workflows/analyze.yml
+++ b/.github/workflows/analyze.yml
@@ -38,6 +38,7 @@ jobs:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
persist-credentials: false
+ fetch-depth: 0
- uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0
with:
python-version: ${{ matrix.python-version }}
@@ -70,3 +71,20 @@ jobs:
- name: Run pre-commit
run: |
uv run --frozen pre-commit run --show-diff-on-failure --color=always --all-files
+
+ - name: Run pre-commit on each pushed commit
+ if: github.event_name == 'push' && github.event.before != '0000000000000000000000000000000000000000'
+ env:
+ BEFORE: ${{ github.event.before }}
+ run: |
+ if ! git rev-parse --verify --quiet "${BEFORE}^{commit}" > /dev/null; then
+ echo "WARNING: previous tip ${BEFORE} is unavailable, skipping per-commit checks for this push"
+ exit 0
+ fi
+ for commit in $(git rev-list --reverse --no-merges "${BEFORE}..HEAD"); do
+ echo "::group::pre-commit at ${commit}"
+ git checkout --quiet "${commit}"
+ uv run --frozen pre-commit run --show-diff-on-failure --color=always --from-ref "${commit}^" --to-ref "${commit}"
+ echo "::endgroup::"
+ done
+ git checkout --quiet "${GITHUB_SHA}"
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 7ca3a70a..ae4d5588 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -224,6 +224,14 @@ repos:
language: system
files: ^atr/models/sql\.py$
+ - id: check-migrations-immutable
+ name: check migration immutability
+ description: Refuse to modify, rename, or delete an existing migrations/versions/ file, because shipped migrations are append only
+ entry: uv run --frozen python scripts/check_migrations_immutable.py
+ language: system
+ pass_filenames: false
+ always_run: true
+
- id: template-usage-linter
name: check template usage
description: Ensure that all templates are used and available w/o duplicates
diff --git a/scripts/check_migrations_immutable.py b/scripts/check_migrations_immutable.py
new file mode 100755
index 00000000..772f131e
--- /dev/null
+++ b/scripts/check_migrations_immutable.py
@@ -0,0 +1,120 @@
+#!/usr/bin/env python3
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import json
+import os
+import subprocess
+import sys
+from typing import Final
+
+_DEPLOY_BRANCHES: Final = ("main", "tertia")
+_MIGRATIONS_PREFIX: Final = "migrations/versions"
+_REMOTE_BRANCHES: Final = ("altera", "arm", "main", "sbp", "tertia")
+
+
+def main() -> None:
+ sys.exit(_run())
+
+
+def _git_lines(*arguments: str) -> list[str]:
+ process = subprocess.run(["git", *arguments], capture_output=True, text=True)
+ if process.returncode != 0:
+ detail = process.stderr.strip() or f"exit status {process.returncode}"
+ sys.exit(f"ERROR: git {' '.join(arguments)}: {detail}")
+ return [line for line in process.stdout.splitlines() if line.strip()]
+
+
+def _git_ok(*arguments: str) -> bool:
+ return subprocess.run(["git", *arguments], capture_output=True).returncode == 0
+
+
+def _github_base() -> str | None:
+ # GITHUB_BASE_REF is set only for pull_request events, so push events get no range check
+ if os.environ.get("GITHUB_ACTIONS") != "true":
+ return None
+ base_ref = os.environ.get("GITHUB_BASE_REF")
+ if not base_ref:
+ return None
+ remote_ref = f"origin/{base_ref}"
+ if not _git_ok("rev-parse", "--verify", "--quiet", remote_ref):
+ sys.exit(f"ERROR: {remote_ref} is not available; the workflow checkout must use fetch-depth: 0")
+ return _git_lines("merge-base", "HEAD", remote_ref)[0]
+
+
+def _push_base() -> str | None:
+ if os.environ.get("GITHUB_EVENT_NAME") != "push":
+ return None
+ event_path = os.environ.get("GITHUB_EVENT_PATH")
+ if not event_path:
+ return None
+ with open(event_path) as file:
+ before = json.load(file).get("before", "")
+ if before.strip("0") and _git_ok("rev-parse", "--verify", "--quiet", f"{before}^{{commit}}"):
+ return before
+ if os.environ.get("GITHUB_REF_NAME") in _DEPLOY_BRANCHES:
+ sys.exit(f"ERROR: the previous tip {before} of this deploy branch is not available to diff against")
+ if not _git_ok("rev-parse", "--verify", "--quiet", "origin/main"):
+ sys.exit("ERROR: origin/main is not available; the workflow checkout must use fetch-depth: 0")
+ return _git_lines("merge-base", "HEAD", "origin/main")[0]
+
+
+def _range_violations(base: str) -> list[str]:
+ filters = ["--name-status", "--diff-filter=DMRT", "--", _MIGRATIONS_PREFIX]
+ violations = _git_lines("log", "--format=%h", base + "..HEAD", *filters)
+ for entry in _git_lines("diff", base, "HEAD", *filters):
+ if entry not in violations:
+ violations.append(entry)
+ return violations
+
+
+def _run() -> int:
+ violations = _staged_violations()
+ base = _github_base()
+ if base is None:
+ base = _push_base()
+ if base is not None:
+ violations.extend(_range_violations(base))
+ if not violations:
+ return 0
+ print("Migrations are append only once pushed, and can never be modified, renamed, or deleted")
+ print("To correct a migration which has already shipped, add a new migration on top instead")
+ for violation in violations:
+ print(f" {violation}")
+ return 1
+
+
+def _shipped_paths() -> set[str]:
+ paths: set[str] = set()
+ for branch in _REMOTE_BRANCHES:
+ ref = f"origin/{branch}"
+ if not _git_ok("rev-parse", "--verify", "--quiet", ref):
+ continue
+ paths.update(_git_lines("ls-tree", "-r", "--name-only", ref, "--", _MIGRATIONS_PREFIX))
+ return paths
+
+
+def _staged_violations() -> list[str]:
+ entries = _git_lines("diff", "--cached", "--name-status", "--diff-filter=DMRT", "--", _MIGRATIONS_PREFIX)
+ if not entries:
+ return []
+ shipped = _shipped_paths()
+ return [entry for entry in entries if entry.split("\t")[1] in shipped]
+
+
+if __name__ == "__main__":
+ main()
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tooling.apache.org
For additional commands, e-mail: commits-help@tooling.apache.org