Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements a robust mechanism to prevent write operations (including DML, DDL, mutations, truncate, and batch operations) when the connection is in read-only mode or configured as a replica. It refactors SQL statement type detection to support statement hints and hash comments, ensuring read-only queries (including Spanner-specific syntax like GRAPH and FROM) are permitted while unauthorized writes raise an ActiveRecord::ReadOnlyError. Extensive acceptance and mock server tests have been added to verify this behavior. There are no review comments to address, and the implementation looks solid.
olavloite
force-pushed
the
enforce-read-only
branch
from
November 20, 2026 17:32
1ab1c1e to
40fadb8
Compare
Ensure Rails read-only mode (such as `while_preventing_writes` and
read-only replica connections) reliably raises `ActiveRecord::ReadOnlyError`
before any database modification or write transaction is attempted.
Previously, several write paths could bypass read-only checks or fail
with `NoMethodError` instead of `ActiveRecord::ReadOnlyError`:
- Buffered mutations (model creates, updates, destroys, upserts, and
unscoped `delete_all`) bypassed query-level write checks.
- Table truncation, direct DDL execution, schema migrations, DML/DDL
batches, and Partitioned DML transactions bypassed read-only guards.
- SQL statements prefixed with Spanner statement hints (`@{...}`) or
`#` comments were misclassified as read queries, allowing hinted DML
and DDL to execute in read-only mode.
- The fallback read-only error path attempted to invoke
`ActiveRecord::ReadOnlyError(...)` as a method instead of raising the
exception class.
Statement classification now skips Spanner statement hints and hash
comments, treats only read and transaction-control statements as
non-writes, and checks all non-SQL write entry points consistently while
leaving validation failures and read-only queries unaffected.
olavloite
force-pushed
the
enforce-read-only
branch
from
November 20, 2026 18:05
40fadb8 to
90532cd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ensure Rails read-only mode (such as
while_preventing_writesand read-only replica connections) reliably raisesActiveRecord::ReadOnlyErrorbefore any database modification or write transaction is attempted.Previously, several write paths could bypass read-only checks or fail with
NoMethodErrorinstead ofActiveRecord::ReadOnlyError:delete_all) bypassed query-level write checks.@{...}) or#comments were misclassified as read queries, allowing hinted DML and DDL to execute in read-only mode.ActiveRecord::ReadOnlyError(...)as a method instead of raising the exception class.Statement classification now skips Spanner statement hints and hash comments, treats only read and transaction-control statements as non-writes, and checks all non-SQL write entry points consistently while leaving validation failures and read-only queries unaffected.