openstax_transaction_isolation 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/Gemfile +22 -0
- data/LICENSE +19 -0
- data/README.md +118 -0
- data/Rakefile +11 -0
- data/lib/transaction_isolation/active_record/base.rb +13 -0
- data/lib/transaction_isolation/active_record/connection_adapters/abstract_adapter.rb +32 -0
- data/lib/transaction_isolation/active_record/connection_adapters/mysql2_adapter.rb +77 -0
- data/lib/transaction_isolation/active_record/connection_adapters/postgresql_adapter.rb +77 -0
- data/lib/transaction_isolation/active_record/connection_adapters/sqlite3_adapter.rb +76 -0
- data/lib/transaction_isolation/active_record/errors.rb +10 -0
- data/lib/transaction_isolation/version.rb +3 -0
- data/lib/transaction_isolation.rb +27 -0
- data/transaction_isolation.gemspec +21 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 642082ffd54b60574deeaf60055241d3382492d7cb93e33e4ab4c9910d9e7614
|
4
|
+
data.tar.gz: 0e364f699743134d38aac37dbdc3fb30129a230daf2433cfd007642337221af0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4461123948d11de2e25044a691845bbed39e3dc167f7edc3868bf5351efae9cdd165d341e99d03297c277ccaea32be320c6d35b13a3886050609b69b33256752
|
7
|
+
data.tar.gz: 8e2a38504706b6a815b5080600bf670dd28cdb6e54116135e3af3578a431105d6f2a7d0a8a47c7fb50b1cd50f9376a69314106ca382768e0e7d78c0941c92169
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in kontolib.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :test do
|
7
|
+
# Use the gem instead of a dated version bundled with Ruby
|
8
|
+
gem 'minitest', '5.3.4'
|
9
|
+
|
10
|
+
gem 'simplecov', :require => false
|
11
|
+
|
12
|
+
gem 'mysql2'
|
13
|
+
gem 'pg'
|
14
|
+
gem 'sqlite3'
|
15
|
+
end
|
16
|
+
|
17
|
+
group :development do
|
18
|
+
gem 'rake'
|
19
|
+
# enhance irb
|
20
|
+
gem 'awesome_print', :require => false
|
21
|
+
gem 'pry', :require => false
|
22
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2012 Piotr 'Qertoip' Włodarek
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
NO LONGER MAINTAINED - PLEASE FORK OR SEEK OTHER SOLUTIONS
|
2
|
+
|
3
|
+
|
4
|
+
# transaction_isolation
|
5
|
+
|
6
|
+
Set transaction isolation level in the ActiveRecord in a database agnostic way.
|
7
|
+
Works with MySQL, PostgreSQL and SQLite as long as you are using new adapters mysql2, pg or sqlite3.
|
8
|
+
Supports all ANSI SQL isolation levels: :serializable, :repeatable_read, :read_committed, :read_uncommitted.
|
9
|
+
|
10
|
+
See also [transaction_retry](https://github.com/qertoip/transaction_retry) gem for auto-retrying transactions
|
11
|
+
on deadlocks and serialization errors.
|
12
|
+
|
13
|
+
## Example
|
14
|
+
|
15
|
+
ActiveRecord::Base.isolation_level( :serializable ) do
|
16
|
+
# your code
|
17
|
+
end
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this to your Gemfile:
|
22
|
+
|
23
|
+
gem 'transaction_isolation'
|
24
|
+
|
25
|
+
Then run:
|
26
|
+
|
27
|
+
bundle
|
28
|
+
|
29
|
+
__It works out of the box with Ruby on Rails__.
|
30
|
+
|
31
|
+
If you have a standalone ActiveRecord-based project you'll need to call:
|
32
|
+
|
33
|
+
TransactionIsolation.apply_activerecord_patch # after connecting to the database
|
34
|
+
|
35
|
+
__after__ connecting to the database. This is because ActiveRecord loads adapters lazilly and only then they can be patched.
|
36
|
+
|
37
|
+
## Features
|
38
|
+
|
39
|
+
* Setting transaction isolation level: :serializable, :repeatable_read, :read_committed, :read_uncommitted
|
40
|
+
* Auto-reverting to the original isolation level after the block
|
41
|
+
* Database agnostic
|
42
|
+
* MySQL, PostgreSQL and SQLite supported
|
43
|
+
* Exception translation. All deadlocks and serialization errors are wrapped in a ActiveRecord::TransactionIsolationConflict exception
|
44
|
+
* Use it in your Rails application or a standalone ActiveRecord-based project
|
45
|
+
|
46
|
+
## Testimonials
|
47
|
+
|
48
|
+
This gem was initially developed for and successfully works in production at [Kontomierz.pl](http://kontomierz.pl) - the finest Polish personal finance app.
|
49
|
+
|
50
|
+
## Real world example
|
51
|
+
|
52
|
+
When implementing a table-based job queue you should ensure that only one worker process can pop a particular job from the queue.
|
53
|
+
Wrapping your code in a transaction is not enough because by default databases do not isolate transactions to the full extent,
|
54
|
+
which leads to occasional phantom reads. It is therefore necessary to manually raise the transaction isolation level.
|
55
|
+
The highest level of transaction isolation is called "serializable" and that's what we need here:
|
56
|
+
|
57
|
+
class QueuedJob < ActiveRecord::Base
|
58
|
+
|
59
|
+
# Job status
|
60
|
+
TODO = 1
|
61
|
+
PROCESSING = 2
|
62
|
+
DONE = 3
|
63
|
+
|
64
|
+
# Returns first job from the queue or nil if the queue is empty
|
65
|
+
def pop
|
66
|
+
QueuedJob.isolation_level( :serializable ) do
|
67
|
+
QueuedJob.transaction do
|
68
|
+
queued_job = find_by_status( TODO )
|
69
|
+
if queud_job
|
70
|
+
queued_job.update_attribute( :status, PROCESSING )
|
71
|
+
return queued_job
|
72
|
+
else
|
73
|
+
return nil
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
rescue ActiveRecord::TransactionIsolationConflict => e
|
78
|
+
logger.warn( e.message )
|
79
|
+
retry
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
[Read more about isolation levels in Wikipedia](http://tinyurl.com/nrqjbb)
|
85
|
+
|
86
|
+
## Requirements
|
87
|
+
|
88
|
+
* Ruby 1.9.2
|
89
|
+
* ActiveRecord 3.0.11+
|
90
|
+
|
91
|
+
## Running tests
|
92
|
+
|
93
|
+
Run tests on the selected database (mysql2 by default):
|
94
|
+
|
95
|
+
db=mysql2 bundle exec rake test
|
96
|
+
db=postgresql bundle exec rake test
|
97
|
+
db=sqlite3 bundle exec rake test
|
98
|
+
|
99
|
+
Run tests on all supported databases:
|
100
|
+
|
101
|
+
./tests
|
102
|
+
|
103
|
+
Database configuration is hardcoded in test/db/db.rb; feel free to improve this and submit a pull request.
|
104
|
+
|
105
|
+
## How intrusive is this gem?
|
106
|
+
|
107
|
+
You should be very suspicious about any gem that monkey patches your stock Ruby on Rails framework.
|
108
|
+
|
109
|
+
This gem is carefully written to not be more intrusive than it needs to be:
|
110
|
+
|
111
|
+
* introduces several new methods to Mysql2Adapter, PostgreSQLAdapter, SQLite3Adapter; names are carefully taken to not collide with future changes
|
112
|
+
* wraps #translate_exception method using alias_method_chain to add new translation
|
113
|
+
* introduces new class ActiveRecord::TransactionIsolationConflict in the ActiveRecord module
|
114
|
+
* introduces new convenience method ActiveRecord::Base.isolation_level akin to ActiveRecord::Base.transaction
|
115
|
+
|
116
|
+
## License
|
117
|
+
|
118
|
+
Released under the MIT license. Copyright (C) 2012 Piotr 'Qertoip' Włodarek.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'active_record/base'
|
2
|
+
|
3
|
+
module TransactionIsolation
|
4
|
+
module ActiveRecord
|
5
|
+
module Base
|
6
|
+
def isolation_level( isolation_level, &block )
|
7
|
+
connection.isolation_level( isolation_level, &block )
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveRecord::Base.extend( TransactionIsolation::ActiveRecord::Base )
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'active_record/connection_adapters/abstract_adapter'
|
2
|
+
|
3
|
+
module TransactionIsolation
|
4
|
+
module ActiveRecord
|
5
|
+
module ConnectionAdapters # :nodoc:
|
6
|
+
module AbstractAdapter
|
7
|
+
|
8
|
+
VALID_ISOLATION_LEVELS = [:read_uncommitted, :read_committed, :repeatable_read, :serializable]
|
9
|
+
|
10
|
+
# If true, #isolation_level(level) method is available
|
11
|
+
def supports_isolation_levels?
|
12
|
+
false
|
13
|
+
end
|
14
|
+
|
15
|
+
def isolation_level( level )
|
16
|
+
raise NotImplementedError
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def validate_isolation_level( isolation_level )
|
22
|
+
unless VALID_ISOLATION_LEVELS.include?( isolation_level )
|
23
|
+
raise ArgumentError, "Invalid isolation level '#{isolation_level}'. Supported levels include #{VALID_ISOLATION_LEVELS.join( ', ' )}."
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
ActiveRecord::ConnectionAdapters::AbstractAdapter.send( :include, TransactionIsolation::ActiveRecord::ConnectionAdapters::AbstractAdapter )
|
@@ -0,0 +1,77 @@
|
|
1
|
+
if defined?( ActiveRecord::ConnectionAdapters::Mysql2Adapter )
|
2
|
+
|
3
|
+
module TransactionIsolation
|
4
|
+
module ActiveRecord
|
5
|
+
module ConnectionAdapters # :nodoc:
|
6
|
+
module Mysql2Adapter
|
7
|
+
|
8
|
+
def self.included( base )
|
9
|
+
base.class_eval do
|
10
|
+
alias_method :translate_exception_without_transaction_isolation_conflict, :translate_exception
|
11
|
+
alias_method :translate_exception, :translate_exception_with_transaction_isolation_conflict
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def supports_isolation_levels?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
VENDOR_ISOLATION_LEVEL = {
|
20
|
+
:read_uncommitted => 'READ UNCOMMITTED',
|
21
|
+
:read_committed => 'READ COMMITTED',
|
22
|
+
:repeatable_read => 'REPEATABLE READ',
|
23
|
+
:serializable => 'SERIALIZABLE'
|
24
|
+
}
|
25
|
+
|
26
|
+
ANSI_ISOLATION_LEVEL = {
|
27
|
+
'READ UNCOMMITTED' => :read_uncommitted,
|
28
|
+
'READ COMMITTED' => :read_committed,
|
29
|
+
'REPEATABLE READ' => :repeatable_read,
|
30
|
+
'SERIALIZABLE' => :serializable
|
31
|
+
}
|
32
|
+
|
33
|
+
def current_isolation_level
|
34
|
+
ANSI_ISOLATION_LEVEL[current_vendor_isolation_level]
|
35
|
+
end
|
36
|
+
|
37
|
+
def current_vendor_isolation_level
|
38
|
+
select_value( "SELECT @@session.tx_isolation" ).gsub( '-', ' ' )
|
39
|
+
end
|
40
|
+
|
41
|
+
def isolation_level( level )
|
42
|
+
validate_isolation_level( level )
|
43
|
+
|
44
|
+
original_vendor_isolation_level = current_vendor_isolation_level if block_given?
|
45
|
+
|
46
|
+
execute( "SET SESSION TRANSACTION ISOLATION LEVEL #{VENDOR_ISOLATION_LEVEL[level]}" )
|
47
|
+
|
48
|
+
begin
|
49
|
+
yield
|
50
|
+
ensure
|
51
|
+
execute "SET SESSION TRANSACTION ISOLATION LEVEL #{original_vendor_isolation_level}"
|
52
|
+
end if block_given?
|
53
|
+
end
|
54
|
+
|
55
|
+
def translate_exception_with_transaction_isolation_conflict( exception, **args )
|
56
|
+
if isolation_conflict?( exception )
|
57
|
+
::ActiveRecord::TransactionIsolationConflict.new( "Transaction isolation conflict detected: #{exception.message}" )
|
58
|
+
else
|
59
|
+
translate_exception_without_transaction_isolation_conflict( exception, **args )
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def isolation_conflict?( exception )
|
64
|
+
[ "Deadlock found when trying to get lock",
|
65
|
+
"Lock wait timeout exceeded"].any? do |error_message|
|
66
|
+
exception.message =~ /#{Regexp.escape( error_message )}/i
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
ActiveRecord::ConnectionAdapters::Mysql2Adapter.send( :include, TransactionIsolation::ActiveRecord::ConnectionAdapters::Mysql2Adapter )
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
if defined?( ActiveRecord::ConnectionAdapters::PostgreSQLAdapter )
|
2
|
+
|
3
|
+
module TransactionIsolation
|
4
|
+
module ActiveRecord
|
5
|
+
module ConnectionAdapters # :nodoc:
|
6
|
+
module PostgreSQLAdapter
|
7
|
+
|
8
|
+
def self.included( base )
|
9
|
+
base.class_eval do
|
10
|
+
alias_method :translate_exception_without_transaction_isolation_conflict, :translate_exception
|
11
|
+
alias_method :translate_exception, :translate_exception_with_transaction_isolation_conflict
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def supports_isolation_levels?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
VENDOR_ISOLATION_LEVEL = {
|
20
|
+
:read_uncommitted => 'READ UNCOMMITTED',
|
21
|
+
:read_committed => 'READ COMMITTED',
|
22
|
+
:repeatable_read => 'REPEATABLE READ',
|
23
|
+
:serializable => 'SERIALIZABLE'
|
24
|
+
}
|
25
|
+
|
26
|
+
ANSI_ISOLATION_LEVEL = {
|
27
|
+
'READ UNCOMMITTED' => :read_uncommitted,
|
28
|
+
'READ COMMITTED' => :read_committed,
|
29
|
+
'REPEATABLE READ' => :repeatable_read,
|
30
|
+
'SERIALIZABLE' => :serializable
|
31
|
+
}
|
32
|
+
|
33
|
+
def current_isolation_level
|
34
|
+
ANSI_ISOLATION_LEVEL[current_vendor_isolation_level]
|
35
|
+
end
|
36
|
+
|
37
|
+
def current_vendor_isolation_level
|
38
|
+
select_value( "SHOW TRANSACTION ISOLATION LEVEL" ).upcase
|
39
|
+
end
|
40
|
+
|
41
|
+
def isolation_level( level )
|
42
|
+
validate_isolation_level( level )
|
43
|
+
|
44
|
+
original_vendor_isolation_level = current_vendor_isolation_level if block_given?
|
45
|
+
|
46
|
+
execute "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL #{VENDOR_ISOLATION_LEVEL[level]}"
|
47
|
+
|
48
|
+
begin
|
49
|
+
yield
|
50
|
+
ensure
|
51
|
+
execute "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL #{original_vendor_isolation_level}"
|
52
|
+
end if block_given?
|
53
|
+
end
|
54
|
+
|
55
|
+
def translate_exception_with_transaction_isolation_conflict( exception, **args )
|
56
|
+
if isolation_conflict?( exception )
|
57
|
+
::ActiveRecord::TransactionIsolationConflict.new( "Transaction isolation conflict detected: #{exception.message}" )
|
58
|
+
else
|
59
|
+
translate_exception_without_transaction_isolation_conflict( exception, **args )
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def isolation_conflict?( exception )
|
64
|
+
[ "deadlock detected",
|
65
|
+
"could not serialize access" ].any? do |error_message|
|
66
|
+
exception.message =~ /#{Regexp.escape( error_message )}/i
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send( :include, TransactionIsolation::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter )
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
if defined?( ActiveRecord::ConnectionAdapters::SQLite3Adapter )
|
2
|
+
|
3
|
+
module TransactionIsolation
|
4
|
+
module ActiveRecord
|
5
|
+
module ConnectionAdapters # :nodoc:
|
6
|
+
module SQLite3Adapter
|
7
|
+
|
8
|
+
def self.included( base )
|
9
|
+
base.class_eval do
|
10
|
+
alias_method :translate_exception_without_transaction_isolation_conflict, :translate_exception
|
11
|
+
alias_method :translate_exception, :translate_exception_with_transaction_isolation_conflict
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def supports_isolation_levels?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
VENDOR_ISOLATION_LEVEL = {
|
20
|
+
:read_uncommitted => 'read_uncommitted = 1',
|
21
|
+
:read_committed => 'read_uncommitted = 0',
|
22
|
+
:repeatable_read => 'read_uncommitted = 0',
|
23
|
+
:serializable => 'read_uncommitted = 0'
|
24
|
+
}
|
25
|
+
|
26
|
+
ANSI_ISOLATION_LEVEL = {
|
27
|
+
'read_uncommitted = 1' => :read_uncommitted,
|
28
|
+
'read_uncommitted = 0' => :serializable
|
29
|
+
}
|
30
|
+
|
31
|
+
def current_isolation_level
|
32
|
+
ANSI_ISOLATION_LEVEL[current_vendor_isolation_level]
|
33
|
+
end
|
34
|
+
|
35
|
+
def current_vendor_isolation_level
|
36
|
+
"read_uncommitted = #{select_value( "PRAGMA read_uncommitted" )}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def isolation_level( level )
|
40
|
+
validate_isolation_level( level )
|
41
|
+
|
42
|
+
original_vendor_isolation_level = current_vendor_isolation_level if block_given?
|
43
|
+
|
44
|
+
execute "PRAGMA #{VENDOR_ISOLATION_LEVEL[level]}"
|
45
|
+
|
46
|
+
begin
|
47
|
+
yield
|
48
|
+
ensure
|
49
|
+
execute "PRAGMA #{original_vendor_isolation_level}"
|
50
|
+
end if block_given?
|
51
|
+
end
|
52
|
+
|
53
|
+
def translate_exception_with_transaction_isolation_conflict( exception, **args )
|
54
|
+
if isolation_conflict?( exception )
|
55
|
+
::ActiveRecord::TransactionIsolationConflict.new( "Transaction isolation conflict detected: #{exception.message}" )
|
56
|
+
else
|
57
|
+
translate_exception_without_transaction_isolation_conflict( exception, **args )
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# http://www.sqlite.org/c3ref/c_abort.html
|
62
|
+
def isolation_conflict?( exception )
|
63
|
+
[ "The database file is locked",
|
64
|
+
"A table in the database is locked",
|
65
|
+
"Database lock protocol error"].any? do |error_message|
|
66
|
+
exception.message =~ /#{Regexp.escape( error_message )}/i
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
ActiveRecord::ConnectionAdapters::SQLite3Adapter.send( :include, TransactionIsolation::ActiveRecord::ConnectionAdapters::SQLite3Adapter )
|
75
|
+
|
76
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'active_record/errors'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
# This exception represents both deadlocks and serialization conflicts.
|
5
|
+
# Deadlocks happen when db engine is using lock-based concurrency control.
|
6
|
+
# Serialization conflicts happen when db engine is using multi-version concurrency control.
|
7
|
+
# Often db engines combine both approaches and thus generate both types of errors.
|
8
|
+
|
9
|
+
class TransactionIsolationConflict < ::ActiveRecord::WrappedDatabaseException; end
|
10
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require_relative 'transaction_isolation/version'
|
3
|
+
|
4
|
+
module TransactionIsolation
|
5
|
+
|
6
|
+
# Must be called after ActiveRecord established a connection.
|
7
|
+
# Only then we know which connection adapter is actually loaded and can be enhanced.
|
8
|
+
# Please note ActiveRecord does not load unused adapters.
|
9
|
+
def self.apply_activerecord_patch
|
10
|
+
require_relative 'transaction_isolation/active_record/errors'
|
11
|
+
require_relative 'transaction_isolation/active_record/base'
|
12
|
+
require_relative 'transaction_isolation/active_record/connection_adapters/abstract_adapter'
|
13
|
+
require_relative 'transaction_isolation/active_record/connection_adapters/mysql2_adapter'
|
14
|
+
require_relative 'transaction_isolation/active_record/connection_adapters/postgresql_adapter'
|
15
|
+
require_relative 'transaction_isolation/active_record/connection_adapters/sqlite3_adapter'
|
16
|
+
end
|
17
|
+
|
18
|
+
if defined?( ::Rails )
|
19
|
+
# Setup applying the patch after Rails is initialized.
|
20
|
+
class Railtie < ::Rails::Railtie
|
21
|
+
config.after_initialize do
|
22
|
+
TransactionIsolation.apply_activerecord_patch
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "transaction_isolation/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "openstax_transaction_isolation"
|
7
|
+
s.version = TransactionIsolation::VERSION
|
8
|
+
s.authors = ["Dante Soares", "Piotr 'Qertoip' Włodarek"]
|
9
|
+
s.email = []
|
10
|
+
s.homepage = "https://github.com/openstax/transaction_isolation"
|
11
|
+
s.summary = %q{Set transaction isolation level in the ActiveRecord in a database agnostic way.}
|
12
|
+
s.description = %q{Set transaction isolation level in the ActiveRecord in a database agnostic way.
|
13
|
+
Works with MySQL, PostgreSQL and SQLite as long as you are using new adapters mysql2, pg or sqlite3.
|
14
|
+
Supports all ANSI SQL isolation levels: :serializable, :repeatable_read, :read_committed, :read_uncommitted.}
|
15
|
+
s.required_ruby_version = '>= 3'
|
16
|
+
|
17
|
+
s.files = `git ls-files -z`.split("\x0").reject { |f| f == 'd' || f.start_with?('test') }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_runtime_dependency "activerecord", ">= 6"
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: openstax_transaction_isolation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dante Soares
|
8
|
+
- Piotr 'Qertoip' Włodarek
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2025-01-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '6'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '6'
|
28
|
+
description: |-
|
29
|
+
Set transaction isolation level in the ActiveRecord in a database agnostic way.
|
30
|
+
Works with MySQL, PostgreSQL and SQLite as long as you are using new adapters mysql2, pg or sqlite3.
|
31
|
+
Supports all ANSI SQL isolation levels: :serializable, :repeatable_read, :read_committed, :read_uncommitted.
|
32
|
+
email: []
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- ".gitignore"
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/transaction_isolation.rb
|
43
|
+
- lib/transaction_isolation/active_record/base.rb
|
44
|
+
- lib/transaction_isolation/active_record/connection_adapters/abstract_adapter.rb
|
45
|
+
- lib/transaction_isolation/active_record/connection_adapters/mysql2_adapter.rb
|
46
|
+
- lib/transaction_isolation/active_record/connection_adapters/postgresql_adapter.rb
|
47
|
+
- lib/transaction_isolation/active_record/connection_adapters/sqlite3_adapter.rb
|
48
|
+
- lib/transaction_isolation/active_record/errors.rb
|
49
|
+
- lib/transaction_isolation/version.rb
|
50
|
+
- transaction_isolation.gemspec
|
51
|
+
homepage: https://github.com/openstax/transaction_isolation
|
52
|
+
licenses: []
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '3'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubygems_version: 3.5.21
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: Set transaction isolation level in the ActiveRecord in a database agnostic
|
73
|
+
way.
|
74
|
+
test_files: []
|