diff --git a/.github/workflows/schema-validation.yml b/.github/workflows/schema-validation.yml index 8fc1641..75b11ec 100644 --- a/.github/workflows/schema-validation.yml +++ b/.github/workflows/schema-validation.yml @@ -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: | diff --git a/database.sql b/database.sql index 4ca376f..6e26571 100644 --- a/database.sql +++ b/database.sql @@ -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; @@ -1691,6 +1696,8 @@ ALTER TABLE ONLY public.verified_contracts -- PostgreSQL database dump complete -- +\unrestrict ofpmBZgaMbuYf558teg7MYcyPbiwJkZyYgREoiBTduRUlCZa72Go911eC5LHqgI + -- -- Dbmate schema migrations diff --git a/tests/helpers.py b/tests/helpers.py index 3852bd2..2132839 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -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