Skip to content
Merged
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
115 changes: 115 additions & 0 deletions .github/workflows/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
name: Database

on:
pull_request:
paths: ["db/**", ".github/workflows/database.yml"]
push:
branches: [main]
paths: ["db/**", ".github/workflows/database.yml"]
tags: ["v*"]

env:
FLYWAY_IMAGE: flyway/flyway:13.8.1

jobs:
test:
name: Test migrations on a fresh database
runs-on: ubuntu-latest
services:
postgres:
# Must match production's Postgres version.
image: supabase/postgres:17.6.1.062
env:
POSTGRES_PASSWORD: postgres
ports: ["5432:5432"]
options: >-
--health-cmd "pg_isready -U postgres -h localhost"
--health-interval 5s
--health-timeout 5s
--health-retries 20
steps:
- uses: actions/checkout@v5
with:
fetch-depth: 0

- name: Check merged migrations were not changed
if: github.event_name == 'pull_request'
run: |
changed=$(git diff --name-only --diff-filter=MDR \
"origin/${{ github.base_ref }}...HEAD" -- db/migrations)
if [ -n "$changed" ]; then
echo "::error::Merged migrations must not be edited, renamed or deleted. Add a new migration instead."
echo "$changed"
exit 1
fi

- name: Run all migrations
env:
FLYWAY_URL: jdbc:postgresql://localhost:5432/postgres
FLYWAY_USER: postgres
FLYWAY_PASSWORD: postgres
run: |
docker run --rm --network host -v "$PWD/db:/flyway/project" -w /flyway/project \
-e FLYWAY_URL -e FLYWAY_USER -e FLYWAY_PASSWORD \
"$FLYWAY_IMAGE" -configFiles=flyway.toml migrate

migrate-dev:
name: Migrate dev database
if: github.ref == 'refs/heads/main'
needs: test
runs-on: ubuntu-latest
environment: dev
concurrency: { group: migrate-dev, cancel-in-progress: false }
env:
FLYWAY_URL: ${{ secrets.FLYWAY_URL }}
FLYWAY_USER: ${{ secrets.FLYWAY_USER }}
FLYWAY_PASSWORD: ${{ secrets.FLYWAY_PASSWORD }}
steps:
- uses: actions/checkout@v5

- name: Migrate
run: |
if [ -z "$FLYWAY_URL" ]; then
echo "::warning::The dev environment has no FLYWAY_URL secret yet. Skipping."
exit 0
fi
docker run --rm -v "$PWD/db:/flyway/project" -w /flyway/project \
-e FLYWAY_URL -e FLYWAY_USER -e FLYWAY_PASSWORD \
"$FLYWAY_IMAGE" -configFiles=flyway.toml migrate

migrate-prod:
name: Migrate production database
if: startsWith(github.ref, 'refs/tags/v')
needs: test
runs-on: ubuntu-latest
environment: production
concurrency: { group: migrate-prod, cancel-in-progress: false }
env:
FLYWAY_URL: ${{ secrets.FLYWAY_URL }}
FLYWAY_USER: ${{ secrets.FLYWAY_USER }}
FLYWAY_PASSWORD: ${{ secrets.FLYWAY_PASSWORD }}
steps:
- uses: actions/checkout@v5

- name: Check production has been baselined
run: |
set -o pipefail
if [ -z "$FLYWAY_URL" ]; then
echo "::error::The production environment has no FLYWAY_URL secret."
exit 1
fi
version=$(docker run --rm -v "$PWD/db:/flyway/project" -w /flyway/project \
-e FLYWAY_URL -e FLYWAY_USER -e FLYWAY_PASSWORD \
"$FLYWAY_IMAGE" -configFiles=flyway.toml info -outputType=json \
| jq -r '.schemaVersion // empty')
if [ -z "$version" ]; then
echo "::error::Production has no Flyway history. Run 'flyway baseline' on it first (kb/database_migrations.md), or the baseline migration would run against the live schema."
exit 1
fi
echo "Production is at version $version"

- name: Migrate
run: |
docker run --rm -v "$PWD/db:/flyway/project" -w /flyway/project \
-e FLYWAY_URL -e FLYWAY_USER -e FLYWAY_PASSWORD \
"$FLYWAY_IMAGE" -configFiles=flyway.toml migrate
2 changes: 2 additions & 0 deletions db/callbacks/beforeEachMigrate.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Flyway switches search_path to its own schema; unqualified names in migrations mean public.
set search_path = public, extensions;
14 changes: 14 additions & 0 deletions db/flyway.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[flyway]
locations = ["filesystem:migrations", "filesystem:callbacks"]

validateMigrationNaming = true
cleanDisabled = true
# Branches merge in any order, so a migration can be older than one already applied.
outOfOrder = true

baselineVersion = "20261016.0000"
baselineDescription = "baseline"

[environments.default]
# Not public: everything in public is exposed through Supabase's REST API.
schemas = ["flyway"]
Loading
Loading