You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+22Lines changed: 22 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -174,6 +174,28 @@ Book.bulk_insert(*destination_columns, update_duplicates: %w[title]) do |worker|
174
174
end
175
175
```
176
176
177
+
### Ignored Columns On Update (MySQL, PostgreSQL)
178
+
179
+
When there is a conflict in _insert_ and you want to instead update those records you would be using the _update_duplicates_ option. Along with that you can also ignore certain columns which you don't want to be updated. For example you may want to ignore `created_at` field during the update.
Copy file name to clipboardExpand all lines: test/bulk_insert/worker_test.rb
+34Lines changed: 34 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -418,4 +418,38 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
418
418
419
419
assert_equalmysql_worker.compose_insert_query,"INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,0,NULL,NULL,'chartreuse') ON DUPLICATE KEY UPDATE `greeting`=VALUES(`greeting`), `age`=VALUES(`age`), `happy`=VALUES(`happy`), `created_at`=VALUES(`created_at`), `updated_at`=VALUES(`updated_at`), `color`=VALUES(`color`)"
420
420
end
421
+
422
+
test"mysql adapter can ignore specific columns when updating duplicates"do
423
+
mysql_worker=BulkInsert::Worker.new(
424
+
Testing.connection,
425
+
Testing.table_name,
426
+
'id',
427
+
%w(greetingagehappycreated_atupdated_atcolor),
428
+
500,# batch size
429
+
false,# ignore
430
+
true,# update_duplicates
431
+
false,# return_primary_keys
432
+
%w(created_at))# ignored_columns_on_update
433
+
mysql_worker.adapter_name='MySQL'
434
+
mysql_worker.add["Yo",15,false,nil,nil]
435
+
436
+
assert_equalmysql_worker.compose_insert_query,"INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,0,NULL,NULL,'chartreuse') ON DUPLICATE KEY UPDATE `greeting`=VALUES(`greeting`), `age`=VALUES(`age`), `happy`=VALUES(`happy`), `updated_at`=VALUES(`updated_at`), `color`=VALUES(`color`)"
437
+
end
438
+
439
+
test"pgsql_worker adapter can ignore specific columns when updating duplicates"do
440
+
pgsql_worker=BulkInsert::Worker.new(
441
+
Testing.connection,
442
+
Testing.table_name,
443
+
'id',
444
+
%w(greetingagehappycreated_atupdated_atcolor),
445
+
500,# batch size
446
+
false,# ignore
447
+
%w(greeting),# update_duplicates
448
+
false,# return_primary_keys
449
+
%w(created_at))# ignored_columns_on_update
450
+
pgsql_worker.adapter_name='PostgreSQL'
451
+
pgsql_worker.add["Yo",15,false,nil,nil]
452
+
453
+
assert_equalpgsql_worker.compose_insert_query,"INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,0,NULL,NULL,'chartreuse') ON CONFLICT(greeting) DO UPDATE SET greeting=EXCLUDED.greeting, age=EXCLUDED.age, happy=EXCLUDED.happy, updated_at=EXCLUDED.updated_at, color=EXCLUDED.color"
0 commit comments