Skip to content

Commit 6049a22

Browse files
authored
Merge pull request rails#49890 from fatkodima/fix-mariadb-on_duplicate-skip
Do not use `INSERT IGNORE` when upserting to MariaDB
2 parents 8e73e62 + eecd672 commit 6049a22

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -639,11 +639,12 @@ def default_index_type?(index) # :nodoc:
639639
end
640640

641641
def build_insert_sql(insert) # :nodoc:
642-
sql = +"INSERT"
643-
sql << " IGNORE" if insert.skip_duplicates?
644-
sql << " #{insert.into} #{insert.values_list}"
642+
sql = +"INSERT #{insert.into} #{insert.values_list}"
645643

646-
if insert.update_duplicates?
644+
if insert.skip_duplicates?
645+
no_op_column = quote_column_name(insert.keys.first)
646+
sql << " ON DUPLICATE KEY UPDATE #{no_op_column}=#{no_op_column}"
647+
elsif insert.update_duplicates?
647648
sql << " ON DUPLICATE KEY UPDATE "
648649
if insert.raw_update_sql?
649650
sql << insert.raw_update_sql

activerecord/test/cases/insert_all_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ def test_insert_all_can_skip_duplicate_records
129129
end
130130
end
131131

132+
if current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
133+
def test_insert_all_generates_correct_sql
134+
skip unless supports_insert_on_duplicate_skip?
135+
136+
assert_sql(/ON DUPLICATE KEY UPDATE/) do
137+
Book.insert_all [{ id: 1, name: "Agile Web Development with Rails" }]
138+
end
139+
end
140+
end
141+
132142
def test_insert_all_with_skip_duplicates_and_autonumber_id_not_given
133143
skip unless supports_insert_on_duplicate_skip?
134144

0 commit comments

Comments
 (0)