Skip to content

Commit e99c33d

Browse files
authored
improvement: support {:unsafe_fragment, ".."} as a conflict target (#129)
this is supported by other sql data layers, for example. This is necessary for AshSqlite3's upsert behavior. Postgres adapter implementation: https://github.com/elixir-ecto/ecto_sql/blob/v3.10.2/lib/ecto/adapters/postgres/connection.ex#L212C4-L213C26
1 parent f9fdfa5 commit e99c33d

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/ecto/adapters/sqlite3/connection.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,9 @@ defmodule Ecto.Adapters.SQLite3.Connection do
748748

749749
defp conflict_target([]), do: ""
750750

751+
defp conflict_target({:unsafe_fragment, fragment}),
752+
do: [fragment, ?\s]
753+
751754
defp conflict_target(targets) do
752755
[?(, intersperse_map(targets, ?,, &quote_name/1), ?), ?\s]
753756
end

test/ecto/adapters/sqlite3/connection/insert_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ defmodule Ecto.Adapters.SQLite3.Connection.InsertTest do
6060
assert query ==
6161
~s{INSERT INTO "schema" AS s0 ("x","y") VALUES (?,?) ON CONFLICT ("x","y") DO UPDATE SET "z" = 'foo' RETURNING "z"}
6262

63+
# For :unsafe_fragment
64+
update = from("schema", update: [set: [z: "foo"]]) |> plan(:update_all)
65+
66+
query =
67+
insert(
68+
nil,
69+
"schema",
70+
[:x, :y],
71+
[[:x, :y]],
72+
{update, [], {:unsafe_fragment, "foobar"}},
73+
[:z]
74+
)
75+
76+
assert query ==
77+
~s{INSERT INTO "schema" AS s0 ("x","y") VALUES (?,?) ON CONFLICT foobar DO UPDATE SET "z" = 'foo' RETURNING "z"}
78+
6379
assert_raise ArgumentError, "Upsert in SQLite3 requires :conflict_target", fn ->
6480
conflict_target = []
6581

0 commit comments

Comments
 (0)