Skip to content

Commit 0ffca03

Browse files
committed
fix: more handling of storage types for typed struct arrays in AshSql
1 parent af7a51a commit 0ffca03

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

lib/atomics.ex

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,20 @@ defmodule AshSql.Atomics do
4545
)
4646
end
4747

48+
type =
49+
case query.__ash_bindings__.sql_behaviour.storage_type(resource, attribute.name) do
50+
nil -> {attribute.type, attribute.constraints}
51+
storage_type -> storage_type
52+
end
53+
4854
case AshSql.Expr.dynamic_expr(
4955
query,
5056
expr,
5157
Map.merge(query.__ash_bindings__, %{
5258
location: :update
5359
}),
54-
false
60+
false,
61+
type
5562
) do
5663
{dynamic, acc} ->
5764
new_field = String.to_atom("__new_#{field}")
@@ -134,7 +141,12 @@ defmodule AshSql.Atomics do
134141
cond do
135142
is_list(expr) and typed_struct_array_attr_type?(attribute.type) and
136143
storage_type in [:map, :jsonb, :json] ->
137-
dump_and_encode_typed_struct_array(expr, attribute, storage_type)
144+
dump_and_encode_map_array(expr, attribute)
145+
146+
is_list(expr) and embedded_ash_resource?(Enum.at(expr, 0)) and
147+
storage_type in [:map, :jsonb, :json] ->
148+
# Embedded resources with jsonb storage need to be dumped to native format
149+
dump_and_encode_map_array(expr, attribute)
138150

139151
is_list(expr) and not embedded_ash_resource?(Enum.at(expr, 0)) ->
140152
{:ok, casted} =
@@ -158,11 +170,7 @@ defmodule AshSql.Atomics do
158170
Ash.Resource.Info.embedded?(value.__struct__)
159171
end
160172

161-
defp dump_and_encode_typed_struct_array(
162-
expr,
163-
%{type: {:array, inner_type}} = attribute,
164-
storage_type
165-
) do
173+
defp dump_and_encode_map_array(expr, %{type: {:array, inner_type}} = attribute) do
166174
dumped_list =
167175
Enum.map(expr, fn item ->
168176
case Ash.Type.dump_to_native(inner_type, item, attribute.constraints[:items] || []) do
@@ -171,15 +179,10 @@ defmodule AshSql.Atomics do
171179
end
172180
end)
173181

174-
casting_type =
175-
if storage_type in [:map, :jsonb, :json],
176-
do: AshSql.TypedStructArrayJsonb,
177-
else: storage_type || attribute.type
178-
179182
{:ok, type_expr} =
180183
Ash.Query.Function.Type.new([
181184
dumped_list,
182-
casting_type,
185+
AshSql.TypedStructArrayJsonb,
183186
attribute.constraints
184187
])
185188

@@ -288,13 +291,20 @@ defmodule AshSql.Atomics do
288291
)
289292
end
290293

294+
type =
295+
case query.__ash_bindings__.sql_behaviour.storage_type(resource, attribute.name) do
296+
nil -> {attribute.type, attribute.constraints}
297+
storage_type -> storage_type
298+
end
299+
291300
case AshSql.Expr.dynamic_expr(
292301
query,
293302
expr,
294303
Map.merge(query.__ash_bindings__, %{
295304
location: :update
296305
}),
297-
false
306+
false,
307+
type
298308
) do
299309
{dynamic, acc} ->
300310
{:cont,

0 commit comments

Comments
 (0)