Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python
3.8 KB
Raw Blame
#!/bin/sh
#
# Pre-commit hook:
#   1. Runs the migration checks when migration-related files are staged:
#      the static PostgreSQL compatibility scan, and the behavioural tests
#      that upgrade real databases from older releases.
#   2. Keeps the VERSION file in sync with the README version badge so the
#      About page in the app never lags behind a release commit.
#
# Install: ln -sf ../../scripts/pre-commit .git/hooks/pre-commit

STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)

# ---------------------------------------------------------------------------
# 1. Migration checks
# ---------------------------------------------------------------------------
if echo "$STAGED_FILES" | grep -qE '^(src/init_db\.py|src/utils/database\.py|src/models/)'; then
    echo "Migration files changed — running PostgreSQL compatibility checks..."
    python tests/test_migration_compatibility.py
    if [ $? -ne 0 ]; then
        echo ""
        echo "Pre-commit hook FAILED: PostgreSQL migration compatibility issues found."
        echo "Fix the issues above before committing."
        exit 1
    fi
    echo "All migration compatibility checks passed."

    # The static scan only reads the source. These actually upgrade databases
    # created by older releases and assert nothing was lost on the way, which
    # is the check that would have caught issue #379.
    if python -c "import pytest" >/dev/null 2>&1; then
        echo "Running migration behaviour tests (upgrade path, ~20s)..."
        python -m pytest -q \
            tests/test_upgrade_path.py \
            tests/test_migration_drop_not_null.py \
            tests/test_migration_infrastructure.py
        if [ $? -ne 0 ]; then
            echo ""
            echo "Pre-commit hook FAILED: a migration test did not pass."
            echo "Upgrading from an older release loses data or does not complete."
            echo "Set TEST_DATABASE_URI to a PostgreSQL URL to include that backend."
            exit 1
        fi
        echo "All migration behaviour tests passed."
    else
        echo "pytest not installed — skipping migration behaviour tests."
        echo "CI still runs them, but installing pytest catches problems earlier."
    fi
fi

# ---------------------------------------------------------------------------
# 2. VERSION-file sync
# ---------------------------------------------------------------------------
# Source of truth: the version inside the README shields.io badge
#   https://img.shields.io/badge/version-X.Y.Z[-suffix]-<colour>.svg
# When that badge moves (typical release commit edit), or when somebody
# touches VERSION directly, we make sure both files end up with the same
# value and re-stage them so the commit captures it.
if echo "$STAGED_FILES" | grep -qE '^(README\.md|VERSION)$'; then
    if [ -f README.md ] && [ -f VERSION ]; then
        # Extract version from README badge. Accepts X.Y.Z or X.Y.Z-suffix.
        # A literal dash in the version (e.g. the "-alpha" suffix) is escaped
        # as "--" in the shields.io badge so the badge renders instead of
        # 404ing; collapse it back to a single dash for the VERSION file.
        README_VER=$(grep -oE 'shields\.io/badge/version-[0-9]+\.[0-9]+\.[0-9]+[A-Za-z0-9.-]*-[a-z]+\.svg' README.md \
            | head -1 \
            | sed -E 's|shields\.io/badge/version-([0-9]+\.[0-9]+\.[0-9]+[A-Za-z0-9.-]*)-[a-z]+\.svg|\1|' \
            | sed 's|--|-|g')

        if [ -z "$README_VER" ]; then
            echo "Version-sync skipped: could not parse version from README badge."
        else
            EXPECTED="v${README_VER}"
            CURRENT=$(tr -d '[:space:]' < VERSION)

            if [ "$CURRENT" != "$EXPECTED" ]; then
                echo "Syncing VERSION file: $CURRENT → $EXPECTED (from README badge)"
                printf '%s\n' "$EXPECTED" > VERSION
                git add VERSION
            fi
        fi
    fi
fi