-
-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathphase.ex
More file actions
142 lines (125 loc) · 3.75 KB
/
phase.ex
File metadata and controls
142 lines (125 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs.contributors>
#
# SPDX-License-Identifier: MIT
defmodule AshPostgres.MigrationGenerator.Phase do
@moduledoc false
defmodule Create do
@moduledoc false
defstruct [
:table,
:schema,
:multitenancy,
:repo,
:create_table_options,
operations: [],
commented?: false
]
import AshPostgres.MigrationGenerator.Operation.Helper, only: [as_atom: 1]
def up(%{
schema: schema,
table: table,
operations: operations,
multitenancy: multitenancy,
repo: repo,
create_table_options: create_table_options
}) do
table_options_str =
if create_table_options do
", options: #{inspect(create_table_options)}"
else
""
end
if multitenancy.strategy == :context do
"create table(:#{as_atom(table)}, primary_key: false, prefix: prefix()#{table_options_str}) do\n" <>
Enum.map_join(operations, "\n", fn operation -> operation.__struct__.up(operation) end) <>
"\nend"
else
pre_create =
if schema && repo.create_schemas_in_migrations?() do
"execute(\"CREATE SCHEMA IF NOT EXISTS #{schema}\")" <> "\n\n"
else
""
end
opts =
if schema do
", prefix: \"#{schema}\""
else
""
end
pre_create <>
"create table(:#{as_atom(table)}, primary_key: false#{opts}#{table_options_str}) do\n" <>
Enum.map_join(operations, "\n", fn operation -> operation.__struct__.up(operation) end) <>
"\nend"
end
end
def down(%{schema: schema, table: table, multitenancy: multitenancy}) do
if multitenancy.strategy == :context do
"drop table(:#{as_atom(table)}, prefix: prefix())"
else
opts =
if schema do
", prefix: \"#{schema}\""
else
""
end
"drop table(:#{as_atom(table)}#{opts})"
end
end
end
defmodule Alter do
@moduledoc false
defstruct [:schema, :table, :multitenancy, operations: [], commented?: false]
import AshPostgres.MigrationGenerator.Operation.Helper, only: [as_atom: 1]
def up(%{table: table, schema: schema, operations: operations, multitenancy: multitenancy}) do
body =
operations
|> Enum.map_join("\n", fn operation -> operation.__struct__.up(operation) end)
|> String.trim()
if body == "" do
""
else
if multitenancy.strategy == :context do
"alter table(:#{as_atom(table)}, prefix: prefix()) do\n" <>
body <>
"\nend"
else
opts =
if schema do
", prefix: \"#{schema}\""
else
""
end
"alter table(:#{as_atom(table)}#{opts}) do\n" <>
body <>
"\nend"
end
end
end
def down(%{table: table, schema: schema, operations: operations, multitenancy: multitenancy}) do
body =
operations
|> Enum.reverse()
|> Enum.map_join("\n", fn operation -> operation.__struct__.down(operation) end)
|> String.trim()
if body == "" do
""
else
if multitenancy.strategy == :context do
"alter table(:#{as_atom(table)}, prefix: prefix()) do\n" <>
body <>
"\nend"
else
opts =
if schema do
", prefix: \"#{schema}\""
else
""
end
"alter table(:#{as_atom(table)}#{opts}) do\n" <>
body <>
"\nend"
end
end
end
end
end