diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4be793eba..cda49d2f2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 @@ -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 diff --git a/demo/config/database.yml b/demo/config/database.yml index 3fba25c7e..77e21938f 100644 --- a/demo/config/database.yml +++ b/demo/config/database.yml @@ -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 } %> diff --git a/scripts/check_migrations.sh b/scripts/check_migrations.sh new file mode 100644 index 000000000..c79a09103 --- /dev/null +++ b/scripts/check_migrations.sh @@ -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