activerecord-transaction_subscriber 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e25d9a11d8fad7fdba04fcd94e6513a13886f581543769d30e9c3b2fc7e61d6b
4
+ data.tar.gz: bd6c3674e9434f2ae33e2d92b049a368361b8215244848e6328f50be976f029f
5
+ SHA512:
6
+ metadata.gz: af959006c45665488fd8963c572c8ecbe770515f432db0afea270cabf2f0eae20f2fcd1b07518d8460ef28981c8e7a7ab44899e594cf2462b39b2c96b697c732
7
+ data.tar.gz: e4ce7bdff8c8366d5210dc00fa319b122afbdc9886dfaf87cbda0ca9cea7175ec2ffd52d4ed87a38c9951c385783d0e0de3f14507f6bc22537ef4f3c89d291f7
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-08-22
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "rake", "~> 13.0"
8
+ gem "rspec", "~> 3.0"
9
+ gem "rubocop", "~> 1.21"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Ueda Satoshi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # ActiveRecord::TransactionSubscriber
2
+
3
+ Log the execution time of ActiveRecord transactions.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add activerecord-transaction_subscriber
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install activerecord-transaction_subscriber
14
+
15
+ ## Usage
16
+
17
+ Write the following in `config/initializer/activerecord-transaction_subscriber.rb`
18
+
19
+ ```ruby
20
+ ActiveRecord::TransactionSubscriber.attach_to :active_record
21
+ ```
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gunyoki/activerecord-transaction_subscriber.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ class TransactionSubscriber
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "transaction_subscriber/version"
4
+
5
+ module ActiveRecord
6
+ class TransactionSubscriber < ActiveRecord::LogSubscriber
7
+ thread_mattr_accessor :transactions
8
+
9
+ def sql(event)
10
+ payload = event.payload
11
+ return if payload[:cached]
12
+
13
+ sql = payload[:sql]
14
+
15
+ case payload[:name]
16
+ when 'TRANSACTION'
17
+ case sql
18
+ when 'BEGIN', 'SAVEPOINT'
19
+ my_transactions << { start_at: event.time }
20
+ when 'COMMIT', 'ROLLBACK'
21
+ tx = my_transactions.pop
22
+ if tx
23
+ logging(payload[:name], tx, event.end)
24
+ end
25
+ else
26
+ # Unknown transaction
27
+ end
28
+ when *IGNORE_PAYLOAD_NAMES
29
+ # no-op
30
+ else
31
+ tx = my_transactions.last
32
+ if tx
33
+ type = sql_type(sql)
34
+ if type
35
+ tx[type] ||= []
36
+ tx[type] << event.duration
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ def my_transactions
43
+ self.class.transactions ||= []
44
+ end
45
+
46
+ def logging(name, tx, end_at)
47
+ elapsed_time = ((end_at - tx[:start_at]) * 1000.0).round(1)
48
+ text = " Transaction #{name} real time: #{elapsed_time}ms"
49
+ %i[lock select insert update delete].each do |type|
50
+ next unless tx[type]
51
+ count = tx[type].size
52
+ total = tx[type].sum.round(1)
53
+ text << ", #{type}: #{count} (#{total}ms)"
54
+ end
55
+ info color(text, :magenta, true)
56
+ end
57
+
58
+ def sql_type(sql)
59
+ case sql
60
+ when /select .*for update/mi, /\A\s*lock/mi
61
+ :lock
62
+ when /\A\s*select/i
63
+ :select
64
+ when /\A\s*insert/i
65
+ :insert
66
+ when /\A\s*update/i
67
+ :update
68
+ when /\A\s*delete/i
69
+ :delete
70
+ else
71
+ nil
72
+ end
73
+ end
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-transaction_subscriber
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ueda Satoshi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-09-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ description: The number of queries executed in a transaction, the execution time,
28
+ and the wall-clock time of the transaction are logged at the end of the transaction.
29
+ email:
30
+ - gunyoki@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - CHANGELOG.md
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/activerecord/.transaction_subscriber.rb.swp
43
+ - lib/activerecord/transaction_subscriber.rb
44
+ - lib/activerecord/transaction_subscriber/version.rb
45
+ homepage: https://github.com/gunyoki/activerecord-transaction_subscriber
46
+ licenses:
47
+ - MIT
48
+ metadata:
49
+ homepage_uri: https://github.com/gunyoki/activerecord-transaction_subscriber
50
+ source_code_uri: https://github.com/gunyoki/activerecord-transaction_subscriber
51
+ changelog_uri: https://github.com/gunyoki/activerecord-transaction_subscriber/blob/main/CHANGELOG.md
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.6.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.4.6
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Log the execution time of ActiveRecord transactions.
71
+ test_files: []