@@ -29,31 +29,33 @@ defmodule AshPostgres.Partitioning do
2929 """
3030 def exists? ( resource , opts ) do
3131 repo = AshPostgres.DataLayer.Info . repo ( resource )
32- key = Keyword . fetch! ( opts , :key )
33- table = AshPostgres.DataLayer.Info . table ( resource )
34- partition_name = table <> "_" <> "#{ key } "
32+ partition_name = partition_name ( resource , opts )
3533
36- partition_exists? ( repo , resource , partition_name )
34+ partition_exists? ( repo , resource , partition_name , opts )
3735 end
3836
3937 # TBI
40- defp create_range_partition ( repo , resource , opts ) do
38+ defp create_range_partition ( _repo , _resource , _opts ) do
4139 end
4240
4341 defp create_list_partition ( repo , resource , opts ) do
4442 key = Keyword . fetch! ( opts , :key )
4543 table = AshPostgres.DataLayer.Info . table ( resource )
46- partition_name = table <> "_" <> "#{ key } "
44+ partition_name = partition_name ( resource , opts )
45+
46+ schema =
47+ Keyword . get ( opts , :tenant )
48+ |> tenant_schema ( resource )
4749
48- if partition_exists? ( repo , resource , partition_name ) do
50+ if partition_exists? ( repo , resource , partition_name , opts ) do
4951 { :error , :allready_exists }
5052 else
5153 Ecto.Adapters.SQL . query (
5254 repo ,
53- "CREATE TABLE #{ partition_name } PARTITION OF public. #{ table } FOR VALUES IN (#{ key } )"
55+ "CREATE TABLE \" #{ schema } \" . \" #{ partition_name } \" PARTITION OF \" #{ schema } \" . \" #{ table } \" FOR VALUES IN (' #{ key } ' )"
5456 )
5557
56- if partition_exists? ( repo , resource , partition_name ) do
58+ if partition_exists? ( repo , resource , partition_name , opts ) do
5759 :ok
5860 else
5961 { :error , "Unable to create partition" }
@@ -62,18 +64,37 @@ defmodule AshPostgres.Partitioning do
6264 end
6365
6466 # TBI
65- defp create_hash_partition ( repo , resource , opts ) do
67+ defp create_hash_partition ( _repo , _resource , _opts ) do
6668 end
6769
68- defp partition_exists? ( repo , resource , parition_name ) do
70+ defp partition_exists? ( repo , resource , parition_name , opts ) do
71+ schema =
72+ Keyword . get ( opts , :tenant )
73+ |> tenant_schema ( resource )
74+
6975 % Postgrex.Result { } =
7076 result =
7177 repo
7278 |> Ecto.Adapters.SQL . query! (
73- "select table_name from information_schema.tables t where t.table_schema = 'public' and t.table_name = $1 " ,
74- [ parition_name ]
79+ "select table_name from information_schema.tables t where t.table_schema = $1 and t.table_name = $2 " ,
80+ [ schema , parition_name ]
7581 )
7682
7783 result . num_rows > 0
7884 end
85+
86+ defp partition_name ( resource , opts ) do
87+ key = Keyword . fetch! ( opts , :key )
88+ table = AshPostgres.DataLayer.Info . table ( resource )
89+ "#{ table } _#{ key } "
90+ end
91+
92+ defp tenant_schema ( tenant , resource ) do
93+ tenant
94+ |> Ash.ToTenant . to_tenant ( resource )
95+ |> case do
96+ nil -> "public"
97+ tenant -> tenant
98+ end
99+ end
79100end
0 commit comments