Skip to content

Commit

Permalink
fix: ensure there is always at least one upsert field so filter is run
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Dec 26, 2024
1 parent 39033fb commit 452a820
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
18 changes: 10 additions & 8 deletions lib/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,7 @@ defmodule AshPostgres.DataLayer do
ecto_changeset.changes,
[]
) do
:empty ->
{:empty, query} ->
if options[:return_records?] do
if changeset.context[:data_layer][:use_atomic_update_data?] do
case query.__ash_bindings__ do
Expand Down Expand Up @@ -1821,12 +1821,8 @@ defmodule AshPostgres.DataLayer do
%{},
upsert_set
) do
:empty ->
if options[:return_records?] do
{:replace, options[:upsert_keys] || Ash.Resource.Info.primary_key(resource)}
else
:nothing
end
{:empty, _query} ->
raise "Cannot upsert with no fields to specify in the upsert statement. This can only happen on resources without a primary key."

{:ok, query} ->
query
Expand Down Expand Up @@ -1967,7 +1963,13 @@ defmodule AshPostgres.DataLayer do

fields_to_upsert =
upsert_fields --
(Keyword.keys(Enum.at(changesets, 0).atomics) -- keys)
Keyword.keys(Enum.at(changesets, 0).atomics) -- keys

fields_to_upsert =
case fields_to_upsert do
[] -> keys
fields_to_upsert -> fields_to_upsert
end

fields_to_upsert
|> Enum.uniq()
Expand Down
2 changes: 1 addition & 1 deletion lib/mix/tasks/ash_postgres.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ if Code.ensure_loaded?(Igniter) do
end
end

defp configure_min_pg_version_function(zipper, repo, version, opts) do
defp configure_min_pg_version_function(zipper, _repo, _version, _opts) do
case Igniter.Code.Function.move_to_def(zipper, :min_pg_version, 0) do
{:ok, zipper} ->
{:ok, zipper}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ defmodule AshPostgres.MixProject do
defp deps do
[
{:ash, ash_version("~> 3.4 and >= 3.4.48")},
{:ash_sql, ash_sql_version("~> 0.2 and >= 0.2.40")},
{:ash_sql, ash_sql_version("~> 0.2 and >= 0.2.43")},
{:igniter, "~> 0.4 and >= 0.4.4", optional: true},
{:ecto_sql, "~> 3.12"},
{:ecto, "~> 3.12 and >= 3.12.1"},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%{
"ash": {:hex, :ash, "3.4.49", "85d1e115058ce6dbd9111b66ed55e2425dc0cfda874b60ed3f49299996326a72", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:igniter, ">= 0.4.8 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:owl, "~> 0.11", [hex: :owl, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, "~> 0.9", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.2.29 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, ">= 0.2.6 and < 1.0.0-0", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 1.0", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ef2573a43b035e2c3bdee614522a33845f9904d07c519cac801972102f5ca057"},
"ash_sql": {:hex, :ash_sql, "0.2.42", "01c160f1cbcce2bad534fd8372a3ce04b1b88bb9348c59aab6755f76963f444e", [:mix], [{:ash, ">= 3.1.7 and < 4.0.0-0", [hex: :ash, repo: "hexpm", optional: false]}, {:ecto, "~> 3.9", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.9", [hex: :ecto_sql, repo: "hexpm", optional: false]}], "hexpm", "75ca2d3265b529b2441d0dc300a0b18c20a18f766a1d7cc97cf5904ce3bd9813"},
"ash_sql": {:hex, :ash_sql, "0.2.43", "80579d708630d3d31e9bb74b74d16ff8ef70de8c1d5043d87a682b5b17cb3154", [:mix], [{:ash, ">= 3.1.7 and < 4.0.0-0", [hex: :ash, repo: "hexpm", optional: false]}, {:ecto, "~> 3.9", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.9", [hex: :ecto_sql, repo: "hexpm", optional: false]}], "hexpm", "c3fc710129178c017002bd108fc9411b10ca1d276fa5e99bd01b99addf28ff42"},
"benchee": {:hex, :benchee, "1.3.1", "c786e6a76321121a44229dde3988fc772bca73ea75170a73fd5f4ddf1af95ccf", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "76224c58ea1d0391c8309a8ecbfe27d71062878f59bd41a390266bf4ac1cc56d"},
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
"credo": {:hex, :credo, "1.7.10", "6e64fe59be8da5e30a1b96273b247b5cf1cc9e336b5fd66302a64b25749ad44d", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "71fbc9a6b8be21d993deca85bf151df023a3097b01e09a2809d460348561d8cd"},
Expand Down

0 comments on commit 452a820

Please sign in to comment.