From 7a32117d56e9d1e7e642c0816daf2fb458962552 Mon Sep 17 00:00:00 2001 From: Adam Bachman Date: Wed, 15 Nov 2023 16:52:13 -0500 Subject: [PATCH] use more permissive argument passthrough for insert_all and upsert_all --- acceptance/cases/models/insert_all_test.rb | 22 ++++++++++++++++++++++ lib/activerecord_spanner_adapter/base.rb | 6 +++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/acceptance/cases/models/insert_all_test.rb b/acceptance/cases/models/insert_all_test.rb index f13e3c38..b149f17e 100644 --- a/acceptance/cases/models/insert_all_test.rb +++ b/acceptance/cases/models/insert_all_test.rb @@ -33,6 +33,12 @@ def test_insert_all assert_raise(NotImplementedError) { Author.insert_all(values) } end + def test_insert + value = { id: Author.next_sequence_value, name: "Alice" } + + assert_raise(NotImplementedError) { Author.insert(value) } + end + def test_insert_all! values = [ { id: Author.next_sequence_value, name: "Alice" }, @@ -85,6 +91,22 @@ def test_insert_all_with_buffered_mutation_transaction assert_equal "Carol", authors[2].name end + def test_upsert + Author.create id: 1, name: "David" + authors = Author.all.order(:name) + assert_equal 1, authors.length + assert_equal "David", authors[0].name + + value = { id: 1, name: "Alice" } + + Author.upsert(value) + + authors = Author.all.order(:name) + + assert_equal 1, authors.length + assert_equal "Alice", authors[0].name + end + def test_upsert_all Author.create id: 1, name: "David" authors = Author.all.order(:name) diff --git a/lib/activerecord_spanner_adapter/base.rb b/lib/activerecord_spanner_adapter/base.rb index 0062f6ac..84ae6fac 100644 --- a/lib/activerecord_spanner_adapter/base.rb +++ b/lib/activerecord_spanner_adapter/base.rb @@ -67,11 +67,11 @@ def self._upsert_record values _buffer_record values, :insert_or_update end - def self.insert_all _attributes, _returning: nil, _unique_by: nil + def self.insert_all _attributes, **_kwargs raise NotImplementedError, "Cloud Spanner does not support skip_duplicates." end - def self.insert_all! attributes, returning: nil + def self.insert_all! attributes, **kwargs return super unless spanner_adapter? return super if active_transaction? && !buffered_mutations? @@ -90,7 +90,7 @@ def self.insert_all! attributes, returning: nil end end - def self.upsert_all attributes, returning: nil, unique_by: nil + def self.upsert_all attributes, **kwargs return super unless spanner_adapter? if active_transaction? && !buffered_mutations? raise NotImplementedError, "Cloud Spanner does not support upsert using DML. " \