Skip to content

Commit

Permalink
Add new MultiTenantHelper module into lib test support folder
Browse files Browse the repository at this point in the history
This module contains functions related with multi tenancy for test purposes
  • Loading branch information
dreamingechoes committed Aug 1, 2017
1 parent 696a6c3 commit a50177a
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/support/multi_tenant_helper.exs
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

0 comments on commit a50177a

Please sign in to comment.