Skip to content

Commit 1159592

Browse files
committed
Raise if conflict_target it not set on upsert for PG
1 parent d701128 commit 1159592

4 files changed

Lines changed: 13 additions & 7 deletions

File tree

lib/ecto/adapters/myxql/connection.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ if Code.ensure_loaded?(MyXQL) do
153153
end
154154

155155
defp on_conflict({_, _, [_ | _]}, _header) do
156-
error!(nil, "The :conflict_target option is not supported in insert/insert_all by MySQL")
156+
error!(nil, ":conflict_target is not supported in insert/insert_all by MySQL")
157157
end
158158
defp on_conflict({:raise, _, []}, _header) do
159159
[]

lib/ecto/adapters/postgres/connection.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,14 @@ if Code.ensure_loaded?(Postgrex) do
178178
defp on_conflict({:nothing, _, targets}, _header),
179179
do: [" ON CONFLICT ", conflict_target(targets) | "DO NOTHING"]
180180
defp on_conflict({fields, _, targets}, _header) when is_list(fields),
181-
do: [" ON CONFLICT ", conflict_target(targets), "DO " | replace(fields)]
181+
do: [" ON CONFLICT ", conflict_target!(targets), "DO " | replace(fields)]
182182
defp on_conflict({query, _, targets}, _header),
183-
do: [" ON CONFLICT ", conflict_target(targets), "DO " | update_all(query, "UPDATE SET ")]
183+
do: [" ON CONFLICT ", conflict_target!(targets), "DO " | update_all(query, "UPDATE SET ")]
184+
185+
defp conflict_target!([]),
186+
do: error!(nil, "the :conflict_target option is required on upserts by PostgreSQL")
187+
defp conflict_target!(target),
188+
do: conflict_target(target)
184189

185190
defp conflict_target({:constraint, constraint}),
186191
do: ["ON CONSTRAINT ", quote_name(constraint), ?\s]

test/ecto/adapters/myxql_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ defmodule Ecto.Adapters.MyXQLTest do
958958
query = insert(nil, "schema", [:x, :y], [[:x, :y]], {[:x, :y], [], []}, [])
959959
assert query == ~s{INSERT INTO `schema` (`x`,`y`) VALUES (?,?) ON DUPLICATE KEY UPDATE `x` = VALUES(`x`),`y` = VALUES(`y`)}
960960

961-
assert_raise ArgumentError, "The :conflict_target option is not supported in insert/insert_all by MySQL", fn ->
961+
assert_raise ArgumentError, ":conflict_target is not supported in insert/insert_all by MySQL", fn ->
962962
insert(nil, "schema", [:x, :y], [[:x, :y]], {[:x, :y], [], [:x]}, [])
963963
end
964964

test/ecto/adapters/postgres_test.exs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,14 +1142,15 @@ defmodule Ecto.Adapters.PostgresTest do
11421142
query = insert(nil, "schema", [:x, :y], [[:x, :y]], {[:x, :y], [], [:id]}, [])
11431143
assert query == ~s{INSERT INTO "schema" ("x","y") VALUES ($1,$2) ON CONFLICT ("id") DO UPDATE SET "x" = EXCLUDED."x","y" = EXCLUDED."y"}
11441144

1145-
query = insert(nil, "schema", [:x, :y], [[:x, :y]], {[:x, :y], [], []}, [])
1146-
assert query == ~s{INSERT INTO "schema" ("x","y") VALUES ($1,$2) ON CONFLICT DO UPDATE SET "x" = EXCLUDED."x","y" = EXCLUDED."y"}
1147-
11481145
query = insert(nil, "schema", [:x, :y], [[:x, :y]], {[:x, :y], [], {:constraint, :foo}}, [])
11491146
assert query == ~s{INSERT INTO "schema" ("x","y") VALUES ($1,$2) ON CONFLICT ON CONSTRAINT \"foo\" DO UPDATE SET "x" = EXCLUDED."x","y" = EXCLUDED."y"}
11501147

11511148
query = insert(nil, "schema", [:x, :y], [[:x, :y]], {[:x, :y], [], {:unsafe_fragment, "(\"id\")"}}, [])
11521149
assert query == ~s{INSERT INTO "schema" ("x","y") VALUES ($1,$2) ON CONFLICT (\"id\") DO UPDATE SET "x" = EXCLUDED."x","y" = EXCLUDED."y"}
1150+
1151+
assert_raise ArgumentError, "the :conflict_target option is required on upserts by PostgreSQL", fn ->
1152+
insert(nil, "schema", [:x, :y], [[:x, :y]], {[:x, :y], [], []}, [])
1153+
end
11531154
end
11541155

11551156
test "insert with query" do

0 commit comments

Comments
 (0)