forked from mojotech/scrivener_ecto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support schema prefixes (mojotech#91)
* Support schema prefixes * Fix format * Code review changes * Remove prefix from comments and key_values migrations
- Loading branch information
1 parent
bb5fe9d
commit 6f160be
Showing
5 changed files
with
82 additions
and
4 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
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,17 @@ | ||
defmodule TestRepo.Migrations.CreateSchemas do | ||
use Ecto.Migration | ||
|
||
@prefixes ["tenant_1", "tenant_2"] | ||
|
||
def up do | ||
for prefix <- @prefixes do | ||
execute "CREATE SCHEMA #{prefix}" | ||
end | ||
end | ||
|
||
def down do | ||
for prefix <- @prefixes do | ||
execute "DROP SCHEMA #{prefix}" | ||
end | ||
end | ||
end |
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,13 @@ | ||
defmodule TestRepo.Migrations.CreateUsers do | ||
use Ecto.Migration | ||
|
||
@prefixes ["tenant_1", "tenant_2"] | ||
|
||
def change do | ||
for prefix <- @prefixes do | ||
create table(:users, prefix: prefix) do | ||
add :email, :string | ||
end | ||
end | ||
end | ||
end |
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 Scrivener.Paginator.Ecto.PrefixTest do | ||
use Scrivener.Ecto.TestCase | ||
|
||
alias Scrivener.Ecto.User | ||
|
||
defp create_users(number, prefix) do | ||
Enum.map(1..number, fn i -> | ||
%User{email: "user_#{i}@#{prefix}.com"} | ||
|> Scrivener.Ecto.Repo.insert!(prefix: prefix) | ||
end) | ||
end | ||
|
||
@schema_tenant_1 "tenant_1" | ||
@schema_tenant_2 "tenant_2" | ||
|
||
describe "prefix" do | ||
test "can specify prefix" do | ||
create_users(6, @schema_tenant_1) | ||
create_users(2, @schema_tenant_2) | ||
|
||
page_tenant_1 = Scrivener.Ecto.Repo.paginate(User, options: [prefix: @schema_tenant_1]) | ||
|
||
assert page_tenant_1.page_size == 5 | ||
assert page_tenant_1.page_number == 1 | ||
assert page_tenant_1.total_entries == 6 | ||
assert page_tenant_1.total_pages == 2 | ||
|
||
page_tenant_2 = Scrivener.Ecto.Repo.paginate(User, options: [prefix: @schema_tenant_2]) | ||
|
||
assert page_tenant_2.page_size == 5 | ||
assert page_tenant_2.page_number == 1 | ||
assert page_tenant_2.total_entries == 2 | ||
assert page_tenant_2.total_pages == 1 | ||
end | ||
end | ||
end |
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,7 @@ | ||
defmodule Scrivener.Ecto.User do | ||
use Ecto.Schema | ||
|
||
schema "users" do | ||
field(:email, :string) | ||
end | ||
end |