Skip to content

Commit e07b602

Browse files
committed
improvement: verify check constraint attributes at compile time
fixes #647
1 parent c89a4bc commit e07b602

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/data_layer.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ defmodule AshPostgres.DataLayer do
417417
verifiers: [
418418
AshPostgres.Verifiers.PreventMultidimensionalArrayAggregates,
419419
AshPostgres.Verifiers.ValidateReferences,
420+
AshPostgres.Verifiers.ValidateCheckConstraints,
420421
AshPostgres.Verifiers.PreventAttributeMultitenancyAndNonFullMatchType,
421422
AshPostgres.Verifiers.EnsureTableOrPolymorphic,
422423
AshPostgres.Verifiers.ValidateIdentityIndexNames
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs.contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule AshPostgres.Verifiers.ValidateCheckConstraints do
6+
@moduledoc false
7+
use Spark.Dsl.Verifier
8+
alias Spark.Dsl.Verifier
9+
10+
def verify(dsl) do
11+
resource = Verifier.get_persisted(dsl, :module)
12+
13+
dsl
14+
|> AshPostgres.DataLayer.Info.check_constraints()
15+
|> Enum.each(fn constraint ->
16+
constraint.attribute
17+
|> List.wrap()
18+
|> Enum.each(fn attribute_name ->
19+
if is_nil(Ash.Resource.Info.attribute(dsl, attribute_name)) do
20+
raise Spark.Error.DslError,
21+
path: [:postgres, :check_constraints, constraint.name],
22+
module: resource,
23+
message: """
24+
Check constraint `#{constraint.name}` references attribute `#{attribute_name}`, but no such attribute exists on resource `#{inspect(resource)}`.
25+
26+
Available attributes: #{dsl |> Ash.Resource.Info.attributes() |> Enum.map(& &1.name) |> inspect()}
27+
""",
28+
location: Spark.Dsl.Transformer.get_section_anno(dsl, [:postgres, :check_constraints])
29+
end
30+
end)
31+
end)
32+
33+
:ok
34+
end
35+
end
36+

0 commit comments

Comments
 (0)