Skip to content

fix: enforce read-only mode across all write paths - #464

Open
olavloite wants to merge 1 commit into
mainfrom
enforce-read-only
Open

olavloite wants to merge 1 commit into
mainfrom
enforce-read-only

Conversation

@olavloite

Copy link
Copy Markdown
Collaborator

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
olavloite requested review from a team and sakthivelmanii as code owners November 20, 2026 17:19
@product-auto-label product-auto-label Bot added the api: spanner Issues related to the googleapis/ruby-spanner-activerecord API. label Nov 20, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

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.
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