Skip to content

Commit

Permalink
Test migrations reverse cleanly
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHeath committed Dec 13, 2023
1 parent ae74482 commit 30bda25
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ jobs:
- run: chrome --version
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Update .ruby-version with matrix value
run: echo "${{ matrix.ruby }}" >| .ruby-version
# Dependencies
Expand Down Expand Up @@ -181,6 +183,10 @@ jobs:
--format PreDocumentationFormatter \
spec/generators
- name: Check generated migrations reverse cleanly
run: |
script/check_migrations.sh
# Archive
- name: Archive system spec screenshots
uses: actions/upload-artifact@v3
Expand Down
1 change: 1 addition & 0 deletions demo/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ default: &default
adapter: postgresql
encoding: unicode
host: localhost
user: postgres
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 20 } %>
Expand Down
46 changes: 46 additions & 0 deletions scripts/check_migrations.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

set -xuo pipefail
# Check for new migrations
versions=$(git diff --name-status remotes/origin/main demo/db/migrate/ | grep '^A' | cut -f2 | sed 's/[^0-9]//g' | sort -n --reverse)
set -Ee # Enable failed command checks after checking

if [ -z "$versions" ] ; then
echo "No changes to migrations."
exit 0
fi

# Replace schema.rb with one from main branch
rm demo/db/schema.rb
git checkout demo/db/schema.rb db/

# Create database with schema from main branch
bundle exec rake db:drop db:create
bundle exec rake db:schema:load

# Apply migrations and check that the generated schema.rb now matches the committed one
bundle exec rake db:migrate
if ! git diff --ignore-all-space --exit-code "HEAD" -- demo/db/schema.rb ; then
echo "Generated schema.rb does not match committed schema.rb"
exit 1
fi

# Run all migrations in reverse
for version in $versions
do
if grep -E 'raise (ActiveRecord::)?IrreversibleMigration' < "db/migrate/$version"* ; then
# One of the migrations explicitly uses IrreversibleMigration.
# Skip check for valid reversing
echo "Irreversible migration $version means we can't check for valid schema.rb; skipping"
exit 0
else
bundle exec rake db:migrate:down "VERSION=${version}"
fi
done

# Compare the resulting schema with
bundle exec rake db:schema:dump
if ! git diff --ignore-all-space --exit-code "$(git merge-base HEAD main)" -- demo/db/schema.rb ; then
echo "Some migrations could not be reversed cleanly, but do not reference IrreversibleMigration"
exit 1
fi

0 comments on commit 30bda25

Please sign in to comment.