trx_ext 1.0.5 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/.ruby-version +1 -1
- data/CHANGELOG.md +10 -2
- data/Gemfile +24 -3
- data/README.md +40 -29
- data/bin/console +0 -12
- data/bin/rails +9 -0
- data/bin/test_all_ar_versions +1 -3
- data/docker/my.cnf +13 -0
- data/docker/mysqld.sh +22 -0
- data/docker-compose.yml +22 -0
- data/dummy_app/.gitattributes +7 -0
- data/dummy_app/.gitignore +38 -0
- data/dummy_app/README.md +24 -0
- data/dummy_app/Rakefile +6 -0
- data/dummy_app/app/controllers/application_controller.rb +2 -0
- data/dummy_app/app/models/application_mysql_record.rb +7 -0
- data/dummy_app/app/models/application_pg_record.rb +7 -0
- data/dummy_app/app/models/application_sqlite_record.rb +7 -0
- data/dummy_app/app/models/application_trilogy_record.rb +7 -0
- data/dummy_app/app/models/dummy_mysql_record.rb +11 -0
- data/dummy_app/app/models/dummy_pg_record.rb +11 -0
- data/dummy_app/app/models/dummy_sqlite_record.rb +11 -0
- data/dummy_app/app/models/dummy_trilogy_record.rb +11 -0
- data/dummy_app/bin/bundle +109 -0
- data/dummy_app/bin/rails +4 -0
- data/dummy_app/bin/rake +4 -0
- data/dummy_app/bin/setup +33 -0
- data/dummy_app/config/application.rb +51 -0
- data/dummy_app/config/boot.rb +4 -0
- data/dummy_app/config/credentials.yml +0 -0
- data/dummy_app/config/database.yml +70 -0
- data/dummy_app/config/environment.rb +5 -0
- data/dummy_app/config/environments/development.rb +66 -0
- data/dummy_app/config/environments/test.rb +61 -0
- data/dummy_app/config/initializers/.keep +0 -0
- data/dummy_app/config/locales/en.yml +2 -0
- data/dummy_app/config/locales/fr-CH.yml +2 -0
- data/dummy_app/config/puma.rb +43 -0
- data/dummy_app/config/routes.rb +5 -0
- data/dummy_app/config.ru +6 -0
- data/dummy_app/db/migrate/.keep +0 -0
- data/dummy_app/db/migrate/20240128111549_create_dummy_record.rb +10 -0
- data/dummy_app/db/primary_mysql/20240128111549_create_dummy_mysql_record.rb +10 -0
- data/dummy_app/db/primary_sqlite/20240128111549_create_dummy_sqlite_record.rb +10 -0
- data/dummy_app/db/primary_trilogy/20240128111549_create_dummy_trilogy_record.rb +10 -0
- data/dummy_app/db/seeds.rb +7 -0
- data/dummy_app/lib/tasks/.keep +0 -0
- data/dummy_app/log/.keep +0 -0
- data/dummy_app/public/robots.txt +1 -0
- data/dummy_app/storage/.keep +0 -0
- data/dummy_app/tmp/.keep +0 -0
- data/dummy_app/vendor/.keep +0 -0
- data/lib/trx_ext/object_ext.rb +12 -7
- data/lib/trx_ext/retry.rb +17 -27
- data/lib/trx_ext/transaction.rb +1 -1
- data/lib/trx_ext/version.rb +1 -2
- data/lib/trx_ext.rb +18 -21
- data/trx_ext.gemspec +1 -7
- metadata +53 -93
- data/bin/setup +0 -8
- data/lib/trx_ext/railtie.rb +0 -9
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "boot"
|
4
|
+
|
5
|
+
require "rails"
|
6
|
+
|
7
|
+
%w(
|
8
|
+
active_record/railtie
|
9
|
+
action_controller/railtie
|
10
|
+
action_view/railtie
|
11
|
+
).each do |railtie|
|
12
|
+
begin
|
13
|
+
require railtie
|
14
|
+
rescue LoadError
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
# Require the gems listed in Gemfile, including any gems
|
20
|
+
# you've limited to :test, :development, or :production.
|
21
|
+
Bundler.require(*Rails.groups)
|
22
|
+
|
23
|
+
module DummyApp
|
24
|
+
class Application < Rails::Application
|
25
|
+
rails_version = Gem::Specification.find_by_name('rails').version
|
26
|
+
# Initialize configuration defaults for originally generated Rails version.
|
27
|
+
config.load_defaults rails_version.segments[0..1].join('.')
|
28
|
+
|
29
|
+
# Configuration for the application, engines, and railties goes here.
|
30
|
+
#
|
31
|
+
# These settings can be overridden in specific environments using the files
|
32
|
+
# in config/environments, which are processed later.
|
33
|
+
#
|
34
|
+
# config.time_zone = "Central Time (US & Canada)"
|
35
|
+
# config.eager_load_paths << Rails.root.join("extras")
|
36
|
+
|
37
|
+
# Only loads a smaller set of middleware suitable for API only apps.
|
38
|
+
# Middleware like session, flash, cookies can be added back manually.
|
39
|
+
# Skip views, helpers and assets when generating a new resource.
|
40
|
+
config.api_only = true
|
41
|
+
if rails_version < Gem::Version.new('7.0.0')
|
42
|
+
config.active_record.legacy_connection_handling = false
|
43
|
+
end
|
44
|
+
# config.active_job.queue_adapter = :async
|
45
|
+
|
46
|
+
|
47
|
+
if defined?(FactoryBotRails)
|
48
|
+
config.factory_bot.definition_file_paths += [File.expand_path('../../spec/factories', __dir__)]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
File without changes
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# SQLite. Versions 3.8.0 and up are supported.
|
2
|
+
# gem install sqlite3
|
3
|
+
#
|
4
|
+
# Ensure the SQLite 3 gem is defined in your Gemfile
|
5
|
+
# gem "sqlite3"
|
6
|
+
#
|
7
|
+
default_pg: &default_pg
|
8
|
+
adapter: 'postgresql'
|
9
|
+
username: postgres
|
10
|
+
password: postgres
|
11
|
+
host: localhost
|
12
|
+
port: 5532
|
13
|
+
pool: <%= Etc.nprocessors + 1 %>
|
14
|
+
timeout: 5000
|
15
|
+
prepared_statements: false
|
16
|
+
|
17
|
+
default_sqlite: &default_sqlite
|
18
|
+
adapter: 'sqlite3'
|
19
|
+
pool: <%= Etc.nprocessors + 1 %>
|
20
|
+
timeout: 1000
|
21
|
+
prepared_statements: false
|
22
|
+
migrations_paths: db/primary_sqlite
|
23
|
+
|
24
|
+
default_mysql: &default_mysql
|
25
|
+
adapter: 'mysql2'
|
26
|
+
username: root
|
27
|
+
host: 127.0.0.1
|
28
|
+
port: 3406
|
29
|
+
pool: <%= Etc.nprocessors + 1 %>
|
30
|
+
timeout: 5000
|
31
|
+
prepared_statements: false
|
32
|
+
migrations_paths: db/primary_mysql
|
33
|
+
|
34
|
+
default_trilogy: &default_trilogy
|
35
|
+
adapter: 'trilogy'
|
36
|
+
username: root
|
37
|
+
host: 127.0.0.1
|
38
|
+
port: 3406
|
39
|
+
pool: <%= Etc.nprocessors + 1 %>
|
40
|
+
timeout: 5000
|
41
|
+
prepared_statements: false
|
42
|
+
migrations_paths: db/primary_trilogy
|
43
|
+
|
44
|
+
development:
|
45
|
+
primary_pg:
|
46
|
+
<<: *default_pg
|
47
|
+
database: trx_ext
|
48
|
+
primary_sqlite:
|
49
|
+
<<: *default_sqlite
|
50
|
+
database: db/trx_ext.sqlite3
|
51
|
+
primary_mysql:
|
52
|
+
<<: *default_mysql
|
53
|
+
database: trx_ext
|
54
|
+
primary_trilogy:
|
55
|
+
<<: *default_trilogy
|
56
|
+
database: trx_ext_trilogy
|
57
|
+
|
58
|
+
test:
|
59
|
+
primary_pg:
|
60
|
+
<<: *default_pg
|
61
|
+
database: trx_ext_test
|
62
|
+
primary_sqlite:
|
63
|
+
<<: *default_sqlite
|
64
|
+
database: db/trx_ext_test.sqlite3
|
65
|
+
primary_mysql:
|
66
|
+
<<: *default_mysql
|
67
|
+
database: trx_ext_test
|
68
|
+
primary_trilogy:
|
69
|
+
<<: *default_trilogy
|
70
|
+
database: trx_ext_trilogy_test
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require "active_support/core_ext/integer/time"
|
2
|
+
|
3
|
+
Rails.application.configure do
|
4
|
+
# Settings specified here will take precedence over those in config/application.rb.
|
5
|
+
|
6
|
+
# In the development environment your application's code is reloaded any time
|
7
|
+
# it changes. This slows down response time but is perfect for development
|
8
|
+
# since you don't have to restart the web server when you make code changes.
|
9
|
+
config.cache_classes = false
|
10
|
+
|
11
|
+
# Do not eager load code on boot.
|
12
|
+
config.eager_load = false
|
13
|
+
|
14
|
+
# Show full error reports.
|
15
|
+
config.consider_all_requests_local = true
|
16
|
+
|
17
|
+
# Enable server timing
|
18
|
+
config.server_timing = true
|
19
|
+
|
20
|
+
# Enable/disable caching. By default caching is disabled.
|
21
|
+
# Run rails dev:cache to toggle caching.
|
22
|
+
if Rails.root.join("tmp/caching-dev.txt").exist?
|
23
|
+
config.cache_store = :memory_store
|
24
|
+
config.public_file_server.headers = {
|
25
|
+
"Cache-Control" => "public, max-age=#{2.days.to_i}"
|
26
|
+
}
|
27
|
+
else
|
28
|
+
config.action_controller.perform_caching = false
|
29
|
+
|
30
|
+
config.cache_store = :null_store
|
31
|
+
end
|
32
|
+
|
33
|
+
# Store uploaded files on the local file system (see config/storage.yml for options).
|
34
|
+
# config.active_storage.service = :local
|
35
|
+
|
36
|
+
# Don't care if the mailer can't send.
|
37
|
+
# config.action_mailer.raise_delivery_errors = false
|
38
|
+
|
39
|
+
# config.action_mailer.perform_caching = false
|
40
|
+
|
41
|
+
# Print deprecation notices to the Rails logger.
|
42
|
+
config.active_support.deprecation = :log
|
43
|
+
|
44
|
+
# Raise exceptions for disallowed deprecations.
|
45
|
+
config.active_support.disallowed_deprecation = :raise
|
46
|
+
|
47
|
+
# Tell Active Support which deprecation messages to disallow.
|
48
|
+
config.active_support.disallowed_deprecation_warnings = []
|
49
|
+
|
50
|
+
# Raise an error on page load if there are pending migrations.
|
51
|
+
config.active_record.migration_error = :page_load
|
52
|
+
|
53
|
+
# Highlight code that triggered database queries in logs.
|
54
|
+
config.active_record.verbose_query_logs = true
|
55
|
+
|
56
|
+
|
57
|
+
# Raises error for missing translations.
|
58
|
+
# config.i18n.raise_on_missing_translations = true
|
59
|
+
|
60
|
+
# Annotate rendered view with file names.
|
61
|
+
# config.action_view.annotate_rendered_view_with_filenames = true
|
62
|
+
|
63
|
+
# Uncomment if you wish to allow Action Cable access from any origin.
|
64
|
+
# config.action_cable.disable_request_forgery_protection = true
|
65
|
+
config.logger = ActiveSupport::Logger.new(File.expand_path('../../../log/development.log', __dir__))
|
66
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "active_support/core_ext/integer/time"
|
2
|
+
|
3
|
+
# The test environment is used exclusively to run your application's
|
4
|
+
# test suite. You never need to work with it otherwise. Remember that
|
5
|
+
# your test database is "scratch space" for the test suite and is wiped
|
6
|
+
# and recreated between test runs. Don't rely on the data there!
|
7
|
+
|
8
|
+
Rails.application.configure do
|
9
|
+
# Settings specified here will take precedence over those in config/application.rb.
|
10
|
+
|
11
|
+
# Turn false under Spring and add config.action_view.cache_template_loading = true.
|
12
|
+
config.cache_classes = true
|
13
|
+
|
14
|
+
# Eager loading loads your whole application. When running a single test locally,
|
15
|
+
# this probably isn't necessary. It's a good idea to do in a continuous integration
|
16
|
+
# system, or in some way before deploying your code.
|
17
|
+
config.eager_load = ENV["CI"].present?
|
18
|
+
|
19
|
+
# Configure public file server for tests with Cache-Control for performance.
|
20
|
+
config.public_file_server.enabled = true
|
21
|
+
config.public_file_server.headers = {
|
22
|
+
"Cache-Control" => "public, max-age=#{1.hour.to_i}"
|
23
|
+
}
|
24
|
+
|
25
|
+
# Show full error reports and disable caching.
|
26
|
+
config.consider_all_requests_local = true
|
27
|
+
config.action_controller.perform_caching = false
|
28
|
+
config.cache_store = :null_store
|
29
|
+
|
30
|
+
# Raise exceptions instead of rendering exception templates.
|
31
|
+
config.action_dispatch.show_exceptions = false
|
32
|
+
|
33
|
+
# Disable request forgery protection in test environment.
|
34
|
+
config.action_controller.allow_forgery_protection = false
|
35
|
+
|
36
|
+
# Store uploaded files on the local file system in a temporary directory.
|
37
|
+
# config.active_storage.service = :test
|
38
|
+
|
39
|
+
# config.action_mailer.perform_caching = false
|
40
|
+
|
41
|
+
# Tell Action Mailer not to deliver emails to the real world.
|
42
|
+
# The :test delivery method accumulates sent emails in the
|
43
|
+
# ActionMailer::Base.deliveries array.
|
44
|
+
# config.action_mailer.delivery_method = :test
|
45
|
+
|
46
|
+
# Print deprecation notices to the stderr.
|
47
|
+
config.active_support.deprecation = :stderr
|
48
|
+
|
49
|
+
# Raise exceptions for disallowed deprecations.
|
50
|
+
config.active_support.disallowed_deprecation = :raise
|
51
|
+
|
52
|
+
# Tell Active Support which deprecation messages to disallow.
|
53
|
+
config.active_support.disallowed_deprecation_warnings = []
|
54
|
+
|
55
|
+
# Raises error for missing translations.
|
56
|
+
# config.i18n.raise_on_missing_translations = true
|
57
|
+
|
58
|
+
# Annotate rendered view with file names.
|
59
|
+
# config.action_view.annotate_rendered_view_with_filenames = true
|
60
|
+
config.logger = ActiveSupport::Logger.new(File.expand_path('../../../log/test.log', __dir__))
|
61
|
+
end
|
File without changes
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# Puma can serve each request in a thread from an internal thread pool.
|
2
|
+
# The `threads` method setting takes two numbers: a minimum and maximum.
|
3
|
+
# Any libraries that use thread pools should be configured to match
|
4
|
+
# the maximum value specified for Puma. Default is set to 5 threads for minimum
|
5
|
+
# and maximum; this matches the default thread size of Active Record.
|
6
|
+
#
|
7
|
+
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
|
8
|
+
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
|
9
|
+
threads min_threads_count, max_threads_count
|
10
|
+
|
11
|
+
# Specifies the `worker_timeout` threshold that Puma will use to wait before
|
12
|
+
# terminating a worker in development environments.
|
13
|
+
#
|
14
|
+
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"
|
15
|
+
|
16
|
+
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
|
17
|
+
#
|
18
|
+
port ENV.fetch("PORT") { 3000 }
|
19
|
+
|
20
|
+
# Specifies the `environment` that Puma will run in.
|
21
|
+
#
|
22
|
+
environment ENV.fetch("RAILS_ENV") { "development" }
|
23
|
+
|
24
|
+
# Specifies the `pidfile` that Puma will use.
|
25
|
+
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }
|
26
|
+
|
27
|
+
# Specifies the number of `workers` to boot in clustered mode.
|
28
|
+
# Workers are forked web server processes. If using threads and workers together
|
29
|
+
# the concurrency of the application would be max `threads` * `workers`.
|
30
|
+
# Workers do not work on JRuby or Windows (both of which do not support
|
31
|
+
# processes).
|
32
|
+
#
|
33
|
+
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }
|
34
|
+
|
35
|
+
# Use the `preload_app!` method when specifying a `workers` number.
|
36
|
+
# This directive tells Puma to first boot the application and load code
|
37
|
+
# before forking the application. This takes advantage of Copy On Write
|
38
|
+
# process behavior so workers use less memory.
|
39
|
+
#
|
40
|
+
# preload_app!
|
41
|
+
|
42
|
+
# Allow puma to be restarted by `bin/rails restart` command.
|
43
|
+
plugin :tmp_restart
|
data/dummy_app/config.ru
ADDED
File without changes
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class CreateDummyRecord < ActiveRecord::Migration[6.1]
|
2
|
+
def change
|
3
|
+
create_table :dummy_pg_records do |t|
|
4
|
+
t.string :name
|
5
|
+
t.string :unique_name
|
6
|
+
t.datetime :created_at, precision: 6, null: false
|
7
|
+
end
|
8
|
+
add_index :dummy_pg_records, :unique_name, unique: true
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class CreateDummyMysqlRecord < ActiveRecord::Migration[6.1]
|
2
|
+
def change
|
3
|
+
create_table :dummy_mysql_records do |t|
|
4
|
+
t.string :name
|
5
|
+
t.string :unique_name
|
6
|
+
t.datetime :created_at, precision: 6, null: false
|
7
|
+
end
|
8
|
+
add_index :dummy_mysql_records, :unique_name, unique: true
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class CreateDummySqliteRecord < ActiveRecord::Migration[6.1]
|
2
|
+
def change
|
3
|
+
create_table :dummy_sqlite_records do |t|
|
4
|
+
t.string :name
|
5
|
+
t.string :unique_name
|
6
|
+
t.datetime :created_at, precision: 6, null: false
|
7
|
+
end
|
8
|
+
add_index :dummy_sqlite_records, :unique_name, unique: true
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class CreateDummyTrilogyRecord < ActiveRecord::Migration[6.1]
|
2
|
+
def change
|
3
|
+
create_table :dummy_trilogy_records do |t|
|
4
|
+
t.string :name
|
5
|
+
t.string :unique_name
|
6
|
+
t.datetime :created_at, precision: 6, null: false
|
7
|
+
end
|
8
|
+
add_index :dummy_trilogy_records, :unique_name, unique: true
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# This file should contain all the record creation needed to seed the database with its default values.
|
2
|
+
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
|
3
|
+
#
|
4
|
+
# Examples:
|
5
|
+
#
|
6
|
+
# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }])
|
7
|
+
# Character.create(name: "Luke", movie: movies.first)
|
File without changes
|
data/dummy_app/log/.keep
ADDED
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
# See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
|
File without changes
|
data/dummy_app/tmp/.keep
ADDED
File without changes
|
File without changes
|
data/lib/trx_ext/object_ext.rb
CHANGED
@@ -22,16 +22,17 @@ module TrxExt
|
|
22
22
|
# # (0.2ms) COMMIT
|
23
23
|
#
|
24
24
|
# @param method [Symbol] a name of the method
|
25
|
+
# @param class_name [String, nil] pass an ActiveRecord model class name to define the appropriate connection of the
|
26
|
+
# transaction
|
25
27
|
# @return [Symbol]
|
26
|
-
def wrap_in_trx(method)
|
28
|
+
def wrap_in_trx(method, class_name = nil)
|
27
29
|
module_to_prepend = Module.new do
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
end
|
30
|
+
define_method(method) do |*args, **kwargs, &blk|
|
31
|
+
context = class_name&.constantize || self
|
32
|
+
context.trx do
|
33
|
+
super(*args, **kwargs, &blk)
|
33
34
|
end
|
34
|
-
|
35
|
+
end
|
35
36
|
end
|
36
37
|
prepend module_to_prepend
|
37
38
|
method
|
@@ -39,6 +40,10 @@ module TrxExt
|
|
39
40
|
|
40
41
|
# A shorthand version of <tt>ActiveRecord::Base.transaction</tt>
|
41
42
|
def trx(...)
|
43
|
+
# If trx method is called over AR model - we should take this into account. Otherwise - call it over
|
44
|
+
# ActiveRecord::Base class. This ensures proper connection picking in sharded environment.
|
45
|
+
return transaction(...) if respond_to?(:transaction)
|
46
|
+
|
42
47
|
ActiveRecord::Base.transaction(...)
|
43
48
|
end
|
44
49
|
end
|
data/lib/trx_ext/retry.rb
CHANGED
@@ -11,7 +11,7 @@ module TrxExt
|
|
11
11
|
module_to_prepend = Module.new do
|
12
12
|
klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
13
13
|
def #{method}(...)
|
14
|
-
::TrxExt::Retry.retry_until_serialized do
|
14
|
+
::TrxExt::Retry.retry_until_serialized(self) do
|
15
15
|
super
|
16
16
|
end
|
17
17
|
end
|
@@ -22,42 +22,32 @@ module TrxExt
|
|
22
22
|
end
|
23
23
|
|
24
24
|
# Retries block execution until serialization errors are no longer raised
|
25
|
-
def retry_until_serialized
|
25
|
+
def retry_until_serialized(connection)
|
26
26
|
retries_count = 0
|
27
27
|
begin
|
28
28
|
yield
|
29
|
-
rescue => error
|
30
|
-
|
31
|
-
|
32
|
-
if error_classification == :record_not_unique
|
33
|
-
retries_count += 1
|
34
|
-
end
|
35
|
-
TrxExt.log("Detected transaction rollback condition. Reason - #{error_classification}. Retrying...")
|
29
|
+
rescue ActiveRecord::SerializationFailure, ActiveRecord::Deadlocked => error
|
30
|
+
if connection.open_transactions == 0
|
31
|
+
TrxExt.log("Detected transaction rollback condition. Reason - #{error.inspect}. Retrying...")
|
36
32
|
retry
|
37
|
-
else
|
38
|
-
raise error
|
39
33
|
end
|
34
|
+
raise
|
35
|
+
rescue ActiveRecord::RecordNotUnique => error
|
36
|
+
raise unless retry_query?(connection, retries_count)
|
37
|
+
|
38
|
+
retries_count += 1
|
39
|
+
TrxExt.log("Detected transaction rollback condition. Reason - #{error.inspect}. Retrying...")
|
40
|
+
retry
|
40
41
|
end
|
41
42
|
end
|
42
43
|
|
43
44
|
private
|
44
45
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
:serialization_error
|
51
|
-
when error.class == ActiveRecord::RecordNotUnique
|
52
|
-
:record_not_unique
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def retry_query?(error, retryies_count)
|
57
|
-
classification = error_classification(error)
|
58
|
-
ActiveRecord::Base.connection.open_transactions == 0 &&
|
59
|
-
(%i(deadlock serialization_error).include?(classification) ||
|
60
|
-
classification == :record_not_unique && retryies_count < TrxExt.config.unique_retries)
|
46
|
+
# @param connection
|
47
|
+
# @param retries_count [Integer]
|
48
|
+
# @return [Boolean]
|
49
|
+
def retry_query?(connection, retries_count)
|
50
|
+
connection.open_transactions == 0 && retries_count < TrxExt.config.unique_retries
|
61
51
|
end
|
62
52
|
end
|
63
53
|
end
|
data/lib/trx_ext/transaction.rb
CHANGED
@@ -7,7 +7,7 @@ module TrxExt
|
|
7
7
|
# for available params
|
8
8
|
def transaction(**kwargs, &blk)
|
9
9
|
pool = nil
|
10
|
-
TrxExt::Retry.retry_until_serialized do
|
10
|
+
TrxExt::Retry.retry_until_serialized(self) do
|
11
11
|
super(**kwargs) do
|
12
12
|
pool = TrxExt::CallbackPool.add(previous: current_callbacks_chain_link)
|
13
13
|
self.current_callbacks_chain_link = pool
|
data/lib/trx_ext/version.rb
CHANGED
data/lib/trx_ext.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'active_record'
|
4
4
|
require_relative "trx_ext/callback_pool"
|
5
5
|
require_relative "trx_ext/object_ext"
|
6
6
|
require_relative "trx_ext/retry"
|
@@ -16,25 +16,15 @@ module TrxExt
|
|
16
16
|
def integrate!
|
17
17
|
# Allow to use #wrap_in_trx and #trx methods everywhere
|
18
18
|
Object.prepend(TrxExt::ObjectExt)
|
19
|
-
|
20
|
-
ActiveSupport.on_load(:
|
21
|
-
|
22
|
-
|
23
|
-
# Patch #transaction
|
24
|
-
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(TrxExt::Transaction)
|
25
|
-
|
26
|
-
# Single SELECT/UPDATE/DELETE queries should also be retried even if they are not a part of explicit transaction
|
27
|
-
TrxExt::Retry.with_retry_until_serialized(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter, :exec_query)
|
28
|
-
TrxExt::Retry.with_retry_until_serialized(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter, :exec_update)
|
29
|
-
TrxExt::Retry.with_retry_until_serialized(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter, :exec_delete)
|
30
|
-
end
|
19
|
+
ActiveSupport.on_load(:active_record_mysql2adapter, &method(:integrate_into_class))
|
20
|
+
ActiveSupport.on_load(:active_record_postgresqladapter, &method(:integrate_into_class))
|
21
|
+
ActiveSupport.on_load(:active_record_sqlite3adapter, &method(:integrate_into_class))
|
22
|
+
ActiveSupport.on_load(:active_record_trilogyadapter, &method(:integrate_into_class))
|
31
23
|
end
|
32
24
|
|
33
25
|
# @return [void]
|
34
26
|
def log(msg)
|
35
|
-
|
36
|
-
|
37
|
-
logger.info(msg)
|
27
|
+
logger&.info(msg)
|
38
28
|
end
|
39
29
|
|
40
30
|
# @return [TrxExt::Config]
|
@@ -45,11 +35,18 @@ module TrxExt
|
|
45
35
|
def configure
|
46
36
|
yield config
|
47
37
|
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def integrate_into_class(klass)
|
42
|
+
klass.prepend TrxExt::Transaction
|
43
|
+
TrxExt::Retry.with_retry_until_serialized(klass, :exec_query)
|
44
|
+
TrxExt::Retry.with_retry_until_serialized(klass, :exec_insert)
|
45
|
+
TrxExt::Retry.with_retry_until_serialized(klass, :exec_delete)
|
46
|
+
TrxExt::Retry.with_retry_until_serialized(klass, :exec_update)
|
47
|
+
TrxExt::Retry.with_retry_until_serialized(klass, :exec_insert_all)
|
48
|
+
end
|
48
49
|
end
|
49
50
|
end
|
50
51
|
|
51
|
-
|
52
|
-
require_relative "trx_ext/railtie"
|
53
|
-
else
|
54
|
-
TrxExt.integrate!
|
55
|
-
end
|
52
|
+
TrxExt.integrate!
|
data/trx_ext.gemspec
CHANGED
@@ -31,11 +31,5 @@ Gem::Specification.new do |spec|
|
|
31
31
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
32
|
spec.require_paths = ["lib"]
|
33
33
|
|
34
|
-
spec.add_dependency 'activerecord', '>=
|
35
|
-
spec.add_dependency 'pg', '~> 1.2'
|
36
|
-
spec.add_development_dependency 'rspec', "~> 3.10"
|
37
|
-
spec.add_development_dependency 'timecop', "~> 0.9.4"
|
38
|
-
spec.add_development_dependency 'factory_bot', "~> 6.2"
|
39
|
-
spec.add_development_dependency 'fivemat', '~> 1.3'
|
40
|
-
spec.add_development_dependency 'rspec-its', '~> 1.3'
|
34
|
+
spec.add_dependency 'activerecord', '>= 7.1'
|
41
35
|
end
|