Skip to content

Commit

Permalink
Also provide the ability to extract the tenant id from the prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdeville committed Sep 8, 2016
1 parent 8471747 commit 8c91ec6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/apartmentex/prefix_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ defmodule Apartmentex.PrefixBuilder do
def build_prefix(tenant) do
@schema_prefix <> Integer.to_string(tenant.id)
end

def extract_tenant(table_prefix) do
String.replace_prefix(table_prefix, @schema_prefix, "")
end
end
9 changes: 7 additions & 2 deletions test/apartmentex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ defmodule Apartmentex.ApartmentexTest do
prefix = %Note{}
|> set_tenant(@tenant_id)
|> Ecto.get_meta(:prefix)
assert prefix == "tenant_2"
assert prefix == "tenant_#{@tenant_id}"
end

test ".set_tenant/2 changeset adds the tenant prefix" do
Expand All @@ -174,7 +174,7 @@ defmodule Apartmentex.ApartmentexTest do
|> Map.fetch!(:data)
|> Ecto.get_meta(:prefix)

assert prefix == "tenant_2"
assert prefix == "tenant_#{@tenant_id}"
end

test ".set_tenant/2 queryable adds the tenant prefix" do
Expand All @@ -184,4 +184,9 @@ defmodule Apartmentex.ApartmentexTest do

assert prefix == "tenant_#{@tenant_id}"
end

test ".extract_tenant/1 removes the prefix from the schema" do
assert Apartmentex.PrefixBuilder.extract_tenant("tenant_#{@tenant_id}") == "#{@tenant_id}"
assert Apartmentex.PrefixBuilder.extract_tenant("tenant_somestring") == "somestring"
end
end

0 comments on commit 8c91ec6

Please sign in to comment.