Skip to content

Reverse order of values when upserting #1318

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 3 commits into from
Mar 24, 2025
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

#### Fixed

- [#1318](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1318) Reverse order of values when upserting

## v8.0.5

#### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def build_insert_sql(insert) # :nodoc:


def build_sql_for_merge_insert(insert:, insert_all:, columns_with_uniqueness_constraints:) # :nodoc:
insert_all.inserts.reverse! if insert.update_duplicates?

sql = <<~SQL
MERGE INTO #{insert.model.quoted_table_name} WITH (UPDLOCK, HOLDLOCK) AS target
USING (
Expand Down
30 changes: 30 additions & 0 deletions test/cases/insert_all_test_sqlserver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require "cases/helper_sqlserver"
require "models/book"

class InsertAllTestSQLServer < ActiveRecord::TestCase
# Test ported from the Rails `main` branch that is not on the `8-0-stable` branch.
def test_insert_all_only_applies_last_value_when_given_duplicate_identifiers
skip unless supports_insert_on_duplicate_skip?

Book.insert_all [
{ id: 111, name: "expected_new_name" },
{ id: 111, name: "unexpected_new_name" }
]
assert_equal "expected_new_name", Book.find(111).name
end

# Test ported from the Rails `main` branch that is not on the `8-0-stable` branch.
def test_upsert_all_only_applies_last_value_when_given_duplicate_identifiers
skip unless supports_insert_on_duplicate_update? && !current_adapter?(:PostgreSQLAdapter)

Book.create!(id: 112, name: "original_name")

Book.upsert_all [
{ id: 112, name: "unexpected_new_name" },
{ id: 112, name: "expected_new_name" }
]
assert_equal "expected_new_name", Book.find(112).name
end
end