pgtk 0.16.2 → 0.17.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b04cd90c1011ad093997ba7f21cf0136104a7b4fb4de8d842c21794d3cf90ee
4
- data.tar.gz: 2c042b596ccebaaffb4f0597d3211ed53c9b02918e0daf400c0f8706135f0854
3
+ metadata.gz: 8a1ac7d09cdc700a6fa51f239d1f14564f14f9cee56773a933e7e7d310291b1a
4
+ data.tar.gz: 8bcce91182b85e2a592cd3031048b9687b56c7be0587eda199b1c97df944f630
5
5
  SHA512:
6
- metadata.gz: cc9383ef551950dc82b2c825c11cc797965d38a29c2e4f1569b112035ea9ecf019e2437c6c50718fe001ff1bb8da0320da3d6b73fc305ce4ad3ab3d691bcea48
7
- data.tar.gz: e72d1e3321a6326d9e8afa66b819e4ceee68d4dd0d5708c27c73df584b5979ea02708070d0c185c333177f430f6925693a3c700aad1658d4dee2e9ad3bdaa63f
6
+ metadata.gz: '018f2c1cc65ca5b64f27609e4934f47163dcd8c4e582508e995dd332992b18f652de8fef63a9f5d86af969ce306796d2e2db0f490072933ab7a38cd91d02cfa0'
7
+ data.tar.gz: 16c2631220d3cdad2aef2132213a79c922aae79edad0d34b9b7003bd5eec62977cbf9f3febb20a99b17612e77c074e64b7305078903b76e156e27bb90bfbb69c
data/Gemfile.lock CHANGED
@@ -10,6 +10,7 @@ PATH
10
10
  pg (~> 1.1)
11
11
  qbash (> 0)
12
12
  random-port (> 0)
13
+ tago (> 0)
13
14
 
14
15
  GEM
15
16
  remote: https://rubygems.org/
@@ -24,7 +25,7 @@ GEM
24
25
  elapsed (0.0.1)
25
26
  loog (> 0)
26
27
  tago (> 0)
27
- joined (0.1.0)
28
+ joined (0.2.0)
28
29
  json (2.12.0)
29
30
  language_server-protocol (3.17.0.5)
30
31
  lint_roller (1.1.0)
@@ -3,6 +3,7 @@
3
3
  # SPDX-FileCopyrightText: Copyright (c) 2019-2025 Yegor Bugayenko
4
4
  # SPDX-License-Identifier: MIT
5
5
 
6
+ require 'tago'
6
7
  require 'timeout'
7
8
  require_relative '../pgtk'
8
9
 
@@ -51,13 +52,18 @@ require_relative '../pgtk'
51
52
  # Copyright:: Copyright (c) 2019-2025 Yegor Bugayenko
52
53
  # License:: MIT
53
54
  class Pgtk::Impatient
55
+ # If timed out
56
+ class TooSlow < StandardError; end
57
+
54
58
  # Constructor.
55
59
  #
56
60
  # @param [Pgtk::Pool] pool The pool to decorate
57
61
  # @param [Integer] timeout Timeout in seconds for each SQL query
58
- def initialize(pool, timeout = 1)
62
+ # @param [Array<Regex>] off List of regex to exclude queries from checking
63
+ def initialize(pool, timeout, *off)
59
64
  @pool = pool
60
65
  @timeout = timeout
66
+ @off = off
61
67
  end
62
68
 
63
69
  # Get the version of PostgreSQL server.
@@ -74,8 +80,20 @@ class Pgtk::Impatient
74
80
  # @return [Array] Result rows
75
81
  # @raise [Timeout::Error] If the query takes too long
76
82
  def exec(sql, *args)
77
- Timeout.timeout(@timeout) do
78
- @pool.exec(sql, *args)
83
+ return @pool.exec(sql, *args) if @off.any? { |re| re.match?(sql) }
84
+ start = Time.now
85
+ begin
86
+ Timeout.timeout(@timeout) do
87
+ @pool.exec(sql, *args)
88
+ end
89
+ rescue Timeout::Error
90
+ raise TooSlow, [
91
+ 'SQL query',
92
+ ("with #{args.count} arguments" unless args.empty?),
93
+ 'stopped after',
94
+ start.ago,
95
+ 'of waiting'
96
+ ].compact.join(' ')
79
97
  end
80
98
  end
81
99
 
data/lib/pgtk/pool.rb CHANGED
@@ -5,6 +5,7 @@
5
5
 
6
6
  require 'pg'
7
7
  require 'loog'
8
+ require 'tago'
8
9
  require_relative '../pgtk'
9
10
  require_relative 'wire'
10
11
 
@@ -203,9 +204,9 @@ class Pgtk::Pool
203
204
  end
204
205
  lag = Time.now - start
205
206
  if lag < 1
206
- @log.debug("#{sql}: #{(lag * 1000).round}ms / #{@conn.object_id}")
207
+ @log.debug("#{sql}: #{start.ago} / #{@conn.object_id}")
207
208
  else
208
- @log.info("#{sql}: #{format('%.02f', lag)}s")
209
+ @log.info("#{sql}: #{start.ago}")
209
210
  end
210
211
  out
211
212
  end
data/lib/pgtk/version.rb CHANGED
@@ -11,5 +11,5 @@ require_relative '../pgtk'
11
11
  # License:: MIT
12
12
  module Pgtk
13
13
  # Current version of the library.
14
- VERSION = '0.16.2'
14
+ VERSION = '0.17.0'
15
15
  end
data/pgtk.gemspec CHANGED
@@ -34,5 +34,6 @@ Gem::Specification.new do |s|
34
34
  s.add_dependency 'pg', '~>1.1'
35
35
  s.add_dependency 'qbash', '>0'
36
36
  s.add_dependency 'random-port', '>0'
37
+ s.add_dependency 'tago', '>0'
37
38
  s.metadata['rubygems_mfa_required'] = 'true'
38
39
  end
@@ -20,14 +20,32 @@ require_relative '../lib/pgtk/impatient'
20
20
  class TestImpatient < Pgtk::Test
21
21
  def test_takes_version
22
22
  fake_pool do |pool|
23
- v = Pgtk::Impatient.new(pool).version
23
+ v = Pgtk::Impatient.new(pool, 1).version
24
24
  refute_nil(v)
25
25
  end
26
26
  end
27
27
 
28
+ def test_interrupts
29
+ fake_pool do |pool|
30
+ assert_raises(Pgtk::Impatient::TooSlow) do
31
+ Pgtk::Impatient.new(pool, 0.01).exec(
32
+ 'SELECT COUNT(*) FROM generate_series(1, 1000000) AS a'
33
+ )
34
+ end
35
+ end
36
+ end
37
+
38
+ def test_skips_by_regex
39
+ fake_pool do |pool|
40
+ Pgtk::Impatient.new(pool, 0.01, /^SELECT.*$/).exec(
41
+ 'SELECT COUNT(*) FROM generate_series(1, 1000000) AS a'
42
+ )
43
+ end
44
+ end
45
+
28
46
  def test_doesnt_interrupt
29
47
  fake_pool do |pool|
30
- id = Pgtk::Impatient.new(pool).exec(
48
+ id = Pgtk::Impatient.new(pool, 1).exec(
31
49
  'INSERT INTO book (title) VALUES ($1) RETURNING id',
32
50
  ['1984']
33
51
  ).first['id'].to_i
@@ -37,7 +55,7 @@ class TestImpatient < Pgtk::Test
37
55
 
38
56
  def test_doesnt_interrupt_in_transaction
39
57
  fake_pool do |pool|
40
- Pgtk::Impatient.new(pool).transaction do |t|
58
+ Pgtk::Impatient.new(pool, 1).transaction do |t|
41
59
  id = t.exec(
42
60
  'INSERT INTO book (title) VALUES ($1) RETURNING id',
43
61
  ['1984']
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgtk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.2
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -121,6 +121,20 @@ dependencies:
121
121
  - - ">"
122
122
  - !ruby/object:Gem::Version
123
123
  version: '0'
124
+ - !ruby/object:Gem::Dependency
125
+ name: tago
126
+ requirement: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">"
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ type: :runtime
132
+ prerelease: false
133
+ version_requirements: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">"
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
124
138
  description: This small Ruby gem helps you integrate PostgreSQL with your Ruby web
125
139
  app, through Liquibase. It also adds a simple connection pool and query processor,
126
140
  to make SQL manipulation simpler.