Skip to content

Commit 896390c

Browse files
committed
add GitHub Actions workflow for database schema validation
1 parent da6494d commit 896390c

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Schema Validation
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- database.sql
7+
- migrations/**
8+
- .github/workflows/schema-validation.yml
9+
10+
jobs:
11+
validate-schema:
12+
name: Validate Database Schema
13+
runs-on: ubuntu-latest
14+
env:
15+
DATABASE_URL: postgres://postgres:password@localhost:5432/test_db?sslmode=disable
16+
DBMATE_MIGRATIONS_DIR: ./migrations
17+
DBMATE_SCHEMA_FILE: ./database.sql
18+
19+
services:
20+
postgres:
21+
image: postgres:15
22+
env:
23+
POSTGRES_PASSWORD: password
24+
POSTGRES_USER: postgres
25+
POSTGRES_DB: test_db
26+
options: >-
27+
--health-cmd pg_isready
28+
--health-interval 10s
29+
--health-timeout 5s
30+
--health-retries 5
31+
ports:
32+
- 5432:5432
33+
34+
steps:
35+
- name: Checkout sources
36+
uses: actions/checkout@v4
37+
38+
- name: Install dbmate
39+
run: |
40+
curl -fsSL -o dbmate https://github.com/amacneil/dbmate/releases/latest/download/dbmate-linux-amd64
41+
chmod +x dbmate
42+
sudo mv dbmate /usr/local/bin/
43+
44+
- name: Validate schema is in sync with migrations
45+
run: |
46+
# Apply migrations, dumps schema
47+
dbmate up
48+
49+
# Check if the schema file has changed
50+
if git diff --exit-code database.sql; then
51+
echo "✅ Schema validation passed - database.sql is in sync with migrations"
52+
else
53+
echo "::error::Schema file is out of sync with migrations!"
54+
echo ""
55+
echo "The database.sql file does not match what the migrations produce."
56+
echo ""
57+
echo "To fix this, run a postgres instance locally."
58+
echo "Then apply the migrations to a clean database:"
59+
echo " dbmate up"
60+
echo ""
61+
echo "Then include the updated schema in your commit:"
62+
echo " git add database.sql && git commit -m 'Update schema'"
63+
exit 1
64+
fi

0 commit comments

Comments
 (0)