Fix concurrent index migrations with schema-driven multitenancy #610#699
Open
RylandBangerter85 wants to merge 3 commits intoash-project:mainfrom
Open
Fix concurrent index migrations with schema-driven multitenancy #610#699RylandBangerter85 wants to merge 3 commits intoash-project:mainfrom
RylandBangerter85 wants to merge 3 commits intoash-project:mainfrom
Conversation
- Detect migrations with @disable_ddl_transaction attribute - Run concurrent index migrations outside transaction context - Use Ecto.Adapters.SQL.checkout to get fresh connection - Add comprehensive test suite for concurrent index scenarios - Maintain backward compatibility with regular migrations Fixes issue where CREATE INDEX CONCURRENTLY fails when creating tenants
…tion check Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
|
I've actually just realized unfortunately a fundamental issue with this approach that I didn't consider the first time. Specifically, migrations often depend on each other, i.e one migration adds a table, another migration adds an index to that table. But in postgres, DDL migrations are transactional, meaning they won't be visible to migrations that are running outside of a transaction 😢 I think our only actual answer here is instead to check if we are running inside of a transaction while doing this and provide the user with guidance that they should disable the transaction on the action they are running. I don't think its possible to solve this in reality 😓 |
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.
Addressed @zachdaniel’s feedback:
migration_requires_no_transaction?now relies only on the compiled module’s metadata viamod.__migration__(), and no longer reads the migration file.Solution
Updated
AshPostgres.MultiTenancy.migrate_tenant/3to:@disable_ddl_transactionin the compiled module metadata)Ecto.Adapters.SQL.checkout/3Changes
lib/multitenancy.ex: Addedmigration_requires_no_transaction?/1andrun_migration_without_transaction/4test/concurrent_index_multitenancy_test.exs: Added regression tests for:create_tenant!Technical notes
__migration__/0metadata fordisable_ddl_transactioninstead of scanning the fileModule.put_attribute/3Contributor checklist
Leave anything that you believe does not apply unchecked.