Repository navigation
Check pending migrations without creating schema_migrations - #375
Open
cole-robertson wants to merge 1 commit into
Open
cole-robertson wants to merge 1 commit into
cole-robertson wants to merge 1 commit into
Conversation
`_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>
There was a problem hiding this comment.
🟢 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
Migratoror creatingschema_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 rungh pr ready --undo.
Click "Ready for review" or rungh pr readyto 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
If the process running
create_tenantis killed while the schema dump is loading (SIGKILL, the OOM killer), it runs norescue, so the database file is left with some tables and noschema_migrations.tenant_exist?returns true for it.db:migraterepairs that tenant:DatabaseTasks#migratesees noschema_migrations, loads the schema, and the tenant is fine. But if anything opens the tenant first, such as a request or a retriedcreate_tenant(..., if_not_exists: true)with a block, the tenant can no longer be repaired:_create_tenanted_poolchecks for pending migrations withpool.migration_context.open.pending_migrationsand raisesPendingMigrationError, as it should.MigrationContext#openbuilds aMigrator, andMigrator#initializecreatesschema_migrations(empty) andar_internal_metadata.db:migrate,DatabaseTasks#migratefindsschema_migrations, skips the schema load, and runs every migration over the half-loaded tables, which fails withtable "users" already exists.migrate_alldoesn't rescue per tenant, so that one tenant also stopsdb:migratefor every tenant after it.Details
The pending migrations are now computed from
MigrationContext#get_all_versions, which only readsschema_migrationsand returns[]when the table is missing, rather than from aMigrator. The pool check finds the same pending migrations as before and raises the same error, but leaves the database as it found it, sodb:migratestill loads the schema.The new test in
database_tasks_test.rbsets up a half-loaded tenant, opens it, and then migrates it. It fails onmainwithtable "users" already existsand passes with this change. I also ran a script that kills a realcreate_tenantpartway through loading a two-table schema file and then sends one request: on 0.9.0 the followingmigrate_tenantfailed 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
rescueruns.🤖 Generated with Claude Code