michaelfich
How to move an existing database into a multi-tenant configuration?
Hey all,
I’ve been working on a project where I need to move an existing database into a multi-tenant configuration.
We have some migrations that we want to run outside of individual tenants (creating enums, etc) to be run from priv/repo/migration, and also a set of migrations we want to run to generate tables for each individual tenant from priv/repo/tenant_migrations.
Ideally I would want the migrations to run for all of these sequentially whenever I run mix ecto.migrate on the single database, in the following order:
publictenant_1tenant_2tenant_3- etc…
Additionally I do not wish maintain multiple pools of database connections to the same database, if possible, though it would be nice to be able to have separate Ecto Repo’s for each tenant, to make queries such as:
MyApp.Repo.Tenant1.all(query)
# -- or --
MyApp.Repo.all(query, prefix: "tenant_1")
I’ve been able to run these migrations properly from the command line through multiple commands, by doing the following:
mix ecto.migrate
mix ecto.migrate --prefix=tenant_1 --migrations-path=priv/repo/tenant_migrations/
mix ecto.migrate --prefix=tenant_2 --migrations-path=priv/repo/tenant_migrations/
mix ecto.migrate --prefix=tenant_3 --migrations-path=priv/repo/tenant_migrations/
If possible, it would be nice to configure each Repo with a shared connection pool and then have their own set of migrations. One of the issues I encountered while trying to define a separate Repo for each tenant was that running mix ecto.create or mix ecto.drop, it tries to do create or drop the database multiple times; working the first and failing the rest of the times.
I’ve scoured the Ecto and EctoSQL libraries for documentation on this and tried multiple solutions I’ve come across, but it’s not yet produced a solution that works yet aside from running multiple ecto.migrate mix tasks as demonstrated above.
Has anyone come across this use-case and able to point me in the right direction? Any assistance would be greatly appreciated.
Most Liked
l00ker
Have you looked at Triplex?
al2o3cr
al2o3cr
I can understand this concern for some libraries, but take the time to read through the Triplex code: there’s not much of it, and what’s there is mostly gluing together things that already exist. All the actual “tenent enforcement” is delegated to Ecto’s prefix machinery.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









