Skip to content

fix: refuse a statement for a resource without tenant_repo while its repo is bound to a tenant - #243

Closed
C-Sinclair wants to merge 2 commits into
ash-project:mainfrom
C-Sinclair:fix/multi-repo-mixed-tenants
Closed

C-Sinclair wants to merge 2 commits into
ash-project:mainfrom
C-Sinclair:fix/multi-repo-mixed-tenants

Conversation

@C-Sinclair

@C-Sinclair C-Sinclair commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

There's a bug in the tenant_repo implementation. If you try to mix resources with the same Repo module, but only one with a tenant_repo config, statements will land on the tenant's database.
If the tenant db doens't have that table, it errors with no such table.

This is proven with a new test against AshSqlite.Test.SharedNote: failing until the implemented fix in this PR.

The fix is just to raise if the transaction spans a tenant db and another. Better to error hard rather than read/write data from the wrong place!

…repo is bound to a tenant

`bind_tenant_repo/5` binds a tenant instance with `put_dynamic_repo/1`, and Ecto keeps that binding per repo module per process. A resource with no `tenant_repo` on the same repo module, run inside a tenant transaction, ran on the tenant database. `unbound/3` and the `nil` branch of `with_tenant_repo/4` now raise `ArgumentError` while the data layer holds that binding.
@C-Sinclair
C-Sinclair marked this pull request as ready for review October 13, 2026 15:47
The refusal now says that a resource on its own repo module commits in its own transaction, which a rollback of the tenant transaction does not undo. A new test rolls back a tenant transaction after a `Post` write on `AshSqlite.TestRepo` and finds the `Post` row kept.
@zachdaniel

Copy link
Copy Markdown
Contributor

@C-Sinclair I took a stab at simplifying this actually via making this a callback on the repo. Could you take a look and see what you think? It should solve this issue at compile time by requiring configuring a different repo for non-multitenant resources: https://github.com/ash-project/ash_sqlite/pull/245/changes

@C-Sinclair

Copy link
Copy Markdown
Contributor Author

@C-Sinclair I took a stab at simplifying this actually via making this a callback on the repo. Could you take a look and see what you think? It should solve this issue at compile time by requiring configuring a different repo for non-multitenant resources: https://github.com/ash-project/ash_sqlite/pull/245/changes

Nice! That callback is much cleaner, now I see it 🧹
I'll close this PR in that case, in favour of yours

@C-Sinclair C-Sinclair closed this Oct 17, 2026
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