Skip to content

Conversation

@aakashanandg
Copy link
Collaborator

@aakashanandg aakashanandg commented Jul 1, 2025

This change adds robust support for ActiveRecord::Base.dml_batch.

Add support for Batch DML in read/write transactions. Batch DML reduces the number of round-trips to Spanner when executing multiple writes. The big advantage of using Batch DML over mutations, is that Batch DML does support read-your-writes after the batch has been written.

@aakashanandg aakashanandg requested review from a team and olavloite as code owners July 1, 2025 13:08
@product-auto-label product-auto-label bot added the api: spanner Issues related to the googleapis/ruby-spanner-activerecord API. label Jul 1, 2025
Comment on lines 215 to 216
# Return 1 to satisfy the ActiveRecord::Persistence contract for instance methods like .save
return 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should always return 1 here. I understand the idea of trying to satisfy the contract, but the problem is that the value that is returned is not necessarily equal to the actual update count. So if the statement for example is:

update foo set bar='new_value' where id=@id and version=@version

Then that might eventually return 0 when the batch is executed. If we return 1 here, then that breaks the contract even more.

So we might need to do the following:

  1. Just return nothing for now, and accept that it breaks the contract (and thereby also fails if it is used with versioned models). If the latter indeed always fails, then we should list it as a limitation.
  2. In a follow-up pull request implement the same type of verification that we have in the JDBC driver for automatic DML batching, where we check afterwards that the update count that we return here was correct.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this change was made to satisfy the contract. Thanks for pointing this out, in that case i will just return nothing for now.

olavloite
olavloite previously approved these changes Jul 1, 2025
if @connection.dml_batch?
# This call buffers the SQL.
execute sql, name, binds
# Return 1 to satisfy the ActiveRecord::Persistence contract for instance methods like .save
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove

Suggested change
# Return 1 to satisfy the ActiveRecord::Persistence contract for instance methods like .save

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

@aakashanandg aakashanandg merged commit a7ca729 into googleapis:main Jul 3, 2025
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the googleapis/ruby-spanner-activerecord API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants