From ce69e834d0596d4ad244480c530888bf57c1e566 Mon Sep 17 00:00:00 2001 From: Nikita Anistratenko Date: Mon, 30 Dec 2019 15:15:51 +0300 Subject: [PATCH 1/5] added update_column_names --- .idea/.gitignore | 2 + .idea/.rakeTasks | 7 ++++ .idea/bulk_insert.iml | 16 ++++++++ .idea/inspectionProfiles/Project_Default.xml | 39 ++++++++++++++++++++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ README.md | 10 ++++- lib/bulk_insert.rb | 4 +- lib/bulk_insert/worker.rb | 7 ++-- test/bulk_insert/worker_test.rb | 37 ++++++++++++++++++- 11 files changed, 132 insertions(+), 8 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/.rakeTasks create mode 100644 .idea/bulk_insert.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/.rakeTasks b/.idea/.rakeTasks new file mode 100644 index 0000000..c6865d9 --- /dev/null +++ b/.idea/.rakeTasks @@ -0,0 +1,7 @@ + + diff --git a/.idea/bulk_insert.iml b/.idea/bulk_insert.iml new file mode 100644 index 0000000..119a19e --- /dev/null +++ b/.idea/bulk_insert.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..b4ba2a5 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,39 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..72ee4bf --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..4e903a8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 969ec43..7eef88e 100644 --- a/README.md +++ b/README.md @@ -157,18 +157,24 @@ then you can use the _update_duplicates_ option. Set this option to true is found the row will be updated with your new values. Default value for this option is false. +You can optionally declare specific column list for update duplicates +statement, use the _update_column_names_ option, then only these columns +will be updated. Be default, if option _update_column_names_ not passed, +used column list from _column_names_ option. + ```ruby destination_columns = [:title, :author] +update_columns = [:title] # Update duplicate rows (MySQL) -Book.bulk_insert(*destination_columns, update_duplicates: true) do |worker| +Book.bulk_insert(*destination_columns, update_duplicates: true, update_column_names: update_columns) do |worker| worker.add(...) worker.add(...) # ... end # Update duplicate rows (PostgreSQL) -Book.bulk_insert(*destination_columns, update_duplicates: %w[title]) do |worker| +Book.bulk_insert(*destination_columns, update_duplicates: %w[title], update_column_names: update_columns) do |worker| worker.add(...) # ... end diff --git a/lib/bulk_insert.rb b/lib/bulk_insert.rb index 7907aff..7964d6a 100644 --- a/lib/bulk_insert.rb +++ b/lib/bulk_insert.rb @@ -4,9 +4,9 @@ module BulkInsert extend ActiveSupport::Concern module ClassMethods - def bulk_insert(*columns, values: nil, set_size:500, ignore: false, update_duplicates: false, return_primary_keys: false) + def bulk_insert(*columns, values: nil, set_size:500, ignore: false, update_duplicates: false, return_primary_keys: false, update_column_names: nil) columns = default_bulk_columns if columns.empty? - worker = BulkInsert::Worker.new(connection, table_name, primary_key, columns, set_size, ignore, update_duplicates, return_primary_keys) + worker = BulkInsert::Worker.new(connection, table_name, primary_key, columns, set_size, ignore, update_duplicates, return_primary_keys, update_column_names) if values.present? transaction do diff --git a/lib/bulk_insert/worker.rb b/lib/bulk_insert/worker.rb index a16a938..377253f 100644 --- a/lib/bulk_insert/worker.rb +++ b/lib/bulk_insert/worker.rb @@ -7,7 +7,7 @@ class Worker attr_accessor :adapter_name attr_reader :ignore, :update_duplicates, :result_sets - def initialize(connection, table_name, primary_key, column_names, set_size=500, ignore=false, update_duplicates=false, return_primary_keys=false) + def initialize(connection, table_name, primary_key, column_names, set_size=500, ignore=false, update_duplicates=false, return_primary_keys=false, update_column_names=nil) @connection = connection @set_size = set_size @@ -24,6 +24,7 @@ def initialize(connection, table_name, primary_key, column_names, set_size=500, @columns = column_names.map { |name| column_map[name.to_s] } @table_name = connection.quote_table_name(table_name) @column_names = column_names.map { |name| connection.quote_column_name(name) }.join(",") + @update_column_names = update_column_names.nil? ? @column_names : @update_column_names @before_save_callback = nil @after_save_callback = nil @@ -154,12 +155,12 @@ def on_conflict_statement if is_postgres && ignore ' ON CONFLICT DO NOTHING' elsif is_postgres && update_duplicates - update_values = @columns.map do |column| + update_values = @update_column_names.map do |column| "#{column.name}=EXCLUDED.#{column.name}" end.join(', ') ' ON CONFLICT(' + update_duplicates.join(', ') + ') DO UPDATE SET ' + update_values elsif adapter_name =~ /^mysql/i && update_duplicates - update_values = @columns.map do |column| + update_values = @update_column_names.map do |column| "`#{column.name}`=VALUES(`#{column.name}`)" end.join(', ') ' ON DUPLICATE KEY UPDATE ' + update_values diff --git a/test/bulk_insert/worker_test.rb b/test/bulk_insert/worker_test.rb index 735972b..c939d03 100644 --- a/test/bulk_insert/worker_test.rb +++ b/test/bulk_insert/worker_test.rb @@ -342,7 +342,7 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase assert_equal pgsql_worker.compose_insert_query, "INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,0,NULL,NULL,'chartreuse') ON CONFLICT DO NOTHING RETURNING id" end - test "adapter dependent postgresql methods (with update_duplicates)" do + test "adapter dependent postgresql methods (with update_duplicates, without update_column_names)" do pgsql_worker = BulkInsert::Worker.new( Testing.connection, Testing.table_name, @@ -359,6 +359,24 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase assert_equal pgsql_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, age, happy) DO UPDATE SET greeting=EXCLUDED.greeting, age=EXCLUDED.age, happy=EXCLUDED.happy, created_at=EXCLUDED.created_at, updated_at=EXCLUDED.updated_at, color=EXCLUDED.color RETURNING id" end + test "adapter dependent postgresql methods (with update_duplicates, with update_column_names)" do + pgsql_worker = BulkInsert::Worker.new( + Testing.connection, + Testing.table_name, + 'id', + %w(greeting age happy created_at updated_at color), + 500, # batch size + false, # ignore + %w(greeting age happy), # update duplicates + true, # return primary keys, + %w(greeting age happy updated_at) # update column names + ) + pgsql_worker.adapter_name = 'PostgreSQL' + pgsql_worker.add ["Yo", 15, false, nil, nil] + + assert_equal pgsql_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, age, happy) DO UPDATE SET greeting=EXCLUDED.greeting, age=EXCLUDED.age, happy=EXCLUDED.happy, updated_at=EXCLUDED.updated_at RETURNING id" + end + test "adapter dependent PostGIS methods" do pgsql_worker = BulkInsert::Worker.new( Testing.connection, @@ -418,4 +436,21 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase assert_equal mysql_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`)" end + + test "mysql adapter can update duplicates (with update_column_names)" do + mysql_worker = BulkInsert::Worker.new( + Testing.connection, + Testing.table_name, + 'id', + %w(greeting age happy created_at updated_at color), + 500, # batch size + false, # ignore + true, # update_duplicates + %w(greeting age happy updated_at) + ) + mysql_worker.adapter_name = 'MySQL' + mysql_worker.add ["Yo", 15, false, nil, nil] + + assert_equal mysql_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`)" + end end From be8f419b4d285652a90931f45774dbcafd075598 Mon Sep 17 00:00:00 2001 From: Nikita Anistratenko Date: Mon, 30 Dec 2019 15:16:12 +0300 Subject: [PATCH 2/5] remove .idea files --- .idea/.gitignore | 2 - .idea/.rakeTasks | 7 ---- .idea/bulk_insert.iml | 16 -------- .idea/inspectionProfiles/Project_Default.xml | 39 -------------------- .idea/misc.xml | 4 -- .idea/modules.xml | 8 ---- .idea/vcs.xml | 6 --- 7 files changed, 82 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/.rakeTasks delete mode 100644 .idea/bulk_insert.iml delete mode 100644 .idea/inspectionProfiles/Project_Default.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 5c98b42..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# Default ignored files -/workspace.xml \ No newline at end of file diff --git a/.idea/.rakeTasks b/.idea/.rakeTasks deleted file mode 100644 index c6865d9..0000000 --- a/.idea/.rakeTasks +++ /dev/null @@ -1,7 +0,0 @@ - - diff --git a/.idea/bulk_insert.iml b/.idea/bulk_insert.iml deleted file mode 100644 index 119a19e..0000000 --- a/.idea/bulk_insert.iml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index b4ba2a5..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 72ee4bf..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 4e903a8..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 4c24055795d6875af8ae1b2c9b91c4cd6683b4f5 Mon Sep 17 00:00:00 2001 From: Nikita Anistratenko Date: Mon, 30 Dec 2019 15:18:14 +0300 Subject: [PATCH 3/5] update README and Worker fix --- .idea/.gitignore | 2 + .idea/.rakeTasks | 7 ++++ .idea/bulk_insert.iml | 16 ++++++++ .idea/inspectionProfiles/Project_Default.xml | 39 ++++++++++++++++++++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ README.md | 2 +- lib/bulk_insert/worker.rb | 2 +- 9 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/.rakeTasks create mode 100644 .idea/bulk_insert.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/.rakeTasks b/.idea/.rakeTasks new file mode 100644 index 0000000..c6865d9 --- /dev/null +++ b/.idea/.rakeTasks @@ -0,0 +1,7 @@ + + diff --git a/.idea/bulk_insert.iml b/.idea/bulk_insert.iml new file mode 100644 index 0000000..119a19e --- /dev/null +++ b/.idea/bulk_insert.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..b4ba2a5 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,39 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..72ee4bf --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..4e903a8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 7eef88e..868a275 100644 --- a/README.md +++ b/README.md @@ -158,7 +158,7 @@ is found the row will be updated with your new values. Default value for this option is false. You can optionally declare specific column list for update duplicates -statement, use the _update_column_names_ option, then only these columns +statement. Use the _update_column_names_ option, then only these columns will be updated. Be default, if option _update_column_names_ not passed, used column list from _column_names_ option. diff --git a/lib/bulk_insert/worker.rb b/lib/bulk_insert/worker.rb index 377253f..962d699 100644 --- a/lib/bulk_insert/worker.rb +++ b/lib/bulk_insert/worker.rb @@ -24,7 +24,7 @@ def initialize(connection, table_name, primary_key, column_names, set_size=500, @columns = column_names.map { |name| column_map[name.to_s] } @table_name = connection.quote_table_name(table_name) @column_names = column_names.map { |name| connection.quote_column_name(name) }.join(",") - @update_column_names = update_column_names.nil? ? @column_names : @update_column_names + @update_column_names = update_column_names.nil? ? @column_names : update_column_names @before_save_callback = nil @after_save_callback = nil From 920ac4375c5ab0d50f3b1af5a2f1f0e060dab2d0 Mon Sep 17 00:00:00 2001 From: Nikita Anistratenko Date: Mon, 30 Dec 2019 15:18:30 +0300 Subject: [PATCH 4/5] remove .idea files --- .idea/.gitignore | 2 - .idea/.rakeTasks | 7 ---- .idea/bulk_insert.iml | 16 -------- .idea/inspectionProfiles/Project_Default.xml | 39 -------------------- .idea/misc.xml | 4 -- .idea/modules.xml | 8 ---- .idea/vcs.xml | 6 --- 7 files changed, 82 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/.rakeTasks delete mode 100644 .idea/bulk_insert.iml delete mode 100644 .idea/inspectionProfiles/Project_Default.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 5c98b42..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# Default ignored files -/workspace.xml \ No newline at end of file diff --git a/.idea/.rakeTasks b/.idea/.rakeTasks deleted file mode 100644 index c6865d9..0000000 --- a/.idea/.rakeTasks +++ /dev/null @@ -1,7 +0,0 @@ - - diff --git a/.idea/bulk_insert.iml b/.idea/bulk_insert.iml deleted file mode 100644 index 119a19e..0000000 --- a/.idea/bulk_insert.iml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index b4ba2a5..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 72ee4bf..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 4e903a8..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From e822b7290266045c6b7d3df09f16a044a09deac6 Mon Sep 17 00:00:00 2001 From: Nikita Anistratenko Date: Mon, 30 Dec 2019 15:46:23 +0300 Subject: [PATCH 5/5] use column_map for update_columns --- README.md | 10 +++++----- lib/bulk_insert.rb | 4 ++-- lib/bulk_insert/worker.rb | 8 ++++---- test/bulk_insert/worker_test.rb | 9 +++++---- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 868a275..90375bd 100644 --- a/README.md +++ b/README.md @@ -158,23 +158,23 @@ is found the row will be updated with your new values. Default value for this option is false. You can optionally declare specific column list for update duplicates -statement. Use the _update_column_names_ option, then only these columns -will be updated. Be default, if option _update_column_names_ not passed, -used column list from _column_names_ option. +statement. Use the _update_columns_ option, then only these columns will be +updated. Be default, if option _update_columns_ not passed, used column list +from _column_names_ option. ```ruby destination_columns = [:title, :author] update_columns = [:title] # Update duplicate rows (MySQL) -Book.bulk_insert(*destination_columns, update_duplicates: true, update_column_names: update_columns) do |worker| +Book.bulk_insert(*destination_columns, update_duplicates: true, update_columns: update_columns) do |worker| worker.add(...) worker.add(...) # ... end # Update duplicate rows (PostgreSQL) -Book.bulk_insert(*destination_columns, update_duplicates: %w[title], update_column_names: update_columns) do |worker| +Book.bulk_insert(*destination_columns, update_duplicates: %w[title], update_columns: update_columns) do |worker| worker.add(...) # ... end diff --git a/lib/bulk_insert.rb b/lib/bulk_insert.rb index 7964d6a..0d6d077 100644 --- a/lib/bulk_insert.rb +++ b/lib/bulk_insert.rb @@ -4,9 +4,9 @@ module BulkInsert extend ActiveSupport::Concern module ClassMethods - def bulk_insert(*columns, values: nil, set_size:500, ignore: false, update_duplicates: false, return_primary_keys: false, update_column_names: nil) + def bulk_insert(*columns, values: nil, set_size:500, ignore: false, update_duplicates: false, return_primary_keys: false, update_columns: nil) columns = default_bulk_columns if columns.empty? - worker = BulkInsert::Worker.new(connection, table_name, primary_key, columns, set_size, ignore, update_duplicates, return_primary_keys, update_column_names) + worker = BulkInsert::Worker.new(connection, table_name, primary_key, columns, set_size, ignore, update_duplicates, return_primary_keys, update_columns) if values.present? transaction do diff --git a/lib/bulk_insert/worker.rb b/lib/bulk_insert/worker.rb index 962d699..636029c 100644 --- a/lib/bulk_insert/worker.rb +++ b/lib/bulk_insert/worker.rb @@ -7,7 +7,7 @@ class Worker attr_accessor :adapter_name attr_reader :ignore, :update_duplicates, :result_sets - def initialize(connection, table_name, primary_key, column_names, set_size=500, ignore=false, update_duplicates=false, return_primary_keys=false, update_column_names=nil) + def initialize(connection, table_name, primary_key, column_names, set_size=500, ignore=false, update_duplicates=false, return_primary_keys=false, update_columns=nil) @connection = connection @set_size = set_size @@ -24,7 +24,7 @@ def initialize(connection, table_name, primary_key, column_names, set_size=500, @columns = column_names.map { |name| column_map[name.to_s] } @table_name = connection.quote_table_name(table_name) @column_names = column_names.map { |name| connection.quote_column_name(name) }.join(",") - @update_column_names = update_column_names.nil? ? @column_names : update_column_names + @update_columns = update_columns.is_a?(Array) ? update_columns.map { |name| column_map[name.to_s] } : @columns @before_save_callback = nil @after_save_callback = nil @@ -155,12 +155,12 @@ def on_conflict_statement if is_postgres && ignore ' ON CONFLICT DO NOTHING' elsif is_postgres && update_duplicates - update_values = @update_column_names.map do |column| + update_values = @update_columns.map do |column| "#{column.name}=EXCLUDED.#{column.name}" end.join(', ') ' ON CONFLICT(' + update_duplicates.join(', ') + ') DO UPDATE SET ' + update_values elsif adapter_name =~ /^mysql/i && update_duplicates - update_values = @update_column_names.map do |column| + update_values = @update_columns.map do |column| "`#{column.name}`=VALUES(`#{column.name}`)" end.join(', ') ' ON DUPLICATE KEY UPDATE ' + update_values diff --git a/test/bulk_insert/worker_test.rb b/test/bulk_insert/worker_test.rb index c939d03..1aa9d7c 100644 --- a/test/bulk_insert/worker_test.rb +++ b/test/bulk_insert/worker_test.rb @@ -342,7 +342,7 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase assert_equal pgsql_worker.compose_insert_query, "INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,0,NULL,NULL,'chartreuse') ON CONFLICT DO NOTHING RETURNING id" end - test "adapter dependent postgresql methods (with update_duplicates, without update_column_names)" do + test "adapter dependent postgresql methods (with update_duplicates, without update_columns)" do pgsql_worker = BulkInsert::Worker.new( Testing.connection, Testing.table_name, @@ -359,7 +359,7 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase assert_equal pgsql_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, age, happy) DO UPDATE SET greeting=EXCLUDED.greeting, age=EXCLUDED.age, happy=EXCLUDED.happy, created_at=EXCLUDED.created_at, updated_at=EXCLUDED.updated_at, color=EXCLUDED.color RETURNING id" end - test "adapter dependent postgresql methods (with update_duplicates, with update_column_names)" do + test "adapter dependent postgresql methods (with update_duplicates, with update_columns)" do pgsql_worker = BulkInsert::Worker.new( Testing.connection, Testing.table_name, @@ -437,7 +437,7 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase assert_equal mysql_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`)" end - test "mysql adapter can update duplicates (with update_column_names)" do + test "mysql adapter can update duplicates (with update_columns)" do mysql_worker = BulkInsert::Worker.new( Testing.connection, Testing.table_name, @@ -445,7 +445,8 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase %w(greeting age happy created_at updated_at color), 500, # batch size false, # ignore - true, # update_duplicates + true, # update_duplicates, + false, # return primary keys %w(greeting age happy updated_at) ) mysql_worker.adapter_name = 'MySQL'