Skip to content

Add tests for the names of migration files #1356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,20 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

- name: Get latest tag
id: get-latest-tag
run: gh release list --limit 1 --json tagName --jq '"latest_tag="+.[0].tagName' >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Run tests
run: uv run pytest --cov=procrastinate --cov-branch
env:
COVERAGE_FILE: ".coverage.${{ matrix.python-version }}"
PGHOST: localhost
PGUSER: postgres
PGPASSWORD: postgres
LATEST_TAG: ${{ steps.get-latest-tag.outputs.latest_tag }}

- name: Store coverage file
uses: actions/upload-artifact@v4
Expand Down
93 changes: 93 additions & 0 deletions tests/migration/test_migration.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import annotations

import contextlib
import os
import pathlib
import subprocess
import warnings

import packaging.version
import pytest
from django.core import management
from django.db import connection
Expand Down Expand Up @@ -98,3 +101,93 @@ def test_django_migrations_run_properly(django_db):

def test_no_missing_django_migration(django_db):
management.call_command("makemigrations", "procrastinate", dry_run=True, check=True)


@pytest.fixture(scope="module")
def latest_version() -> packaging.version.Version:
if latest_tag := os.environ.get("LATEST_TAG"):
return packaging.version.Version(latest_tag)

if "CI" in os.environ:
raise ValueError("Cannot fetch latest tag in CI unless LATEST_TAG is set")

# If we're not in the CI, we can accept loosing a bit of time to fetch the latest tag
try:
subprocess.check_call(["git", "fetch", "--tags"])
out = subprocess.check_output(["git", "tag", "--list"], text=True)
except subprocess.CalledProcessError as exc:
raise ValueError("Cannot fetch latest tag") from exc

return max(packaging.version.Version(tag) for tag in out.splitlines())


migration_files = sorted(
(pathlib.Path(__file__).parents[2] / "procrastinate" / "sql" / "migrations").glob(
"*.sql"
)
)


@pytest.fixture(scope="module")
def new_migrations(latest_version) -> set[pathlib.Path]:
# git diff latest_version..HEAD --name-only --diff-filter=A

try:
out = subprocess.check_output(
[
"git",
"diff",
f"{latest_version}..HEAD",
"--name-only",
"--diff-filter=A",
],
text=True,
)
except subprocess.CalledProcessError as exc:
raise ValueError("Cannot fetch new migrations") from exc

return {pathlib.Path(path) for path in out.splitlines()}


@pytest.mark.parametrize(
"migration", [pytest.param(m, id=m.name) for m in migration_files]
)
def test_migration_properly_named(
migration: pathlib.Path,
latest_version: packaging.version.Version,
new_migrations: set[pathlib.Path],
):
# migration is:
# pathlib.Path("..." / "03.00.00_01_pre_cancel_notification.sql")

migration_name_parts = migration.stem.split("_", 3)
version_str, index_str, *pre_post_name = migration_name_parts

mig_version = packaging.version.Version(version_str)

next_minor = packaging.version.Version(
f"{latest_version.major}.{latest_version.minor + 1}.0"
)

if migration in new_migrations:
assert mig_version == next_minor
else:
assert mig_version <= latest_version

if mig_version < packaging.version.Version("3.0.0"):
pre_post = "pre"
name = pre_post_name[0]
else:
pre_post, name = pre_post_name

index = int(index_str)
if pre_post == "pre":
assert 1 <= index < 50
elif pre_post == "post":
assert 50 <= index
else:
assert False, f"Invalid migration name: expecting 'pre' or 'post': {pre_post}"

assert name == name.lower()
assert "-" not in name
assert " " not in name
Loading