@@ -3,24 +3,21 @@ defmodule AshPostgres.MigrationGenerator.Phase do
33
44 defmodule Create do
55 @ moduledoc false
6- defstruct [ :table , :schema , :multitenancy , operations: [ ] , commented?: false ]
6+ defstruct [ :table , :schema , :multitenancy , partitioning: nil , operations: [ ] , commented?: false ]
77
88 import AshPostgres.MigrationGenerator.Operation.Helper , only: [ as_atom: 1 ]
99
10- def up ( % { schema: schema , table: table , operations: operations , multitenancy: multitenancy } ) do
10+ def up ( % { schema: schema , table: table , operations: operations , multitenancy: multitenancy , partitioning: partitioning } ) do
1111 if multitenancy . strategy == :context do
12- "create table(:#{ as_atom ( table ) } , primary_key: false, prefix: prefix()) do\n " <>
12+ arguments = arguments ( [ prefix ( "prefix()" ) , options ( partitioning: partitioning ) ] )
13+
14+ "create table(:#{ as_atom ( table ) } , primary_key: false#{ arguments } ) do\n " <>
1315 Enum . map_join ( operations , "\n " , fn operation -> operation . __struct__ . up ( operation ) end ) <>
1416 "\n end"
1517 else
16- opts =
17- if schema do
18- ", prefix: \" #{ schema } \" "
19- else
20- ""
21- end
18+ arguments = arguments ( [ prefix ( schema ) , options ( partitioning: partitioning ) ] )
2219
23- "create table(:#{ as_atom ( table ) } , primary_key: false#{ opts } ) do\n " <>
20+ "create table(:#{ as_atom ( table ) } , primary_key: false#{ arguments } ) do\n " <>
2421 Enum . map_join ( operations , "\n " , fn operation -> operation . __struct__ . up ( operation ) end ) <>
2522 "\n end"
2623 end
@@ -40,6 +37,27 @@ defmodule AshPostgres.MigrationGenerator.Phase do
4037 "drop table(:#{ as_atom ( table ) } #{ opts } )"
4138 end
4239 end
40+
41+ def arguments ( [ "" , "" ] ) , do: ""
42+ def arguments ( arguments ) , do: ", " <> Enum . join ( Enum . reject ( arguments , & is_nil ( & 1 ) ) , "," )
43+
44+ def prefix ( nil ) , do: nil
45+ def prefix ( schema ) , do: "prefix: #{ schema } "
46+
47+ def options ( _options , _acc \\ [ ] )
48+ def options ( [ ] , [ ] ) , do: ""
49+ def options ( [ ] , acc ) , do: "options: \" #{ Enum . join ( acc , " " ) } \" "
50+
51+ def options ( [ { :partitioning , % { method: method , attribute: attribute } } | rest ] , acc ) do
52+ option = "PARTITION BY #{ String . upcase ( Atom . to_string ( method ) ) } (#{ attribute } )"
53+
54+ rest
55+ |> options ( acc ++ [ option ] )
56+ end
57+
58+ def options ( [ _ | rest ] , acc ) do
59+ options ( rest , acc )
60+ end
4361 end
4462
4563 defmodule Alter do
0 commit comments