Fix concurrent index migrations with schema-driven multitenancy #610#699
Fix concurrent index migrations with schema-driven multitenancy #610#699RylandBangerter85 wants to merge 4 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>
|
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 😓 |
|
@zachdaniel so you just want me to have it output a warning? |
|
Yes, unfortunately that is all that we can really do. |
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.