Skip to content

Commit

Permalink
Support schema prefixes (mojotech#91)
Browse files Browse the repository at this point in the history
* Support schema prefixes

* Fix format

* Code review changes

* Remove prefix from comments and key_values migrations
  • Loading branch information
tschmidleithner authored Aug 23, 2020
1 parent bb5fe9d commit 6f160be
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/scrivener/paginater/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ defimpl Scrivener.Paginater, for: Ecto.Query do
options: options
}) do
total_entries =
Keyword.get_lazy(options, :total_entries, fn -> total_entries(query, repo, caller) end)
Keyword.get_lazy(options, :total_entries, fn ->
total_entries(query, repo, caller, options)
end)

total_pages = total_pages(total_entries, page_size)
allow_overflow_page_number = Keyword.get(options, :allow_overflow_page_number, false)
Expand All @@ -35,20 +37,23 @@ defimpl Scrivener.Paginater, for: Ecto.Query do

defp entries(query, repo, page_number, _, page_size, caller, options) do
offset = Keyword.get_lazy(options, :offset, fn -> page_size * (page_number - 1) end)
prefix = options[:prefix]

query
|> offset(^offset)
|> limit(^page_size)
|> repo.all(caller: caller)
|> repo.all(caller: caller, prefix: prefix)
end

defp total_entries(query, repo, caller) do
defp total_entries(query, repo, caller, options) do
prefix = options[:prefix]

total_entries =
query
|> exclude(:preload)
|> exclude(:order_by)
|> aggregate()
|> repo.one(caller: caller)
|> repo.one(caller: caller, prefix: prefix)

total_entries || 0
end
Expand Down
17 changes: 17 additions & 0 deletions priv/repo/migrations/04_create_schemas.exs
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
13 changes: 13 additions & 0 deletions priv/repo/migrations/05_create_users.exs
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
36 changes: 36 additions & 0 deletions test/scrivener/paginator/ecto/prefix_test.exs
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
7 changes: 7 additions & 0 deletions test/support/user.ex
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

0 comments on commit 6f160be

Please sign in to comment.