diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-12-11 06:40:38 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-12-11 06:40:38 +0900 |
commit | 07f0f1a8c7bcb3f789bb48bba7f4a485bfa317e9 (patch) | |
tree | f67c2408eba9d62896b8b8469746155002ed438a | |
parent | cf71f31e2e152eceeb311aea6eadba3f2a814d81 (diff) | |
download | rails-07f0f1a8c7bcb3f789bb48bba7f4a485bfa317e9.tar.gz rails-07f0f1a8c7bcb3f789bb48bba7f4a485bfa317e9.tar.bz2 rails-07f0f1a8c7bcb3f789bb48bba7f4a485bfa317e9.zip |
Don't treat begin and rollback transactions as write queries
Otherwise `save` method would raise the `ReadOnlyError` against `BEGIN`
and `ROLLBACK` queries.
5 files changed, 17 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb b/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb index edd671964f..15e07229e8 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb @@ -19,7 +19,7 @@ module ActiveRecord execute(sql, name).to_a end - READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(:begin, :select, :set, :show, :release, :savepoint) # :nodoc: + READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(:begin, :select, :set, :show, :release, :savepoint, :rollback) # :nodoc: private_constant :READ_QUERY def write_query?(sql) # :nodoc: diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb index d93c1f449e..0fd14fac0e 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb @@ -67,7 +67,7 @@ module ActiveRecord end end - READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(:select, :show, :set) # :nodoc: + READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(:begin, :select, :set, :show, :savepoint, :rollback) # :nodoc: private_constant :READ_QUERY def write_query?(sql) # :nodoc: diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 0b77dc7bc9..0871cc5fb1 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -209,7 +209,7 @@ module ActiveRecord # DATABASE STATEMENTS ====================================== #++ - READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(:select, :pragma) # :nodoc: + READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(:begin, :select, :pragma, :savepoint, :rollback) # :nodoc: private_constant :READ_QUERY def write_query?(sql) # :nodoc: diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index b74bd93f37..11df74ea25 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1490,31 +1490,37 @@ class BasicsTest < ActiveRecord::TestCase end test "creating a record raises if preventing writes" do - assert_raises ActiveRecord::ReadOnlyError do + error = assert_raises ActiveRecord::ReadOnlyError do ActiveRecord::Base.connection.while_preventing_writes do Bird.create! name: "Bluejay" end end + + assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, error.message end test "updating a record raises if preventing writes" do bird = Bird.create! name: "Bluejay" - assert_raises ActiveRecord::ReadOnlyError do + error = assert_raises ActiveRecord::ReadOnlyError do ActiveRecord::Base.connection.while_preventing_writes do bird.update! name: "Robin" end end + + assert_match %r/\AWrite query attempted while in readonly mode: UPDATE /, error.message end test "deleting a record raises if preventing writes" do bird = Bird.create! name: "Bluejay" - assert_raises ActiveRecord::ReadOnlyError do + error = assert_raises ActiveRecord::ReadOnlyError do ActiveRecord::Base.connection.while_preventing_writes do bird.destroy! end end + + assert_match %r/\AWrite query attempted while in readonly mode: DELETE /, error.message end test "selecting a record does not raise if preventing writes" do diff --git a/activerecord/test/models/bird.rb b/activerecord/test/models/bird.rb index be08636ac6..cfefa555b3 100644 --- a/activerecord/test/models/bird.rb +++ b/activerecord/test/models/bird.rb @@ -6,6 +6,11 @@ class Bird < ActiveRecord::Base accepts_nested_attributes_for :pirate + before_save do + # force materialize_transactions + self.class.connection.materialize_transactions + end + attr_accessor :cancel_save_from_callback before_save :cancel_save_callback_method, if: :cancel_save_from_callback def cancel_save_callback_method |