activerecord-transaction_subscriber 0.1.2 → 0.1.3
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1390a414cdcade726071ee89877878015c09e15ebd9d00638c44bf8159fa9b00
|
4
|
+
data.tar.gz: c3d296d57c029879dfa9357f8c4088d29403762749e968eb22dfe8498a300393
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8bec2b5d384ff332c7e2a8fd43b7d8ec08c928ce6c97d3c11b8b54690266842589e872e6aa5b90a5bd9a7cb82aa32b596cd4212b21ee1f347c596f70ff557b7
|
7
|
+
data.tar.gz: 704810d08dc109dfd16fe7498dd237f969520b01028daef09886c16216397c77a6d338669694f96ad7a1770c4a07139edfe78bc1f00d129d09a656a967440495
|
data/CHANGELOG.md
CHANGED
@@ -1,26 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require 'active_record'
|
4
|
+
require 'active_record/log_subscriber'
|
5
|
+
|
6
|
+
require_relative 'transaction_subscriber/version'
|
4
7
|
|
5
8
|
module ActiveRecord
|
6
|
-
class TransactionSubscriber <
|
9
|
+
class TransactionSubscriber < LogSubscriber
|
7
10
|
thread_mattr_accessor :transactions
|
8
11
|
|
9
12
|
def sql(event)
|
10
13
|
payload = event.payload
|
11
14
|
return if payload[:cached]
|
12
15
|
|
13
|
-
|
16
|
+
sql_statement = payload[:sql]
|
14
17
|
|
15
18
|
case payload[:name]
|
16
19
|
when 'TRANSACTION'
|
17
|
-
case
|
20
|
+
case sql_statement
|
18
21
|
when 'BEGIN', 'SAVEPOINT'
|
19
22
|
my_transactions << { start_at: event.time }
|
20
23
|
when 'COMMIT', 'ROLLBACK'
|
21
24
|
tx = my_transactions.pop
|
22
25
|
if tx
|
23
|
-
logging(
|
26
|
+
logging(sql_statement, tx, event.end)
|
24
27
|
end
|
25
28
|
else
|
26
29
|
# Unknown transaction
|
@@ -30,7 +33,7 @@ module ActiveRecord
|
|
30
33
|
else
|
31
34
|
tx = my_transactions.last
|
32
35
|
if tx
|
33
|
-
type = sql_type(
|
36
|
+
type = sql_type(sql_statement)
|
34
37
|
if type
|
35
38
|
tx[type] ||= []
|
36
39
|
tx[type] << event.duration
|
@@ -43,9 +46,9 @@ module ActiveRecord
|
|
43
46
|
self.class.transactions ||= []
|
44
47
|
end
|
45
48
|
|
46
|
-
def logging(
|
49
|
+
def logging(sql_statement, tx, end_at)
|
47
50
|
elapsed_time = ((end_at - tx[:start_at]) * 1000.0).round(1)
|
48
|
-
text = " Transaction #{
|
51
|
+
text = " Transaction #{sql_statement} real time: #{elapsed_time}ms"
|
49
52
|
%i[lock select insert update delete].each do |type|
|
50
53
|
next unless tx[type]
|
51
54
|
count = tx[type].size
|