-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathinfo.ex
235 lines (188 loc) · 7.54 KB
/
info.ex
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
defmodule AshPostgres.DataLayer.Info do
@moduledoc "Introspection functions for "
alias Spark.Dsl.Extension
@doc "The configured repo for a resource"
def repo(resource, type \\ :mutate) do
case Extension.get_opt(resource, [:postgres], :repo, nil, true) do
fun when is_function(fun, 2) ->
fun.(resource, type)
repo ->
repo
end
end
@doc "A keyword list of calculations to their sql representation"
def calculations_to_sql(resource) do
Extension.get_opt(resource, [:postgres], :calculations_to_sql, [])
end
def calculation_to_sql(resource, calc) do
calculations_to_sql(resource)[calc]
end
@doc """
A keyword list of identity names to the literal SQL string representation of
the `where` clause portion of identity's partial unique index.
For example, given the following identity for a resource:
identities do
identity :active, [:status] do
where expr(status == "active")
end
end
An appropriate `identity_wheres_to_sql` would need to be made to generate the
correct migration for the partial index used by the identity:
postgres do
...
identity_wheres_to_sql active: "status = 'active'"
end
"""
@spec identity_wheres_to_sql(Ash.Resource.t()) :: keyword(String.t())
def identity_wheres_to_sql(resource) do
Extension.get_opt(resource, [:postgres], :identity_wheres_to_sql, [])
end
@doc """
Returns the literal SQL for the `where` clause given a resource and an
identity name.
See `identity_wheres_to_sql/1` for more details.
"""
@spec identity_where_to_sql(Ash.Resource.t(), atom()) :: String.t() | nil
def identity_where_to_sql(resource, identity) do
identity_wheres_to_sql(resource)[identity]
end
@doc "Checks a version requirement against the resource's repo's postgres version"
def pg_version_matches?(resource, requirement) do
resource
|> min_pg_version()
|> Version.match?(requirement)
end
@doc "Gets the resource's repo's postgres version"
def min_pg_version(resource) do
case repo(resource, :read).min_pg_version() do
%Version{} = version ->
version
string when is_binary(string) ->
IO.warn(
"Got a `string` for min_pg_version, expected a `Version` struct. Got: #{inspect(string)}. Please call `Version.parse!` before returning the value."
)
Version.parse!(string)
end
end
@doc "The configured table for a resource"
def table(resource) do
Extension.get_opt(resource, [:postgres], :table, nil, true)
end
def simple_join_first_aggregates(resource) do
Extension.get_opt(resource, [:postgres], :simple_join_first_aggregates, [])
end
@doc "The configured schema for a resource"
def schema(resource) do
Extension.get_opt(resource, [:postgres], :schema, nil, true)
end
@doc "The configured references for a resource"
def references(resource) do
Extension.get_entities(resource, [:postgres, :references])
end
@doc "The configured reference for a given relationship of a resource"
def reference(resource, relationship) do
resource
|> Extension.get_entities([:postgres, :references])
|> Enum.find(&(&1.relationship == relationship))
end
@doc "A keyword list of customized migration types"
def migration_types(resource) do
Extension.get_opt(resource, [:postgres], :migration_types, [])
end
@doc "A keyword list of customized storage types"
def storage_types(resource) do
Extension.get_opt(resource, [:postgres], :storage_types, [])
end
@doc "A keyword list of customized migration defaults"
def migration_defaults(resource) do
Extension.get_opt(resource, [:postgres], :migration_defaults, [])
end
@doc "A list of attributes to be ignored when generating migrations"
def migration_ignore_attributes(resource) do
Extension.get_opt(resource, [:postgres], :migration_ignore_attributes, [])
end
@doc "The configured check_constraints for a resource"
def check_constraints(resource) do
Extension.get_entities(resource, [:postgres, :check_constraints])
end
@doc "The configured custom_indexes for a resource"
def custom_indexes(resource) do
Extension.get_entities(resource, [:postgres, :custom_indexes])
end
@doc "The configured custom_statements for a resource"
def custom_statements(resource) do
Extension.get_entities(resource, [:postgres, :custom_statements])
end
@doc "The configured polymorphic_reference_on_delete for a resource"
def polymorphic_on_delete(resource) do
Extension.get_opt(resource, [:postgres, :references], :polymorphic_on_delete, nil, true)
end
@doc "The configured polymorphic_reference_on_update for a resource"
def polymorphic_on_update(resource) do
Extension.get_opt(resource, [:postgres, :references], :polymorphic_on_update, nil, true)
end
@doc "The configured polymorphic_reference_name for a resource"
def polymorphic_name(resource) do
Extension.get_opt(resource, [:postgres, :references], :polymorphic_name, nil, true)
end
@doc "The configured polymorphic? for a resource"
def polymorphic?(resource) do
Extension.get_opt(resource, [:postgres], :polymorphic?, nil, true)
end
@doc "The configured unique_index_names"
def unique_index_names(resource) do
Extension.get_opt(resource, [:postgres], :unique_index_names, [], true)
end
@doc "The configured exclusion_constraint_names"
def exclusion_constraint_names(resource) do
Extension.get_opt(resource, [:postgres], :exclusion_constraint_names, [], true)
end
@doc "The configured identity_index_names"
def identity_index_names(resource) do
Extension.get_opt(resource, [:postgres], :identity_index_names, [], true)
end
@doc "Identities not to include in the migrations"
def skip_identities(resource) do
Extension.get_opt(resource, [:postgres], :skip_identities, [], true)
end
@doc "The configured foreign_key_names"
def foreign_key_names(resource) do
Extension.get_opt(resource, [:postgres], :foreign_key_names, [], true)
end
@doc "Whether or not the resource should be included when generating migrations"
def migrate?(resource) do
Extension.get_opt(resource, [:postgres], :migrate?, nil, true)
end
@doc "A list of keys to always include in upserts."
def global_upsert_keys(resource) do
Extension.get_opt(resource, [:postgres], :global_upsert_keys, [])
end
@doc "A stringified version of the base_filter, to be used in a where clause when generating unique indexes"
def base_filter_sql(resource) do
Extension.get_opt(resource, [:postgres], :base_filter_sql, nil)
end
@doc "Skip generating unique indexes when generating migrations"
def skip_unique_indexes(resource) do
Extension.get_opt(resource, [:postgres], :skip_unique_indexes, [])
end
@doc "The template for a managed tenant"
def manage_tenant_template(resource) do
Extension.get_opt(resource, [:postgres, :manage_tenant], :template, nil)
end
@doc "Whether or not to create a tenant for a given resource"
def manage_tenant_create?(resource) do
Extension.get_opt(resource, [:postgres, :manage_tenant], :create?, false)
end
@doc "Whether or not to update a tenant for a given resource"
def manage_tenant_update?(resource) do
Extension.get_opt(resource, [:postgres, :manage_tenant], :update?, false)
end
@doc "Partitioning method"
def partitioning_method(resource) do
Extension.get_opt(resource, [:postgres, :partitioning], :method, nil)
end
@doc "Partitioning attribute"
def partitioning_attribute(resource) do
Extension.get_opt(resource, [:postgres, :partitioning], :attribute, nil)
end
end