-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing multi-tenancy support #280
Comments
My team just hit this snag. |
Thanks for posting here @churcho, and sorry @joeppeeters for taking so long to respond here! We currently don't have anything that supports multitenancy. There is also a conversation on #273 (comment) that might be related, since they're also trying to provide extra arguments to build(:user) |> insert(prefix: "test_tenant") You would have to define them in each strategy you create, # in TestTenantEctoStrategy or something like that
def handle_insert(%{__meta__: %{__struct__: Ecto.Schema.Metadata}} = record, %{repo: repo}) do
record
|> cast
|> repo.insert!(prefix: "test_tenant")
end Is that something that might work? |
Hi @germsvel
However, this leads to a conflict with the
However, it works with another function name. |
I added something like this to our factory 🤷♂ def tenant_insert(factory_name, attrs \\ %{}, tenant) do
build(factory_name, attrs)
|> MyApp.Repo.insert!(prefix: tenant)
end We will see how far this will get us 😅 Edit: With the mentioned release v2.6.0 below this might look like: def tenant_insert(factory_name, attrs \\ %{}, tenant) do
insert(factory_name, attrs, prefix: tenant)
end |
The def checkout_factory do
%Checkout{
(...)
}
|> Ecto.put_meta(prefix: "test")
end |
I was looking through this issue once again, and though I still think adding prefix options to It means we'd have to define a new factory per tenant, but maybe that would be good enough for some time? And if you need it per struct, @hl's comment on What do people think? If it seems good to move with a |
I'd very much prefer |
Excellent! Would love a PR for this if someone is interested in doing that. (Or I'll try to get this in at some point) It's been mentioned before, but just so there's no confusion, the usage would be to specify defmodule MyTestTenantFactory do
use ExMachina.Ecto, repo: MyRepo, repo_options: [prefix: "test_tenant"]
end That would apply globally to all factories defined in Does that sound good? 👍 👎 |
I just pushed v2.6.0 of ExMachina which includes passing Repo options to |
Hi,
In our project we implement multi-tenancy by setting prefixes on the
Repo
(eg.MyApp.Repo.insert(%User{name: 'John'}, prefix: tenant_id)
We'd like to reuse this behaviour in our tests, but ex_machina doesn't seem to support it currently. Would that be something you would consider to add?
An easy way would be to create a factory for a test tenant by configure it in the
use
statement:and then rewriting the
ExMachina.EctoStrategy
to:or more flexible at runtime by introducing an
insert/3
method.Is this something worth to consider? Or do you see any other ways to achieve the same behaviour?
The text was updated successfully, but these errors were encountered: