def create_new_tenant(subdomain, company_name) do # 1. Create schema in DB prefix = "tenant_#subdomain" Ecto.Adapters.SQL.query!(MyApp.Repo, "CREATE SCHEMA IF NOT EXISTS #prefix") UniEcto.Plugin.run_migrations_for(prefix) 3. Insert tenant record into public tenants table %Tenantprefix: prefix, name: company_name |> Repo.insert!() # Runs in 'public' schema
:ok, prefix end The uni_ecto_plugin often bundles a caching mechanism. Setting a tenant prefix usually involves a database lookup to validate the tenant exists. This adds latency. uni ecto plugin
def list_users_raw do prefix = UniEcto.Plugin.get_tenant_prefix() Repo.query("SELECT * FROM #prefix.users") end What if your Settings table is global and Orders is per-tenant? def create_new_tenant(subdomain, company_name) do # 1