Multi-tenancy in Ecto and Phoenix
Creating a new tentant
A new tenant requires a namespace, which is a schema in Postgres, and a prefix in Ecto:
$ psql -U postgres database_x
> create schema aktagon;
Querying data
import Ecto.Query
email = "[email protected]"
q = from(m in User, where: m.email == ^email)
Repo.all(%{q | prefix: "aktagon"})
Documentation: https://hexdocs.pm/ecto/Ecto.Query.html#module-query-prefix
Inserting data
Repo.insert(
Ecto.put_meta(
%User{ email: "[email protected]" },
prefix: "aktagon"
)
)
Migrations
$ mix ecto.migrate --prefix "aktagon"
Notes
- (KeyError) key :__meta__ not found
I got this error when passing a changeset to Ecto.put_meta instead of a User struct.