@@ -7,7 +7,7 @@ defmodule AshPostgres.MigrationGenerator.Phase do
77
88 defmodule Create do
99 @ moduledoc false
10- defstruct [ :table , :schema , :multitenancy , :repo , operations: [ ] , commented?: false ]
10+ defstruct [ :table , :schema , :multitenancy , partitioning: nil , :repo , operations: [ ] , commented?: false ]
1111
1212 import AshPostgres.MigrationGenerator.Operation.Helper , only: [ as_atom: 1 ]
1313
@@ -16,10 +16,13 @@ defmodule AshPostgres.MigrationGenerator.Phase do
1616 table: table ,
1717 operations: operations ,
1818 multitenancy: multitenancy ,
19+ partitioning: partitioning ,
1920 repo: repo
2021 } ) do
2122 if multitenancy . strategy == :context do
22- "create table(:#{ as_atom ( table ) } , primary_key: false, prefix: prefix()) do\n " <>
23+ arguments = arguments ( [ prefix ( "prefix()" ) , options ( partitioning: partitioning ) ] )
24+
25+ "create table(:#{ as_atom ( table ) } , primary_key: false#{ arguments } ) do\n " <>
2326 Enum . map_join ( operations , "\n " , fn operation -> operation . __struct__ . up ( operation ) end ) <>
2427 "\n end"
2528 else
@@ -36,9 +39,11 @@ defmodule AshPostgres.MigrationGenerator.Phase do
3639 else
3740 ""
3841 end
42+
43+ arguments = arguments ( [ prefix ( schema ) , options ( partitioning: partitioning ) ] )
3944
4045 pre_create <>
41- "create table(:#{ as_atom ( table ) } , primary_key: false#{ opts } ) do\n " <>
46+ "create table(:#{ as_atom ( table ) } , primary_key: false#{ opts } #{ arguments } ) do\n " <>
4247 Enum . map_join ( operations , "\n " , fn operation -> operation . __struct__ . up ( operation ) end ) <>
4348 "\n end"
4449 end
@@ -58,6 +63,27 @@ defmodule AshPostgres.MigrationGenerator.Phase do
5863 "drop table(:#{ as_atom ( table ) } #{ opts } )"
5964 end
6065 end
66+
67+ def arguments ( [ "" , "" ] ) , do: ""
68+ def arguments ( arguments ) , do: ", " <> Enum . join ( Enum . reject ( arguments , & is_nil ( & 1 ) ) , "," )
69+
70+ def prefix ( nil ) , do: nil
71+ def prefix ( schema ) , do: "prefix: #{ schema } "
72+
73+ def options ( _options , _acc \\ [ ] )
74+ def options ( [ ] , [ ] ) , do: ""
75+ def options ( [ ] , acc ) , do: "options: \" #{ Enum . join ( acc , " " ) } \" "
76+
77+ def options ( [ { :partitioning , % { method: method , attribute: attribute } } | rest ] , acc ) do
78+ option = "PARTITION BY #{ String . upcase ( Atom . to_string ( method ) ) } (#{ attribute } )"
79+
80+ rest
81+ |> options ( acc ++ [ option ] )
82+ end
83+
84+ def options ( [ _ | rest ] , acc ) do
85+ options ( rest , acc )
86+ end
6187 end
6288
6389 defmodule Alter do
0 commit comments