Skip to content

Check pending migrations without creating schema_migrations - #375

Open
cole-robertson wants to merge 1 commit into
basecamp:mainfrom
cole-robertson:pending-check-does-not-write
Open

cole-robertson wants to merge 1 commit into
basecamp:mainfrom
cole-robertson:pending-check-does-not-write

Conversation

@cole-robertson

Copy link
Copy Markdown

Motivation

If the process running create_tenant is killed while the schema dump is loading (SIGKILL, the OOM killer), it runs no rescue, so the database file is left with some tables and no schema_migrations. tenant_exist? returns true for it.

db:migrate repairs that tenant: DatabaseTasks#migrate sees no schema_migrations, loads the schema, and the tenant is fine. But if anything opens the tenant first, such as a request or a retried create_tenant(..., if_not_exists: true) with a block, the tenant can no longer be repaired:

  1. _create_tenanted_pool checks for pending migrations with pool.migration_context.open.pending_migrations and raises PendingMigrationError, as it should.
  2. MigrationContext#open builds a Migrator, and Migrator#initialize creates schema_migrations (empty) and ar_internal_metadata.
  3. On the next db:migrate, DatabaseTasks#migrate finds schema_migrations, skips the schema load, and runs every migration over the half-loaded tables, which fails with table "users" already exists.

migrate_all doesn't rescue per tenant, so that one tenant also stops db:migrate for every tenant after it.

Details

The pending migrations are now computed from MigrationContext#get_all_versions, which only reads schema_migrations and returns [] when the table is missing, rather than from a Migrator. The pool check finds the same pending migrations as before and raises the same error, but leaves the database as it found it, so db:migrate still loads the schema.

The new test in database_tasks_test.rb sets up a half-loaded tenant, opens it, and then migrates it. It fails on main with table "users" already exists and passes with this change. I also ran a script that kills a real create_tenant partway through loading a two-table schema file and then sends one request: on 0.9.0 the following migrate_tenant failed 3 of 3 times, and with this change it repaired the tenant 3 of 3 times. An uninterrupted run behaves the same either way.

#154 drops the database when creation raises; this covers a process that was killed, where no rescue runs.

🤖 Generated with Claude Code

`_create_tenanted_pool` called `migration_context.open.pending_migrations`. Building a
`Migrator` creates `schema_migrations` and `ar_internal_metadata`, so opening a tenant whose
schema load had been interrupted left an empty `schema_migrations` behind. `DatabaseTasks#migrate`
then took the tenant for initialized, skipped the schema load, and ran every migration over the
tables the interrupted load had already created.

Compute the pending migrations from `get_all_versions` instead, which only reads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 02:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approved

No active review issues are reported for the change to make tenant pending-migration checks read-only.

No open issues 🎉

What changed in this PR

Makes tenant pending-migration checks read-only so db:migrate can still repair tenants whose schema loading was interrupted, even after they have been opened.

Changes:

  • Computes pending migrations without constructing a Migrator or creating schema_migrations.
  • Adds a regression test covering tenant access followed by repair after an interrupted schema load.
  • Documents the fix in the changelog.

[!TIP]
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

File Description
test/​unit/​database_tasks_test.rb Tests that a partially initialized tenant remains repairable after access raises a pending-migration error.
lib/​active_record/​tenanted/​tenant.rb Checks migration versions without creating migration metadata tables.
CHANGELOG.md Records the interrupted tenant-creation recovery fix.

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants