-
Notifications
You must be signed in to change notification settings - Fork 35
feat: Batch DML support #370
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
Conversation
ceb0f65 to
72fd1ad
Compare
| # Return 1 to satisfy the ActiveRecord::Persistence contract for instance methods like .save | ||
| return 1 |
There was a problem hiding this comment.
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:
- 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.
- 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.
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove
| # Return 1 to satisfy the ActiveRecord::Persistence contract for instance methods like .save |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
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.