Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: more permissive arg passthrough for insert_all and upsert_all #283

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions acceptance/cases/models/insert_all_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions lib/activerecord_spanner_adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
raise NotImplementedError, "Cloud Spanner does not support skip_duplicates."
def self.insert_all _attributes, **_kwargs
raise NotImplementedError, "Cloud Spanner does not support skip_duplicates. Use insert! or upsert instead."
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?

Expand All @@ -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. " \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,68 @@
require_relative "./base_spanner_mock_server_test"

module MockServerTests
CommitRequest = Google::Cloud::Spanner::V1::CommitRequest

class SpannerActiveRecordMockServerTest < BaseSpannerMockServerTest

def test_insert
singer = { first_name: "Alice", last_name: "Ecila" }

assert_raises(NotImplementedError) { Singer.insert(singer) }
end

def test_insert!
singer = { first_name: "Alice", last_name: "Ecila" }
singer = Singer.insert! singer
id = singer[0]["id"]
id = id.value if id.respond_to?(:value)

commit_requests = @mock.requests.select { |req| req.is_a?(CommitRequest) }
assert_equal 1, commit_requests.length
mutations = commit_requests[0].mutations
assert_equal 1, mutations.length
mutation = mutations[0]
assert_equal :insert, mutation.operation
assert_equal "singers", mutation.insert.table

assert_equal 1, mutation.insert.values.length
assert_equal 3, mutation.insert.values[0].length
assert_equal "Alice", mutation.insert.values[0][0]
assert_equal "Ecila", mutation.insert.values[0][1]
assert_equal id, mutation.insert.values[0][2].to_i

assert_equal 3, mutation.insert.columns.length
assert_equal "first_name", mutation.insert.columns[0]
assert_equal "last_name", mutation.insert.columns[1]
assert_equal "id", mutation.insert.columns[2]
end

def test_upsert
singer = { first_name: "Alice", last_name: "Ecila" }
singer = Singer.upsert singer
id = singer[0]["id"]
id = id.value if id.respond_to?(:value)

commit_requests = @mock.requests.select { |req| req.is_a?(CommitRequest) }
assert_equal 1, commit_requests.length
mutations = commit_requests[0].mutations
assert_equal 1, mutations.length
mutation = mutations[0]
assert_equal :insert_or_update, mutation.operation
assert_equal "singers", mutation.insert_or_update.table

assert_equal 1, mutation.insert_or_update.values.length
assert_equal 3, mutation.insert_or_update.values[0].length
assert_equal "Alice", mutation.insert_or_update.values[0][0]
assert_equal "Ecila", mutation.insert_or_update.values[0][1]
assert_equal id, mutation.insert_or_update.values[0][2].to_i

assert_equal 3, mutation.insert_or_update.columns.length
assert_equal "first_name", mutation.insert_or_update.columns[0]
assert_equal "last_name", mutation.insert_or_update.columns[1]
assert_equal "id", mutation.insert_or_update.columns[2]
end

def test_selects_all_singers_without_transaction
sql = "SELECT `singers`.* FROM `singers`"
@mock.put_statement_result sql, MockServerTests::create_random_singers_result(4)
Expand Down
Loading