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
49 changes: 28 additions & 21 deletions .github/workflows/schema-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,35 @@ jobs:
chmod +x dbmate
sudo mv dbmate /usr/local/bin/

# Currently skipping since pg_dump doesn't produce consistent output anymore
# - name: Validate schema is in sync with migrations
# run: |
# # Apply migrations, dumps schema
# dbmate up
- name: Validate schema is in sync with migrations
run: |
# Apply migrations
dbmate up

# Force dump in case problems were ignored
DBMATE_SCHEMA_FILE=ci-dump.sql dbmate dump

# Check if the schema file has changed
# Define cleanup function to remove comments and psql meta-commands
clean_sql() {
sed -e '/^--/d' -e '/^\\restrict/d' -e '/^\\unrestrict/d' "$1"
}

# # Check if the schema file has changed
# if git diff --exit-code database.sql; then
# echo "✅ Schema validation passed - database.sql is in sync with migrations"
# else
# echo "::error::Schema file is out of sync with migrations!"
# echo ""
# echo "The database.sql file does not match what the migrations produce."
# echo ""
# echo "To fix this, run a postgres instance locally."
# echo "Then apply the migrations to a clean database:"
# echo " dbmate up"
# echo ""
# echo "Then include the updated schema in your commit:"
# echo " git add database.sql && git commit -m 'Update schema'"
# exit 1
# fi
if diff -u <(clean_sql database.sql) <(clean_sql ci-dump.sql); then
echo "✅ Schema validation passed - database.sql is in sync with migrations"
else
echo "::error::Schema file is out of sync with migrations!"
echo ""
echo "The database.sql file does not match what the migrations produce."
echo ""
echo "To fix this, run a postgres instance locally."
echo "Then apply the migrations to a clean database:"
echo " dbmate up"
echo ""
echo "Then include the updated schema in your commit:"
echo " git add database.sql && git commit -m 'Update schema'"
exit 1
fi

- name: Test that down migrations work
run: |
Expand Down
7 changes: 7 additions & 0 deletions database.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
\restrict ofpmBZgaMbuYf558teg7MYcyPbiwJkZyYgREoiBTduRUlCZa72Go911eC5LHqgI

-- Dumped from database version 16.11 (Ubuntu 16.11-1.pgdg24.04+1)
-- Dumped by pg_dump version 16.11 (Ubuntu 16.11-1.pgdg24.04+1)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
Expand Down Expand Up @@ -1691,6 +1696,8 @@ ALTER TABLE ONLY public.verified_contracts
-- PostgreSQL database dump complete
--

\unrestrict ofpmBZgaMbuYf558teg7MYcyPbiwJkZyYgREoiBTduRUlCZa72Go911eC5LHqgI


--
-- Dbmate schema migrations
Expand Down
9 changes: 9 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ def initialize_schema(connection, schema_name="public"):
with open('./database.sql', 'r') as schema_file:
schema = schema_file.read()

# Remove psql meta-commands that are not valid SQL
# Filter out lines starting with \restrict, and \unrestrict
schema_lines = schema.split('\n')
filtered_lines = [
line for line in schema_lines
if not line.strip().startswith(('\\restrict', '\\unrestrict'))
]
schema = '\n'.join(filtered_lines)

with connection.cursor() as cursor:
cursor.execute(schema)
# Set the schema search path because `database.sql` resets it
Expand Down