Repository navigation
fix: enforce read-only mode across all write paths #464
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
Open
olavloite
wants to merge
1
commit into
main
Choose a base branch
from
enforce-read-only
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+681
−39
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Use of this source code is governed by an MIT-style | ||
| # license that can be found in the LICENSE file or at | ||
| # https://opensource.org/licenses/MIT. | ||
| # | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
| require "models/author" | ||
|
|
||
| module ActiveRecord | ||
| module Model | ||
| class PreventingWritesTest < SpannerAdapter::TestCase | ||
| include SpannerAdapter::Associations::TestHelper | ||
|
|
||
| def setup | ||
| super | ||
| @author = Author.create! name: "Original" | ||
| end | ||
|
|
||
| def teardown | ||
| super | ||
| Author.destroy_all | ||
| end | ||
|
|
||
| def test_reads_succeed_while_preventing_writes | ||
| ActiveRecord::Base.while_preventing_writes do | ||
| found = Author.find @author.id | ||
| assert_equal "Original", found.name | ||
|
|
||
| hinted = Author.optimizer_hints("statement_hint: @{USE_ADDITIONAL_PARALLELISM=true}").find @author.id | ||
| assert_equal "Original", hinted.name | ||
|
|
||
| ActiveRecord::Base.transaction isolation: :read_only do | ||
| assert_equal "Original", Author.find(@author.id).name | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def test_mutations_and_dml_and_ddl_raise_read_only_error_without_modifying_data | ||
| ActiveRecord::Base.while_preventing_writes do | ||
| assert_raises ActiveRecord::ReadOnlyError do | ||
| Author.create! name: "Created" | ||
| end | ||
|
|
||
| assert_raises ActiveRecord::ReadOnlyError do | ||
| @author.update! name: "Updated" | ||
| end | ||
|
|
||
| assert_raises ActiveRecord::ReadOnlyError do | ||
| @author.destroy! | ||
| end | ||
|
|
||
| assert_raises ActiveRecord::ReadOnlyError do | ||
| Author.delete_all | ||
| end | ||
|
|
||
| assert_raises ActiveRecord::ReadOnlyError do | ||
| Author.where(id: @author.id).update_all name: "DmlUpdated" | ||
| end | ||
|
|
||
| assert_raises ActiveRecord::ReadOnlyError do | ||
| Author | ||
| .optimizer_hints("statement_hint: @{USE_ADDITIONAL_PARALLELISM=true}") | ||
| .where(id: @author.id) | ||
| .update_all name: "HintedUpdated" | ||
| end | ||
|
|
||
| assert_raises ActiveRecord::ReadOnlyError do | ||
| ActiveRecord::Base.connection.truncate "authors" | ||
| end | ||
|
|
||
| assert_raises ActiveRecord::ReadOnlyError do | ||
| ActiveRecord::Base.connection.execute_ddl "CREATE TABLE `blocked_table` (`id` INT64) PRIMARY KEY (`id`)" | ||
| end | ||
|
|
||
| assert_raises ActiveRecord::ReadOnlyError do | ||
| ActiveRecord::Base.transaction isolation: :pdml do | ||
| Author.where(id: @author.id).update_all name: "PdmlUpdated" | ||
| end | ||
| end | ||
| end | ||
|
|
||
| assert_equal [@author.id], Author.where(id: @author.id).pluck(:id) | ||
| assert_equal "Original", Author.find(@author.id).name | ||
| refute ActiveRecord::Base.connection.table_exists?("blocked_table") | ||
| end | ||
| end | ||
| end | ||
| end |
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.