-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathvalidate_identity_index_names.ex
61 lines (52 loc) · 1.99 KB
/
validate_identity_index_names.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
defmodule AshPostgres.Verifiers.ValidateIdentityIndexNames do
@moduledoc false
use Spark.Dsl.Verifier
alias Spark.Dsl.Verifier
def verify(dsl) do
identity_index_names =
AshPostgres.DataLayer.Info.identity_index_names(dsl)
Enum.each(identity_index_names, fn {identity, name} ->
if String.length(name) > 63 do
raise Spark.Error.DslError,
path: [:postgres, :identity_index_names, identity],
module: Verifier.get_persisted(dsl, :module),
message: """
Identity #{identity} has a name that is too long. Names must be 63 characters or less.
"""
end
end)
table = AshPostgres.DataLayer.Info.table(dsl)
if table do
dsl
|> Ash.Resource.Info.identities()
|> Enum.map(fn identity ->
{identity, identity_index_names[identity.name] || "#{table}_#{identity.name}_index"}
end)
|> Enum.group_by(&elem(&1, 1), &elem(&1, 0))
|> Enum.each(fn
{name, [_, _ | _] = identities} ->
raise Spark.Error.DslError,
path: [:postgres, :identity_index_names, name],
module: Verifier.get_persisted(dsl, :module),
message: """
Multiple identities would result in the same index name: #{name}
Identities: #{inspect(Enum.map(identities, & &1.name))}
"""
{name, [identity]} ->
if String.length(name) > 63 do
raise Spark.Error.DslError,
path: [:postgres, :identity_index_names, name],
module: Verifier.get_persisted(dsl, :module),
message: """
Identity #{identity.name} has a name that is too long. Names must be 63 characters or less.
Please configure an index name for this identity in the `identity_index_names` configuration. For example:
postgres do
identity_index_names #{identity.name}: "a_shorter_name"
end
"""
end
end)
end
:ok
end
end