-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
verify_actions_atomic.ex
159 lines (139 loc) · 4.54 KB
/
verify_actions_atomic.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
defmodule Ash.Resource.Verifiers.VerifyActionsAtomic do
@moduledoc """
Raises an error on update or destroy actions with `require_atomic?` set to
true when it is known at compile time that they will not be atomic.
"""
use Spark.Dsl.Verifier
def verify(dsl) do
module = Spark.Dsl.Verifier.get_persisted(dsl, :module)
warnings =
dsl
|> Ash.Resource.Info.actions()
|> Enum.filter(
&(&1.type in [:update, :destroy] && &1.require_atomic? &&
data_layer_can_atomic?(module, &1))
)
|> Enum.flat_map(fn action ->
warnings =
if action.manual do
[
Spark.Error.DslError.exception(
module: module,
message:
non_atomic_message(
module,
action.name,
"manual actions cannot be done atomically"
),
path: [:actions, action.name]
)
]
else
[]
end
change_warnings =
action.changes
|> Enum.concat(Ash.Resource.Info.changes(dsl, action.type))
|> Enum.concat(Ash.Resource.Info.validations(dsl, action.type))
|> Enum.map(fn
%{change: {module, _}} ->
module
%{validation: {module, _}} ->
module
end)
|> Enum.reject(fn module ->
module.atomic?()
end)
|> case do
[] ->
[]
non_atomic_change_modules ->
[
Spark.Error.DslError.exception(
module: module,
message:
non_atomic_message(
module,
action.name,
"the changes `#{inspect(non_atomic_change_modules)}` cannot be done atomically"
),
path: [:actions, action.name]
)
]
end
notifier_warnings =
dsl
|> Ash.Resource.Info.notifiers()
|> Enum.filter(fn notifier ->
notifier.requires_original_data?(module, action.name)
end)
|> case do
[] ->
[]
non_atomic_notifier_modules ->
[
Spark.Error.DslError.exception(
module: module,
message:
non_atomic_message(
module,
action.name,
"the notifiers `#{inspect(non_atomic_notifier_modules)}` cannot be done atomically"
),
path: [:actions, action.name]
)
]
end
attribute_warnings = []
action.accept
|> Enum.map(&Ash.Resource.Info.attribute(dsl, &1))
|> Enum.filter(fn %{type: type, constraints: constraints} ->
case type do
{:array, {:array, _}} ->
true
{:array, type} when is_atom(type) ->
Code.ensure_compiled!(type)
not_atomic?(type, constraints)
type when is_atom(type) ->
Code.ensure_compiled!(type)
not_atomic?(type, constraints)
_ ->
false
end
end)
|> case do
[] ->
[]
not_atomic_attributes ->
[
Spark.Error.DslError.exception(
module: module,
message:
non_atomic_message(
module,
action.name,
"the attributes `#{inspect(not_atomic_attributes)}` cannot be updated atomically"
),
path: [:actions, action.name]
)
]
end
warnings ++ change_warnings ++ notifier_warnings ++ attribute_warnings
end)
{:warn, Enum.map(warnings, &Exception.message/1)}
end
defp data_layer_can_atomic?(resource, %{type: :update}) do
Ash.DataLayer.can?(:update_query, resource)
end
defp data_layer_can_atomic?(resource, %{type: :destroy}) do
Ash.DataLayer.can?(:destroy_query, resource)
end
defp not_atomic?(Ash.Type.Union, _), do: true
defp not_atomic?(_, _), do: false
defp non_atomic_message(module, action_name, reason) do
"""
`#{inspect(module)}.#{action_name}` cannot be done atomically, because #{reason}
You must either address the issue or set `require_atomic? false` on `#{inspect(module)}.#{action_name}`.
"""
end
end