@@ -27,11 +27,25 @@ defmodule AshPostgres.Partitioning do
2727 @ doc """
2828 Check if partition exists
2929 """
30- def exists ?( resource , opts ) do
30+ def existing_partition ?( resource , opts ) do
3131 repo = AshPostgres.DataLayer.Info . repo ( resource )
32- partition_name = partition_name ( resource , opts )
3332
34- partition_exists? ( repo , resource , partition_name , opts )
33+ resource
34+ |> AshPostgres.DataLayer.Info . partitioning_method ( )
35+ |> case do
36+ :range ->
37+ false
38+
39+ :list ->
40+ partition_name = partition_name ( resource , opts )
41+ schema_exists? ( repo , resource , partition_name , opts )
42+
43+ :hash ->
44+ false
45+
46+ unsupported_method ->
47+ raise "Invalid partition method, got: #{ unsupported_method } "
48+ end
3549 end
3650
3751 # TBI
@@ -47,15 +61,15 @@ defmodule AshPostgres.Partitioning do
4761 Keyword . get ( opts , :tenant )
4862 |> tenant_schema ( resource )
4963
50- if partition_exists ?( repo , resource , partition_name , opts ) do
64+ if schema_exists ?( repo , resource , partition_name , opts ) do
5165 { :error , :already_exists }
5266 else
5367 Ecto.Adapters.SQL . query (
5468 repo ,
5569 "CREATE TABLE \" #{ schema } \" .\" #{ partition_name } \" PARTITION OF \" #{ schema } \" .\" #{ table } \" FOR VALUES IN ('#{ key } ')"
5670 )
5771
58- if partition_exists ?( repo , resource , partition_name , opts ) do
72+ if schema_exists ?( repo , resource , partition_name , opts ) do
5973 :ok
6074 else
6175 { :error , "Unable to create partition" }
@@ -67,7 +81,7 @@ defmodule AshPostgres.Partitioning do
6781 defp create_hash_partition ( _repo , _resource , _opts ) do
6882 end
6983
70- defp partition_exists ?( repo , resource , parition_name , opts ) do
84+ defp schema_exists ?( repo , resource , parition_name , opts ) do
7185 schema =
7286 Keyword . get ( opts , :tenant )
7387 |> tenant_schema ( resource )
0 commit comments