From 32318359348c2cb9b2ccb564dc69029c3e9c5757 Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Wed, 25 Mar 2026 13:37:31 +0000 Subject: [PATCH 1/5] Action to test database migrations --- .github/workflows/migration-test.yml | 84 ++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 .github/workflows/migration-test.yml diff --git a/.github/workflows/migration-test.yml b/.github/workflows/migration-test.yml new file mode 100644 index 000000000..d08885a60 --- /dev/null +++ b/.github/workflows/migration-test.yml @@ -0,0 +1,84 @@ +name: Database migration tests +on: + push: + paths: + - 'db/migrate/**' + pull_request: + paths: + - 'db/migrate/**' + +jobs: + test: + runs-on: ubuntu-latest + env: + DB_HOST: localhost + DB_NAME: tess + DB_USER: tess + DB_PASSWORD: password + SECRET_BASE_KEY: test_key + RAILS_ENV: test + REDIS_TEST_URL: redis://localhost:6379/0 + services: + postgres: + image: postgres + env: + POSTGRES_DB: ${{ env.DB_NAME }} + POSTGRES_USER: ${{ env.DB_USER }} + POSTGRES_PASSWORD: ${{ env.DB_PASSWORD }} + ports: + - 5432:5432 + options: >- + --health-cmd "pg_isready" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + redis: + image: redis + options: >- + --health-cmd "redis-cli ping" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 6379:6379 + steps: + - name: Install system dependencies + run: | + sudo apt-get update + sudo apt-get install imagemagick + - name: Check out code + uses: actions/checkout@v4 + - name: Install Ruby & gems + uses: ruby/setup-ruby@v1 + with: + bundler-cache: true # runs 'bundle install' and caches installed gems automatically + - name: Configure + run: | + cp test/config/test_tess.yml config/tess.yml + cp config/secrets.github.yml config/secrets.yml + cp config/ingestion.example.yml config/ingestion.yml + - name: Checkout previous schema + # Checks out previous schema.rb and temporarily renames migrations + if: github.event.before != '0000000000000000000000000000000000000000' + run: | + git fetch origin ${{ github.event.before }} + git checkout ${{ github.event.before }} -- db/schema.rb + mv db/migrate db/migrate.bkp + git checkout ${{ github.event.before }} -- db/migrate + - name: Load previous database schema + run: bundle exec rake db:test:prepare + - name: Restore migrations + # Restore the renamed migrations + if: github.event.before != '0000000000000000000000000000000000000000' + run: | + rm -rf db/migrate + mv db/migrate.bkp db/migrate + - name: Set up Node + uses: actions/setup-node@v4 + with: + node-version: '20.x' + cache: 'yarn' + - name: Install JS dependencies + run: yarn install --frozen-lockfile --non-interactive + - name: Run migrations + run: bundle exec rails db:migrate From 0a8c24fb524858ec90e60430c4e5ddcff2751d16 Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Wed, 25 Mar 2026 13:58:55 +0000 Subject: [PATCH 2/5] Rename --- .github/workflows/migration-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/migration-test.yml b/.github/workflows/migration-test.yml index d08885a60..b3ae67dfb 100644 --- a/.github/workflows/migration-test.yml +++ b/.github/workflows/migration-test.yml @@ -8,7 +8,7 @@ on: - 'db/migrate/**' jobs: - test: + migrate: runs-on: ubuntu-latest env: DB_HOST: localhost From c5980a1f25196650be26113c4caed28df3d7e38a Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Wed, 25 Mar 2026 14:52:42 +0000 Subject: [PATCH 3/5] For PRs, get SHA from `base` --- .github/workflows/migration-test.yml | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/workflows/migration-test.yml b/.github/workflows/migration-test.yml index b3ae67dfb..b16fec19a 100644 --- a/.github/workflows/migration-test.yml +++ b/.github/workflows/migration-test.yml @@ -18,6 +18,8 @@ jobs: SECRET_BASE_KEY: test_key RAILS_ENV: test REDIS_TEST_URL: redis://localhost:6379/0 + PREV_SHA: ${{ github.event.pull_request.base.sha || github.event.before }} + services: postgres: image: postgres @@ -59,17 +61,17 @@ jobs: cp config/ingestion.example.yml config/ingestion.yml - name: Checkout previous schema # Checks out previous schema.rb and temporarily renames migrations - if: github.event.before != '0000000000000000000000000000000000000000' + if: env.PREV_SHA != '0000000000000000000000000000000000000000' run: | - git fetch origin ${{ github.event.before }} - git checkout ${{ github.event.before }} -- db/schema.rb + git fetch origin ${{ env.PREV_SHA }} + git checkout ${{ env.PREV_SHA }} -- db/schema.rb mv db/migrate db/migrate.bkp - git checkout ${{ github.event.before }} -- db/migrate + git checkout ${{ env.PREV_SHA }} -- db/migrate - name: Load previous database schema run: bundle exec rake db:test:prepare - name: Restore migrations # Restore the renamed migrations - if: github.event.before != '0000000000000000000000000000000000000000' + if: env.PREV_SHA != '0000000000000000000000000000000000000000' run: | rm -rf db/migrate mv db/migrate.bkp db/migrate From f43c0b2e9d89cd07eba2ee4aeeef1fc661e2bace Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Wed, 25 Mar 2026 13:39:05 +0000 Subject: [PATCH 4/5] Testing GH action --- db/migrate/20260325133804_good_migration.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20260325133804_good_migration.rb diff --git a/db/migrate/20260325133804_good_migration.rb b/db/migrate/20260325133804_good_migration.rb new file mode 100644 index 000000000..1ef55a60b --- /dev/null +++ b/db/migrate/20260325133804_good_migration.rb @@ -0,0 +1,5 @@ +class GoodMigration < ActiveRecord::Migration[7.2] + def change + add_column :materials, :a_good_field, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index eba25c8d5..20ea7e1c0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2026_03_10_163512) do +ActiveRecord::Schema[7.2].define(version: 2026_03_25_133804) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -388,6 +388,7 @@ t.string "fields", default: [], array: true t.boolean "visible", default: true t.bigint "space_id" + t.string "a_good_field" t.index ["content_provider_id"], name: "index_materials_on_content_provider_id" t.index ["slug"], name: "index_materials_on_slug", unique: true t.index ["space_id"], name: "index_materials_on_space_id" From 553a62d0dfbba5d2ed9a1fe3cc4d5b23d4e3333d Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Wed, 25 Mar 2026 13:40:18 +0000 Subject: [PATCH 5/5] This migration should fail --- db/migrate/20260325133939_bad_migration.rb | 5 +++++ db/schema.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20260325133939_bad_migration.rb diff --git a/db/migrate/20260325133939_bad_migration.rb b/db/migrate/20260325133939_bad_migration.rb new file mode 100644 index 000000000..9542db8a7 --- /dev/null +++ b/db/migrate/20260325133939_bad_migration.rb @@ -0,0 +1,5 @@ +class BadMigration < ActiveRecord::Migration[7.2] + def change + raise ':(' + end +end diff --git a/db/schema.rb b/db/schema.rb index 20ea7e1c0..c39d74204 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2026_03_25_133804) do +ActiveRecord::Schema[7.2].define(version: 2026_03_25_133939) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql"