Skip to content

Commit aa334eb

Browse files
committed
improvement: support default_prefix configuration
1 parent effb7b4 commit aa334eb

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/data_layer.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ defmodule AshPostgres.DataLayer do
16031603
query_tenant) do
16041604
Ecto.Query.put_query_prefix(query, query_tenant || root_tenant)
16051605
else
1606-
%{query | prefix: "public"}
1606+
%{query | prefix: repo(relationship.destination).config()[:default_prefix] || "public"}
16071607
end
16081608
end
16091609

@@ -1631,7 +1631,10 @@ defmodule AshPostgres.DataLayer do
16311631
query_tenant) do
16321632
Ecto.Query.put_query_prefix(query, query_tenant || root_tenant)
16331633
else
1634-
%{query | prefix: "public"}
1634+
%{
1635+
query
1636+
| prefix: repo(relationship.destination).config()[:default_prefix] || "public"
1637+
}
16351638
end
16361639
end
16371640

@@ -2263,9 +2266,12 @@ defmodule AshPostgres.DataLayer do
22632266

22642267
defp set_join_prefix(join_query, query, resource) do
22652268
if Ash.Resource.Info.multitenancy_strategy(resource) == :context do
2266-
%{join_query | prefix: query.prefix}
2269+
%{join_query | prefix: query.prefix || "public"}
22672270
else
2268-
%{join_query | prefix: "public"}
2271+
%{
2272+
join_query
2273+
| prefix: repo(resource).config()[:default_prefix] || "public"
2274+
}
22692275
end
22702276
end
22712277

lib/repo.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ defmodule AshPostgres.Repo do
3333
@callback tenant_migrations_path() :: String.t()
3434
@doc "The path where your migrations are stored"
3535
@callback migrations_path() :: String.t()
36+
@doc "The default prefix(postgres schema) to use when building queries"
37+
@callback default_prefix() :: String.t()
3638

3739
defmacro __using__(opts) do
3840
quote bind_quoted: [opts: opts] do
@@ -45,6 +47,7 @@ defmodule AshPostgres.Repo do
4547
def installed_extensions, do: []
4648
def tenant_migrations_path, do: nil
4749
def migrations_path, do: nil
50+
def default_prefix, do: "public"
4851

4952
def all_tenants do
5053
raise """
@@ -65,11 +68,16 @@ defmodule AshPostgres.Repo do
6568
|> Keyword.put(:installed_extensions, installed_extensions())
6669
|> Keyword.put(:tenant_migrations_path, tenant_migrations_path())
6770
|> Keyword.put(:migrations_path, migrations_path())
71+
|> Keyword.put(:default_prefix, default_prefix())
6872

6973
{:ok, new_config}
7074
end
7175

72-
defoverridable init: 2, installed_extensions: 0, all_tenants: 0, tenant_migrations_path: 0
76+
defoverridable init: 2,
77+
installed_extensions: 0,
78+
all_tenants: 0,
79+
tenant_migrations_path: 0,
80+
default_prefix: 0
7381
end
7482
end
7583
end

0 commit comments

Comments
 (0)