Skip to content
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

insert_all with source ( update syntax) #4469

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tests
  • Loading branch information
greg-rychlewski committed Aug 1, 2024
commit ec1874c6f90f90b1784af2b7669771b8d88b415d
46 changes: 46 additions & 0 deletions integration_test/cases/repo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 969,52 @@ defmodule Ecto.Integration.RepoTest do
source = from p in Permalink, select: %{url: p.title, post_id: ^post_id}
assert {1, _} = TestRepo.insert_all(Permalink, source)
end

@tag :upsert_all
@tag :with_conflict_target
@tag :returning
test "insert_all with query and source" do
josevalim marked this conversation as resolved.
Show resolved Hide resolved
{:ok, %Comment{id: id, text: text}} = TestRepo.insert(%Comment{text: "comment1_text"})
source = from c in Comment, select: c, where: c.id == ^id

assert {1, [%Comment{id: ^id, text: ^text}]} =
TestRepo.insert_all(Comment, source,
conflict_target: [:id],
on_conflict: :replace_all,
returning: true
)
end

@tag :upsert_all
@tag :with_conflict_target
@tag :returning
test "insert_all with query and source with update syntax" do
{:ok, %Comment{id: id, text: text, lock_version: version}} =
TestRepo.insert(%Comment{text: "comment1_text", lock_version: 1})

# select param
new_lock_version = version 1

source =
from c in Comment, select: %{c | lock_version: ^new_lock_version}, where: c.id == ^id

assert {1, [%Comment{lock_version: ^new_lock_version, text: ^text}]} =
TestRepo.insert_all(Comment, source,
conflict_target: [:id],
on_conflict: :replace_all,
returning: true
)

# select literal
source = from c in Comment, select: %{c | lock_version: 2}, where: c.id == ^id

assert {1, [%Comment{lock_version: 2, text: ^text}]} =
TestRepo.insert_all(Comment, source,
conflict_target: [:id],
on_conflict: :replace_all,
returning: true
)
end
end

@tag :invalid_prefix
Expand Down