Skip to content

Commit 96e3b04

Browse files
authored
Reverse order of values when upserting (#1318)
1 parent de34235 commit 96e3b04

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
#### Fixed
4+
5+
- [#1318](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1318) Reverse order of values when upserting
6+
17
## v8.0.5
28

39
#### Added

lib/active_record/connection_adapters/sqlserver/database_statements.rb

+2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ def build_insert_sql(insert) # :nodoc:
159159

160160

161161
def build_sql_for_merge_insert(insert:, insert_all:, columns_with_uniqueness_constraints:) # :nodoc:
162+
insert_all.inserts.reverse! if insert.update_duplicates?
163+
162164
sql = <<~SQL
163165
MERGE INTO #{insert.model.quoted_table_name} WITH (UPDLOCK, HOLDLOCK) AS target
164166
USING (
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require "cases/helper_sqlserver"
4+
require "models/book"
5+
6+
class InsertAllTestSQLServer < ActiveRecord::TestCase
7+
# Test ported from the Rails `main` branch that is not on the `8-0-stable` branch.
8+
def test_insert_all_only_applies_last_value_when_given_duplicate_identifiers
9+
skip unless supports_insert_on_duplicate_skip?
10+
11+
Book.insert_all [
12+
{ id: 111, name: "expected_new_name" },
13+
{ id: 111, name: "unexpected_new_name" }
14+
]
15+
assert_equal "expected_new_name", Book.find(111).name
16+
end
17+
18+
# Test ported from the Rails `main` branch that is not on the `8-0-stable` branch.
19+
def test_upsert_all_only_applies_last_value_when_given_duplicate_identifiers
20+
skip unless supports_insert_on_duplicate_update? && !current_adapter?(:PostgreSQLAdapter)
21+
22+
Book.create!(id: 112, name: "original_name")
23+
24+
Book.upsert_all [
25+
{ id: 112, name: "unexpected_new_name" },
26+
{ id: 112, name: "expected_new_name" }
27+
]
28+
assert_equal "expected_new_name", Book.find(112).name
29+
end
30+
end

0 commit comments

Comments
 (0)