|
| 1 | +name: DB Integrity |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + |
| 6 | +jobs: |
| 7 | + db-integrity: |
| 8 | + name: Migration smoke test + seed freshness |
| 9 | + runs-on: ubuntu-latest |
| 10 | + |
| 11 | + services: |
| 12 | + postgres: |
| 13 | + image: postgres:16-alpine |
| 14 | + env: |
| 15 | + POSTGRES_USER: power2plant |
| 16 | + POSTGRES_PASSWORD: power2plant |
| 17 | + POSTGRES_DB: power2plant |
| 18 | + options: >- |
| 19 | + --health-cmd pg_isready |
| 20 | + --health-interval 5s |
| 21 | + --health-timeout 5s |
| 22 | + --health-retries 5 |
| 23 | + ports: |
| 24 | + - 5432:5432 |
| 25 | + |
| 26 | + env: |
| 27 | + DATABASE_URL: postgresql://power2plant:power2plant@localhost:5432/power2plant |
| 28 | + |
| 29 | + steps: |
| 30 | + - uses: actions/checkout@v4 |
| 31 | + |
| 32 | + - uses: pnpm/action-setup@v4 |
| 33 | + |
| 34 | + - uses: actions/setup-node@v4 |
| 35 | + with: |
| 36 | + node-version: 22 |
| 37 | + cache: pnpm |
| 38 | + |
| 39 | + - run: pnpm install --frozen-lockfile |
| 40 | + |
| 41 | + - run: pnpm exec prisma generate |
| 42 | + |
| 43 | + - name: Load seed.sql |
| 44 | + run: psql "$DATABASE_URL" < db/seed.sql |
| 45 | + |
| 46 | + - name: Apply pending migrations |
| 47 | + run: | |
| 48 | + BEFORE_TS=$(date -u +"%Y-%m-%dT%H:%M:%SZ") |
| 49 | + echo "BEFORE_TS=$BEFORE_TS" >> "$GITHUB_ENV" |
| 50 | + pnpm exec prisma migrate deploy |
| 51 | +
|
| 52 | + - name: Check seed.sql freshness |
| 53 | + run: | |
| 54 | + APPLIED=$(psql "$DATABASE_URL" -t -c \ |
| 55 | + "SELECT count(*) FROM \"_prisma_migrations\" WHERE started_at > '$BEFORE_TS' AND finished_at IS NOT NULL AND logs IS NULL;" \ |
| 56 | + | tr -d ' ') |
| 57 | + echo "Migrations applied on top of seed.sql: $APPLIED" |
| 58 | + if [ "$APPLIED" -gt 0 ]; then |
| 59 | + echo "::warning::seed.sql is stale — $APPLIED migration(s) missing. Run \`pnpm db:dump\` and commit db/seed.sql." |
| 60 | + if [[ "${{ github.head_ref }}" == release/* ]]; then |
| 61 | + echo "::error::seed.sql must include all migrations on release branches. Run \`pnpm db:dump\` and commit." |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | + fi |
| 65 | +
|
| 66 | + upgrade-path: |
| 67 | + name: Upgrade path from last release |
| 68 | + runs-on: ubuntu-latest |
| 69 | + |
| 70 | + services: |
| 71 | + postgres: |
| 72 | + image: postgres:16-alpine |
| 73 | + env: |
| 74 | + POSTGRES_USER: power2plant |
| 75 | + POSTGRES_PASSWORD: power2plant |
| 76 | + POSTGRES_DB: power2plant |
| 77 | + options: >- |
| 78 | + --health-cmd pg_isready |
| 79 | + --health-interval 5s |
| 80 | + --health-timeout 5s |
| 81 | + --health-retries 5 |
| 82 | + ports: |
| 83 | + - 5432:5432 |
| 84 | + |
| 85 | + env: |
| 86 | + DATABASE_URL: postgresql://power2plant:power2plant@localhost:5432/power2plant |
| 87 | + |
| 88 | + steps: |
| 89 | + - uses: actions/checkout@v4 |
| 90 | + with: |
| 91 | + fetch-depth: 0 |
| 92 | + |
| 93 | + - uses: pnpm/action-setup@v4 |
| 94 | + |
| 95 | + - uses: actions/setup-node@v4 |
| 96 | + with: |
| 97 | + node-version: 22 |
| 98 | + cache: pnpm |
| 99 | + |
| 100 | + - run: pnpm install --frozen-lockfile |
| 101 | + |
| 102 | + - run: pnpm exec prisma generate |
| 103 | + |
| 104 | + - name: Load last release seed.sql |
| 105 | + run: | |
| 106 | + LAST_TAG=$(git tag --sort=-version:refname | grep -E '^v[0-9]' | head -1) |
| 107 | + if [ -z "$LAST_TAG" ]; then |
| 108 | + echo "No release tag found — skipping" |
| 109 | + exit 0 |
| 110 | + fi |
| 111 | + echo "Testing upgrade path from $LAST_TAG" |
| 112 | + echo "LAST_TAG=$LAST_TAG" >> "$GITHUB_ENV" |
| 113 | + git show "$LAST_TAG:db/seed.sql" | psql "$DATABASE_URL" |
| 114 | +
|
| 115 | + - name: Apply current migrations on top |
| 116 | + run: pnpm exec prisma migrate deploy |
0 commit comments