-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new MultiTenantHelper module into lib test support folder
This module contains functions related with multi tenancy for test purposes
- Loading branch information
1 parent
696a6c3
commit a50177a
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
defmodule PaperTrailTest.MultiTenantHelper do | ||
alias Ecto.Adapters.SQL | ||
alias Ecto.Changeset | ||
|
||
import Mix.Ecto, only: [build_repo_priv: 1] | ||
|
||
@migrations_path "migrations" | ||
@tenant "tenant_id" | ||
|
||
def add_prefix_to_changeset(%Changeset{} = changeset) do | ||
%{changeset | data: add_prefix_to_struct(changeset.data)} | ||
end | ||
|
||
def add_prefix_to_query(query) do | ||
query |> Ecto.Queryable.to_query() |> Map.put(:prefix, @tenant) | ||
end | ||
|
||
def add_prefix_to_struct(%{__struct__: _} = model) do | ||
Ecto.put_meta(model, prefix: @tenant) | ||
end | ||
|
||
def setup_tenant(repo, direction \\ :up, opts \\ [all: true]) do | ||
# Drop the previous tenant to reset the data | ||
SQL.query(repo, "DROP SCHEMA \"#{@tenant}\" CASCADE", []) | ||
|
||
opts_with_prefix = Keyword.put(opts, :prefix, @tenant) | ||
|
||
# Create new tenant | ||
SQL.query(repo, "CREATE SCHEMA \"#{@tenant}\"", []) | ||
Ecto.Migrator.run(repo, migrations_path(repo), direction, opts_with_prefix) | ||
end | ||
|
||
def tenant(), do: @tenant | ||
|
||
defp migrations_path(repo), do: Path.join(build_repo_priv(repo), @migrations_path) | ||
end |