![]() Then, create a mix task to handle migrations. It also should delete the schema if tenant is deleted. What I want to do next is automatically issue query for creating postgres schema when new tenant is created. But of course, the above doesn't work because this feature is only in master branch at this point in time. ![]() Then the record will be added to the correct table in tenant_1 schema. Ecto.Schema does! iex> changeset = Guard.changeset(%Guard Initially I thought I could update the changeset's _meta_, but Ecto.Changeset does not have _meta_ key. :context - changes the struct meta context.:prefix - changes the struct query prefix.:source - changes the struct query source.When you define a schema with Ecto.Schema, you’re defining a container for data, but code tests are. Returns a new struct with updated metadata. Ecto.Changeset is code that, among other things, provides type casting and validation logic. Then, I found out about Ecto.put_meta/2 after looking through Ecto source code. However, elixir module attribute is resolved at compile time, so you can't change that dynamically during run time. should use postgres schema for tenant-1. When defining a model, you could set module attribute to tell Ecto that this table should be in postgres schema as defined in But for multi-tenancy purpose, needs to be set dynamically - usually from subdomain i.e. Also, there are few things that are still confusing to me. These are the things that I do to actually insert/update records into a correct schema. Then, run migration for your tenant by running mix ecto.migrate -prefix "tenant_1"įire up iex console by running iex -S mix rver. I don't think Ecto has a built-in function to actually create a schema for you. You would also need to create your own schema by issuing CREATE SCHEMA "tenant_1" from psql shell. This assumes the following model in web/models/guard.ex defmodule Tenancy.Guard doīuilds a changeset based on the `struct` and `params`. There is also library called Apartmentex that you could use to do multi-tenancy. What I am trying to do here is to use Postgres schema to achieve multi-tenancy, and of course using Ecto.ĭisclaimer: This is mostly for my personal notes as I try to understand Ecto/Elixir better. With Ecto 2.0, there is support for Postgres schema or multiple databases for MySQL.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |