Skip to content

improvement: support {:unsafe_fragment, ".."} as a conflict target #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/ecto/adapters/sqlite3/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,9 @@ defmodule Ecto.Adapters.SQLite3.Connection do

defp conflict_target([]), do: ""

defp conflict_target({:unsafe_fragment, fragment}),
do: [fragment, ?\s]

defp conflict_target(targets) do
[?(, intersperse_map(targets, ?,, &quote_name/1), ?), ?\s]
end
Expand Down
16 changes: 16 additions & 0 deletions test/ecto/adapters/sqlite3/connection/insert_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ defmodule Ecto.Adapters.SQLite3.Connection.InsertTest do
assert query ==
~s{INSERT INTO "schema" AS s0 ("x","y") VALUES (?,?) ON CONFLICT ("x","y") DO UPDATE SET "z" = 'foo' RETURNING "z"}

# For :unsafe_fragment
update = from("schema", update: [set: [z: "foo"]]) |> plan(:update_all)

query =
insert(
nil,
"schema",
[:x, :y],
[[:x, :y]],
{update, [], {:unsafe_fragment, "foobar"}},
[:z]
)

assert query ==
~s{INSERT INTO "schema" AS s0 ("x","y") VALUES (?,?) ON CONFLICT foobar DO UPDATE SET "z" = 'foo' RETURNING "z"}

assert_raise ArgumentError, "Upsert in SQLite3 requires :conflict_target", fn ->
conflict_target = []

Expand Down