From 2bf627bd7d2658f54ee9b81552488a1e0f45a0dd Mon Sep 17 00:00:00 2001 From: Nikhil Chennupati Date: Fri, 16 Oct 2026 23:08:04 -0700 Subject: [PATCH 1/3] feat(firestore): add x-goog-is-transaction routing header Add x-goog-is-transaction routing header in Ruby SDK to differentiate transactional requests and non-transactional requests from firestore. Having a routing header for transactions helps in routing grpc requests at GFE. --- .../lib/google/cloud/firestore/service.rb | 46 +++++--- .../firestore/aggregate_query/explain_test.rb | 4 +- .../firestore/aggregate_query/get_test.rb | 6 +- .../firestore/client/transaction_test.rb | 109 ++++++++++++++++-- .../list_documents_test.rb | 50 +++++--- google-cloud-firestore/test/helper.rb | 12 +- 6 files changed, 170 insertions(+), 57 deletions(-) diff --git a/google-cloud-firestore/lib/google/cloud/firestore/service.rb b/google-cloud-firestore/lib/google/cloud/firestore/service.rb index 2107258f7d9e..b94edbf5c774 100644 --- a/google-cloud-firestore/lib/google/cloud/firestore/service.rb +++ b/google-cloud-firestore/lib/google/cloud/firestore/service.rb @@ -73,7 +73,7 @@ def get_documents document_paths, mask: nil, transaction: nil, read_time: nil if read_time batch_get_req[:read_time] = read_time_to_timestamp(read_time) end - firestore.batch_get_documents batch_get_req, call_options(parent: database_path) + firestore.batch_get_documents batch_get_req, call_options(parent: database_path, is_transaction: !transaction.nil?) end ## @@ -83,15 +83,20 @@ def get_documents document_paths, mask: nil, transaction: nil, read_time: nil # the showMissing flag to true to support full document traversal. If # there are too many documents, recommendation will be not to call this # method. - def list_documents parent, collection_id, token: nil, max: nil, read_time: nil + def list_documents parent, collection_id, token: nil, max: nil, read_time: nil, transaction: nil mask = { field_paths: [] } - paged_enum = firestore.list_documents parent: parent, - collection_id: collection_id, - page_size: max, - page_token: token, - mask: mask, - show_missing: true, - read_time: read_time_to_timestamp(read_time) + list_docs_req = { + parent: parent, + collection_id: collection_id, + page_size: max, + page_token: token, + mask: mask, + show_missing: true, + read_time: read_time_to_timestamp(read_time) + } + list_docs_req[:transaction] = transaction if transaction + paged_enum = firestore.list_documents list_docs_req, + call_options(parent: database_path, is_transaction: !transaction.nil?) paged_enum.response end @@ -118,7 +123,7 @@ def partition_query parent, query_grpc, partition_count, token: nil, max: nil, r page_size: max, read_time: read_time_to_timestamp(read_time) ) - paged_enum = firestore.partition_query request + paged_enum = firestore.partition_query request, call_options(parent: database_path) paged_enum.response end @@ -142,7 +147,7 @@ def run_query path, query_grpc, transaction: nil, read_time: nil, explain_option run_query_req[:explain_options] = explain_options end - firestore.run_query run_query_req, call_options(parent: database_path) + firestore.run_query run_query_req, call_options(parent: database_path, is_transaction: !transaction.nil?) end ## @@ -160,7 +165,7 @@ def run_aggregate_query parent, structured_aggregation_query, transaction: nil, if explain_options request.explain_options = explain_options end - firestore.run_aggregation_query request + firestore.run_aggregation_query request, call_options(parent: database_path, is_transaction: !transaction.nil?) end def listen enum @@ -173,7 +178,7 @@ def begin_transaction transaction_opt database: database_path, options: transaction_opt }, - call_options(parent: database_path) + call_options(parent: database_path, is_transaction: true) ) end @@ -184,7 +189,7 @@ def commit writes, transaction: nil } commit_req[:transaction] = transaction if transaction - firestore.commit commit_req, call_options(parent: database_path) + firestore.commit commit_req, call_options(parent: database_path, is_transaction: !transaction.nil?) end def rollback transaction @@ -193,7 +198,7 @@ def rollback transaction database: database_path, transaction: transaction }, - call_options(parent: database_path) + call_options(parent: database_path, is_transaction: true) ) end @@ -236,15 +241,18 @@ def read_time_to_timestamp read_time protected - def default_headers parent = nil + def default_headers parent = nil, is_transaction: false parent ||= database_path routing_val = CGI.escapeURIComponent parent - { "x-goog-request-params" => "database=#{routing_val}" } + { + "x-goog-request-params" => "database=#{routing_val}", + "x-goog-is-transaction" => (is_transaction ? "true" : "false") + } end - def call_options parent: nil, token: nil + def call_options parent: nil, token: nil, is_transaction: false Gapic::CallOptions.new(**{ - metadata: default_headers(parent), + metadata: default_headers(parent, is_transaction: is_transaction), page_token: token }.compact) end diff --git a/google-cloud-firestore/test/google/cloud/firestore/aggregate_query/explain_test.rb b/google-cloud-firestore/test/google/cloud/firestore/aggregate_query/explain_test.rb index 66d8a7e44d44..d36bdf5c3168 100644 --- a/google-cloud-firestore/test/google/cloud/firestore/aggregate_query/explain_test.rb +++ b/google-cloud-firestore/test/google/cloud/firestore/aggregate_query/explain_test.rb @@ -66,7 +66,7 @@ explain_options: expected_explain_options ) - firestore_mock.expect :run_aggregation_query, response_with_planning_metrics_only, [request] + firestore_mock.expect :run_aggregation_query, response_with_planning_metrics_only, [request, default_options] explain_result = aggregate_query.explain _(explain_result).must_be_kind_of Google::Cloud::Firestore::AggregateQueryExplainResult @@ -82,7 +82,7 @@ explain_options: expected_explain_options ) - firestore_mock.expect :run_aggregation_query, response_with_metrics_and_result, [request] + firestore_mock.expect :run_aggregation_query, response_with_metrics_and_result, [request, default_options] explain_result = aggregate_query.explain analyze: true _(explain_result).must_be_kind_of Google::Cloud::Firestore::AggregateQueryExplainResult diff --git a/google-cloud-firestore/test/google/cloud/firestore/aggregate_query/get_test.rb b/google-cloud-firestore/test/google/cloud/firestore/aggregate_query/get_test.rb index 8322ccb3c53d..162160876c6b 100644 --- a/google-cloud-firestore/test/google/cloud/firestore/aggregate_query/get_test.rb +++ b/google-cloud-firestore/test/google/cloud/firestore/aggregate_query/get_test.rb @@ -157,7 +157,7 @@ ) ) ].to_enum - firestore_mock.expect :run_aggregation_query, mocked_response, [expected_params] + firestore_mock.expect :run_aggregation_query, mocked_response, [expected_params, default_options] aggregate_query = query.aggregate_query.add_count aggregate_alias: "total_score" # custom alias results_enum = aggregate_query.get @@ -295,7 +295,7 @@ ) ) ].to_enum - firestore_mock.expect :run_aggregation_query, mocked_response, [expected_params] + firestore_mock.expect :run_aggregation_query, mocked_response, [expected_params, default_options] aggregate_query = query.aggregate_query.add_sum 'score', aggregate_alias: "total_score" # custom alias results_enum = aggregate_query.get @@ -433,7 +433,7 @@ ) ) ].to_enum - firestore_mock.expect :run_aggregation_query, mocked_response, [expected_params] + firestore_mock.expect :run_aggregation_query, mocked_response, [expected_params, default_options] aggregate_query = query.aggregate_query.add_avg 'score', aggregate_alias: "total_score" # custom alias results_enum = aggregate_query.get diff --git a/google-cloud-firestore/test/google/cloud/firestore/client/transaction_test.rb b/google-cloud-firestore/test/google/cloud/firestore/client/transaction_test.rb index d955f26a15f1..5ca38a42732d 100644 --- a/google-cloud-firestore/test/google/cloud/firestore/client/transaction_test.rb +++ b/google-cloud-firestore/test/google/cloud/firestore/client/transaction_test.rb @@ -194,6 +194,91 @@ end end + it "runs an aggregate query" do + expected_query = Google::Cloud::Firestore::V1::RunAggregationQueryRequest.new( + parent: documents_path, + structured_aggregation_query: Google::Cloud::Firestore::V1::StructuredAggregationQuery.new( + structured_query: Google::Cloud::Firestore::V1::StructuredQuery.new( + from: [Google::Cloud::Firestore::V1::StructuredQuery::CollectionSelector.new(collection_id: "users", all_descendants: false)] + ), + aggregations: [ + Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation.new( + count: Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation::Count.new, + alias: "count" + ) + ] + ), + new_transaction: transaction_opt + ) + agg_response = [ + Google::Cloud::Firestore::V1::RunAggregationQueryResponse.new( + transaction: transaction_id, + result: Google::Cloud::Firestore::V1::AggregationResult.new( + aggregate_fields: { + "count": Google::Cloud::Firestore::V1::Value.new(integer_value: 2) + } + ) + ) + ].to_enum + firestore_mock.expect :run_aggregation_query, agg_response, [expected_query, transaction_options] + firestore_mock.expect :commit, empty_commit_resp, commit_args(transaction: transaction_id) + + firestore.transaction do |tx| + aq = firestore.col(:users).aggregate_query.add_count + results = tx.get_aggregate(aq).to_a + _(results.count).must_equal 1 + _(results.first.get("count")).must_equal 2 + end + end + + it "runs multiple aggregate queries" do + structured_agg_query = Google::Cloud::Firestore::V1::StructuredAggregationQuery.new( + structured_query: Google::Cloud::Firestore::V1::StructuredQuery.new( + from: [Google::Cloud::Firestore::V1::StructuredQuery::CollectionSelector.new(collection_id: "users", all_descendants: false)] + ), + aggregations: [ + Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation.new( + count: Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation::Count.new, + alias: "count" + ) + ] + ) + first_query = Google::Cloud::Firestore::V1::RunAggregationQueryRequest.new( + parent: documents_path, + structured_aggregation_query: structured_agg_query, + new_transaction: transaction_opt + ) + second_query = Google::Cloud::Firestore::V1::RunAggregationQueryRequest.new( + parent: documents_path, + structured_aggregation_query: structured_agg_query, + transaction: transaction_id + ) + agg_response = [ + Google::Cloud::Firestore::V1::RunAggregationQueryResponse.new( + transaction: transaction_id, + result: Google::Cloud::Firestore::V1::AggregationResult.new( + aggregate_fields: { + "count": Google::Cloud::Firestore::V1::Value.new(integer_value: 2) + } + ) + ) + ].to_enum + firestore_mock.expect :run_aggregation_query, agg_response, [first_query, transaction_options] + firestore_mock.expect :run_aggregation_query, agg_response, [second_query, transaction_options] + firestore_mock.expect :commit, empty_commit_resp, commit_args(transaction: transaction_id) + + firestore.transaction do |tx| + aq = firestore.col(:users).aggregate_query.add_count + results = tx.get_aggregate(aq).to_a + _(results.count).must_equal 1 + _(results.first.get("count")).must_equal 2 + + results = tx.get_aggregate(aq).to_a + _(results.count).must_equal 1 + _(results.first.get("count")).must_equal 2 + end + end + it "that does nothing makes no calls" do firestore.transaction do |tx| # no op @@ -201,7 +286,7 @@ end it "creates a new document using string path" do - firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options] + firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options] firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: create_writes) resp = firestore.transaction commit_response: true do |tx| @@ -213,7 +298,7 @@ end it "creates a new document using doc ref" do - firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options] + firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options] firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: create_writes) doc = firestore.doc document_path @@ -235,7 +320,7 @@ end it "sets a new document using string path" do - firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options] + firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options] firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: set_writes) resp = firestore.transaction commit_response: true do |tx| @@ -247,7 +332,7 @@ end it "sets a new document using doc ref" do - firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options] + firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options] firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: set_writes) doc = firestore.doc document_path @@ -269,7 +354,7 @@ end it "updates a new document using string path" do - firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options] + firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options] firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: update_writes) resp = firestore.transaction commit_response: true do |tx| @@ -281,7 +366,7 @@ end it "updates a new document using doc ref" do - firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options] + firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options] firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: update_writes) doc = firestore.doc document_path @@ -303,7 +388,7 @@ end it "deletes a document using string path" do - firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options] + firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options] firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: delete_writes) resp = firestore.transaction commit_response: true do |tx| @@ -315,7 +400,7 @@ end it "deletes a document using doc ref" do - firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options] + firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options] firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: delete_writes) doc = firestore.doc document_path @@ -355,7 +440,7 @@ it "performs multiple writes in the same commit (string)" do all_writes = create_writes + set_writes + update_writes + delete_writes - firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options] + firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options] firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: all_writes) resp = firestore.transaction commit_response: true do |tx| @@ -371,7 +456,7 @@ it "performs multiple writes in the same commit (doc ref)" do all_writes = create_writes + set_writes + update_writes + delete_writes - firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options] + firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options] firestore_mock.expect :commit, write_commit_resp, commit_args(transaction: transaction_id, writes: all_writes) doc_ref = firestore.doc document_path @@ -500,8 +585,8 @@ def firestore.sleep num end it "does not retry when an unsupported error is raised" do - firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, default_options] - firestore_mock.expect :rollback, nil, [{ database: database_path, transaction: transaction_id }, default_options] + firestore_mock.expect :begin_transaction, begin_tx_resp, [{ database: database_path, options: transaction_opt }, transaction_options] + firestore_mock.expect :rollback, nil, [{ database: database_path, transaction: transaction_id }, transaction_options] # Unable to use mocks to raise an error, so stub the method instead def firestore_mock.commit req, options diff --git a/google-cloud-firestore/test/google/cloud/firestore/collection_reference/list_documents_test.rb b/google-cloud-firestore/test/google/cloud/firestore/collection_reference/list_documents_test.rb index 3e7db3bc6225..e0f84321c527 100644 --- a/google-cloud-firestore/test/google/cloud/firestore/collection_reference/list_documents_test.rb +++ b/google-cloud-firestore/test/google/cloud/firestore/collection_reference/list_documents_test.rb @@ -25,7 +25,7 @@ list_res = paged_enum_struct list_documents_gapi(num_documents) - firestore_mock.expect :list_documents, list_res, **list_documents_args + firestore_mock.expect :list_documents, list_res, list_documents_args documents = collection.list_documents @@ -39,8 +39,8 @@ first_list_res = paged_enum_struct list_documents_gapi(3, "next_page_token") second_list_res = paged_enum_struct list_documents_gapi(2) - firestore_mock.expect :list_documents, first_list_res, **list_documents_args - firestore_mock.expect :list_documents, second_list_res, **list_documents_args(page_token: "next_page_token") + firestore_mock.expect :list_documents, first_list_res, list_documents_args + firestore_mock.expect :list_documents, second_list_res, list_documents_args(page_token: "next_page_token") first_documents = collection.list_documents second_documents = collection.list_documents token: first_documents.token @@ -61,8 +61,8 @@ first_list_res = paged_enum_struct list_documents_gapi(3, "next_page_token") second_list_res = paged_enum_struct list_documents_gapi(2) - firestore_mock.expect :list_documents, first_list_res, **list_documents_args - firestore_mock.expect :list_documents, second_list_res, **list_documents_args(page_token: "next_page_token") + firestore_mock.expect :list_documents, first_list_res, list_documents_args + firestore_mock.expect :list_documents, second_list_res, list_documents_args(page_token: "next_page_token") first_documents = collection.list_documents second_documents = first_documents.next @@ -82,8 +82,8 @@ first_list_res = paged_enum_struct list_documents_gapi(3, "next_page_token") second_list_res = paged_enum_struct list_documents_gapi(2) - firestore_mock.expect :list_documents, first_list_res, **list_documents_args(read_time: read_time) - firestore_mock.expect :list_documents, second_list_res, **list_documents_args(page_token: "next_page_token", read_time: read_time) + firestore_mock.expect :list_documents, first_list_res, list_documents_args(read_time: read_time) + firestore_mock.expect :list_documents, second_list_res, list_documents_args(page_token: "next_page_token", read_time: read_time) first_documents = collection.list_documents read_time: read_time second_documents = first_documents.next @@ -103,8 +103,8 @@ first_list_res = paged_enum_struct list_documents_gapi(3, "next_page_token") second_list_res = paged_enum_struct list_documents_gapi(2) - firestore_mock.expect :list_documents, first_list_res, **list_documents_args - firestore_mock.expect :list_documents, second_list_res, **list_documents_args(page_token: "next_page_token") + firestore_mock.expect :list_documents, first_list_res, list_documents_args + firestore_mock.expect :list_documents, second_list_res, list_documents_args(page_token: "next_page_token") all_documents = collection.list_documents.all.to_a @@ -118,8 +118,8 @@ first_list_res = paged_enum_struct list_documents_gapi(3, "next_page_token") second_list_res = paged_enum_struct list_documents_gapi(3, "second_page_token") - firestore_mock.expect :list_documents, first_list_res, **list_documents_args - firestore_mock.expect :list_documents, second_list_res, **list_documents_args(page_token: "next_page_token") + firestore_mock.expect :list_documents, first_list_res, list_documents_args + firestore_mock.expect :list_documents, second_list_res, list_documents_args(page_token: "next_page_token") all_documents = collection.list_documents.all.take(5) @@ -133,8 +133,8 @@ first_list_res = paged_enum_struct list_documents_gapi(3, "next_page_token") second_list_res = paged_enum_struct list_documents_gapi(3, "second_page_token") - firestore_mock.expect :list_documents, first_list_res, **list_documents_args - firestore_mock.expect :list_documents, second_list_res, **list_documents_args(page_token: "next_page_token") + firestore_mock.expect :list_documents, first_list_res, list_documents_args + firestore_mock.expect :list_documents, second_list_res, list_documents_args(page_token: "next_page_token") all_documents = collection.list_documents.all(request_limit: 1).to_a @@ -147,7 +147,7 @@ it "paginates documents with max set" do list_res = paged_enum_struct list_documents_gapi(3, "next_page_token") - firestore_mock.expect :list_documents, list_res, **list_documents_args(page_size: 3) + firestore_mock.expect :list_documents, list_res, list_documents_args(page_size: 3) documents = collection.list_documents max: 3 @@ -159,6 +159,22 @@ _(documents.token).must_equal "next_page_token" end + it "lists documents with transaction" do + num_documents = 3 + + list_res = paged_enum_struct list_documents_gapi(num_documents) + + firestore_mock.expect :list_documents, list_res, list_documents_args(transaction: transaction_id) + + response = firestore.service.list_documents "projects/#{project}/databases/(default)/documents/users/alice", + collection_id, + transaction: transaction_id + + firestore_mock.verify + + _(response.documents.size).must_equal num_documents + end + def document_gapi Google::Cloud::Firestore::V1::Document.new( name: "projects/#{project}/databases/(default)/documents/my-document", @@ -175,8 +191,8 @@ def list_documents_gapi count = 2, token = nil ) end - def list_documents_args page_size: nil, page_token: nil, read_time: nil - { + def list_documents_args page_size: nil, page_token: nil, read_time: nil, transaction: nil + req = { parent: "projects/projectID/databases/(default)/documents/users/alice", collection_id: "messages", page_size: page_size, @@ -185,5 +201,7 @@ def list_documents_args page_size: nil, page_token: nil, read_time: nil show_missing: true, read_time: firestore.service.read_time_to_timestamp(read_time) } + req[:transaction] = transaction if transaction + [req, transaction ? transaction_options : default_options] end end diff --git a/google-cloud-firestore/test/helper.rb b/google-cloud-firestore/test/helper.rb index ffc0942588af..bf96eef02773 100644 --- a/google-cloud-firestore/test/helper.rb +++ b/google-cloud-firestore/test/helper.rb @@ -176,7 +176,8 @@ class MockFirestore < Minitest::Spec let(:full_doc_paths) { ["#{documents_path}/users/alice", "#{documents_path}/users/bob", "#{documents_path}/users/carol"] } - let(:default_options) { Gapic::CallOptions.new(metadata: { "x-goog-request-params" => "database=#{CGI.escapeURIComponent(database_path)}"}, retry_policy: {}) } + let(:default_options) { Gapic::CallOptions.new(metadata: { "x-goog-request-params" => "database=#{CGI.escapeURIComponent(database_path)}", "x-goog-is-transaction" => "false" }, retry_policy: {}) } + let(:transaction_options) { Gapic::CallOptions.new(metadata: { "x-goog-request-params" => "database=#{CGI.escapeURIComponent(database_path)}", "x-goog-is-transaction" => "true" }, retry_policy: {}) } let(:credentials) { OpenStruct.new(client: OpenStruct.new(updater_proc: Proc.new {})) } let(:firestore) { Google::Cloud::Firestore::Client.new(Google::Cloud::Firestore::Service.new(project, credentials, database: database)) } let(:secondary_firestore) { Google::Cloud::Firestore::Client.new(Google::Cloud::Firestore::Service.new(project, credentials, database: secondary_database)) } @@ -219,7 +220,7 @@ def batch_get_documents_args database: database_path, req[:transaction] = transaction if transaction req[:new_transaction] = new_transaction if new_transaction req[:read_time] = read_time if read_time - [req, default_options] + [req, transaction || new_transaction ? transaction_options : default_options] end def commit_args database: database_path, @@ -230,7 +231,7 @@ def commit_args database: database_path, writes: writes } req[:transaction] = transaction if transaction - [req, default_options] + [req, transaction ? transaction_options : default_options] end def list_collection_ids_args parent: "projects/#{project}/databases/(default)/documents", @@ -258,7 +259,7 @@ def run_query_args query, req[:new_transaction] = new_transaction if new_transaction req[:read_time] = firestore.service.read_time_to_timestamp(read_time) if read_time req[:explain_options] = explain_options if explain_options - [req, default_options] + [req, transaction || new_transaction ? transaction_options : default_options] end def partition_query_args query_grpc, @@ -275,7 +276,8 @@ def partition_query_args query_grpc, page_token: page_token, page_size: page_size, read_time: read_time - ) + ), + default_options ] end From ecbdedb7df92299cc71506d7a1569070ca7198f2 Mon Sep 17 00:00:00 2001 From: Nikhil Chennupati Date: Wed, 21 Oct 2026 10:06:05 -0700 Subject: [PATCH 2/3] style(firestore): fix rubocop line length and remove unused list_documents transaction param --- .../lib/google/cloud/firestore/service.rb | 16 +++++--- .../list_documents_test.rb | 41 ++++++------------- 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/google-cloud-firestore/lib/google/cloud/firestore/service.rb b/google-cloud-firestore/lib/google/cloud/firestore/service.rb index b94edbf5c774..604597efdf76 100644 --- a/google-cloud-firestore/lib/google/cloud/firestore/service.rb +++ b/google-cloud-firestore/lib/google/cloud/firestore/service.rb @@ -73,7 +73,10 @@ def get_documents document_paths, mask: nil, transaction: nil, read_time: nil if read_time batch_get_req[:read_time] = read_time_to_timestamp(read_time) end - firestore.batch_get_documents batch_get_req, call_options(parent: database_path, is_transaction: !transaction.nil?) + firestore.batch_get_documents( + batch_get_req, + call_options(parent: database_path, is_transaction: !transaction.nil?) + ) end ## @@ -83,7 +86,7 @@ def get_documents document_paths, mask: nil, transaction: nil, read_time: nil # the showMissing flag to true to support full document traversal. If # there are too many documents, recommendation will be not to call this # method. - def list_documents parent, collection_id, token: nil, max: nil, read_time: nil, transaction: nil + def list_documents parent, collection_id, token: nil, max: nil, read_time: nil mask = { field_paths: [] } list_docs_req = { parent: parent, @@ -94,9 +97,7 @@ def list_documents parent, collection_id, token: nil, max: nil, read_time: nil, show_missing: true, read_time: read_time_to_timestamp(read_time) } - list_docs_req[:transaction] = transaction if transaction - paged_enum = firestore.list_documents list_docs_req, - call_options(parent: database_path, is_transaction: !transaction.nil?) + paged_enum = firestore.list_documents list_docs_req, call_options(parent: database_path) paged_enum.response end @@ -165,7 +166,10 @@ def run_aggregate_query parent, structured_aggregation_query, transaction: nil, if explain_options request.explain_options = explain_options end - firestore.run_aggregation_query request, call_options(parent: database_path, is_transaction: !transaction.nil?) + firestore.run_aggregation_query( + request, + call_options(parent: database_path, is_transaction: !transaction.nil?) + ) end def listen enum diff --git a/google-cloud-firestore/test/google/cloud/firestore/collection_reference/list_documents_test.rb b/google-cloud-firestore/test/google/cloud/firestore/collection_reference/list_documents_test.rb index e0f84321c527..bbd10c078970 100644 --- a/google-cloud-firestore/test/google/cloud/firestore/collection_reference/list_documents_test.rb +++ b/google-cloud-firestore/test/google/cloud/firestore/collection_reference/list_documents_test.rb @@ -159,22 +159,6 @@ _(documents.token).must_equal "next_page_token" end - it "lists documents with transaction" do - num_documents = 3 - - list_res = paged_enum_struct list_documents_gapi(num_documents) - - firestore_mock.expect :list_documents, list_res, list_documents_args(transaction: transaction_id) - - response = firestore.service.list_documents "projects/#{project}/databases/(default)/documents/users/alice", - collection_id, - transaction: transaction_id - - firestore_mock.verify - - _(response.documents.size).must_equal num_documents - end - def document_gapi Google::Cloud::Firestore::V1::Document.new( name: "projects/#{project}/databases/(default)/documents/my-document", @@ -191,17 +175,18 @@ def list_documents_gapi count = 2, token = nil ) end - def list_documents_args page_size: nil, page_token: nil, read_time: nil, transaction: nil - req = { - parent: "projects/projectID/databases/(default)/documents/users/alice", - collection_id: "messages", - page_size: page_size, - page_token: page_token, - mask: {field_paths: []}, - show_missing: true, - read_time: firestore.service.read_time_to_timestamp(read_time) - } - req[:transaction] = transaction if transaction - [req, transaction ? transaction_options : default_options] + def list_documents_args page_size: nil, page_token: nil, read_time: nil + [ + { + parent: "projects/projectID/databases/(default)/documents/users/alice", + collection_id: "messages", + page_size: page_size, + page_token: page_token, + mask: {field_paths: []}, + show_missing: true, + read_time: firestore.service.read_time_to_timestamp(read_time) + }, + default_options + ] end end From 623ee27b730157449076661b653defea486a5668 Mon Sep 17 00:00:00 2001 From: Nikhil Chennupati Date: Wed, 21 Oct 2026 14:07:03 -0700 Subject: [PATCH 3/3] refactor(firestore): pass transaction directly to call_options and default_headers --- .../lib/google/cloud/firestore/service.rb | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/google-cloud-firestore/lib/google/cloud/firestore/service.rb b/google-cloud-firestore/lib/google/cloud/firestore/service.rb index 604597efdf76..ac07f7d110ff 100644 --- a/google-cloud-firestore/lib/google/cloud/firestore/service.rb +++ b/google-cloud-firestore/lib/google/cloud/firestore/service.rb @@ -73,10 +73,7 @@ def get_documents document_paths, mask: nil, transaction: nil, read_time: nil if read_time batch_get_req[:read_time] = read_time_to_timestamp(read_time) end - firestore.batch_get_documents( - batch_get_req, - call_options(parent: database_path, is_transaction: !transaction.nil?) - ) + firestore.batch_get_documents batch_get_req, call_options(parent: database_path, transaction: transaction) end ## @@ -148,7 +145,7 @@ def run_query path, query_grpc, transaction: nil, read_time: nil, explain_option run_query_req[:explain_options] = explain_options end - firestore.run_query run_query_req, call_options(parent: database_path, is_transaction: !transaction.nil?) + firestore.run_query run_query_req, call_options(parent: database_path, transaction: transaction) end ## @@ -166,10 +163,7 @@ def run_aggregate_query parent, structured_aggregation_query, transaction: nil, if explain_options request.explain_options = explain_options end - firestore.run_aggregation_query( - request, - call_options(parent: database_path, is_transaction: !transaction.nil?) - ) + firestore.run_aggregation_query request, call_options(parent: database_path, transaction: transaction) end def listen enum @@ -182,7 +176,7 @@ def begin_transaction transaction_opt database: database_path, options: transaction_opt }, - call_options(parent: database_path, is_transaction: true) + call_options(parent: database_path, transaction: transaction_opt) ) end @@ -193,7 +187,7 @@ def commit writes, transaction: nil } commit_req[:transaction] = transaction if transaction - firestore.commit commit_req, call_options(parent: database_path, is_transaction: !transaction.nil?) + firestore.commit commit_req, call_options(parent: database_path, transaction: transaction) end def rollback transaction @@ -202,7 +196,7 @@ def rollback transaction database: database_path, transaction: transaction }, - call_options(parent: database_path, is_transaction: true) + call_options(parent: database_path, transaction: transaction) ) end @@ -245,18 +239,18 @@ def read_time_to_timestamp read_time protected - def default_headers parent = nil, is_transaction: false + def default_headers parent = nil, transaction: nil parent ||= database_path routing_val = CGI.escapeURIComponent parent { "x-goog-request-params" => "database=#{routing_val}", - "x-goog-is-transaction" => (is_transaction ? "true" : "false") + "x-goog-is-transaction" => (!transaction.nil?).to_s } end - def call_options parent: nil, token: nil, is_transaction: false + def call_options parent: nil, token: nil, transaction: nil Gapic::CallOptions.new(**{ - metadata: default_headers(parent, is_transaction: is_transaction), + metadata: default_headers(parent, transaction: transaction), page_token: token }.compact) end