statement_timeout 1.0.0.pre.rc.1 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b42943a4a7aaa58ea1d0111e663391f30e8781dee8d220ad8db2ad011b1efda3
4
- data.tar.gz: c585bf0781f46e562b3947c0813c478ba6d14e038d9c0f876ba27dd0ee0fe4be
3
+ metadata.gz: e0a38e0ec0dab300f6ddfbc11f8af5f7ef60d5af3083435e1c8cfa869b779197
4
+ data.tar.gz: 28fc70d816752e5399cc69574c11b6d73200e85fe6c6700ed7d6caa5a75af84f
5
5
  SHA512:
6
- metadata.gz: 66a2345bbb4088bdebd6fe59327c5550b4d114e6edac342d4778641a081184cd5a7e5d0bfec6db4d5e07be71acf9f9abbd727a0b665725b916f41f6798742641
7
- data.tar.gz: 566f9e844756d16d3d85da9a31a35a5c52b52969cf8acf95879dbf04c915e1eb974b7bc1c5d8c4e3fa0902be25885cf24ce67375ac10bb3dd0b373674d45606c
6
+ metadata.gz: b7dee7d2c5dc5ff99d0f8d12616b834cc7ac05e472b3ed1dcddb8854003fd33b2256d439b8fbc9c6a74d4e6fedbfacb892b51987bfc66fe0179f500b951e65de
7
+ data.tar.gz: 46dd7481f5a302ec553a0e01b2f018dedb577d607f312aff48e9c52357986f94bef84edd92ab3ec88f8fca4d1925f4cdeb902195bb214835859eb3d0a2016c3a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
1
  # Changelog
2
2
 
3
- ## [Unreleased]
3
+ ## v1.1.0
4
+
5
+ - Add support for transaction `:mode`, useful in cases where a PgBouncer is in
6
+ sitting front of Postgres and in transaction mode. In this case, the default
7
+ session-based `SET statement_timeout = ...` will not work consistently.
8
+
9
+ ## v1.0.0
10
+
11
+ Initial release.
data/README.md CHANGED
@@ -38,6 +38,17 @@ Or install it yourself as:
38
38
  $ gem install statement_timeout
39
39
  ```
40
40
 
41
+ ## Configuration
42
+
43
+ ```ruby
44
+ StatementTimeout.configure do |config|
45
+ # When using a connection pooler like PgBouncer while in transaction mode, it
46
+ # is required that you change the default mode from :session to :transaction
47
+ # otherwise the statement timeouts may be SET on the right connection.
48
+ config.default_mode = :transaction
49
+ end
50
+ ```
51
+
41
52
  ## Usage
42
53
 
43
54
  ```ruby
@@ -49,16 +60,15 @@ User.posts.statement_timeout 10.seconds do
49
60
  some_cheap_operation
50
61
  end
51
62
 
63
+ Post.statement_timeout 10.minutes do |conn|
64
+ conn.transaction { archive_old_posts }
65
+ end
66
+
52
67
  Tag.statement_timeout 6.hours do |conn|
53
68
  conn.execute 'VACUUM ANALYZE tags'
54
69
  end
55
70
  ```
56
71
 
57
- ## Future
58
-
59
- Right now, the gem only supports RSpec, but we're open to pull requests that
60
- extend the functionality to other testing frameworks.
61
-
62
72
  ## Supported databases
63
73
 
64
74
  We currently support PostgreSQL. We'd love contributions that add MySQL,
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StatementTimeout
4
+ class Configuration
5
+ class_attribute :default_mode
6
+ self.default_mode = :session
7
+ end
8
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StatementTimeout
4
- VERSION = '1.0.0-rc.1'
4
+ VERSION = '1.1.0'
5
5
  end
@@ -3,6 +3,7 @@
3
3
  require 'active_support'
4
4
  require 'active_record'
5
5
 
6
+ require_relative 'statement_timeout/configuration'
6
7
  require_relative 'statement_timeout/version'
7
8
  require_relative 'statement_timeout/railtie'
8
9
 
@@ -10,23 +11,35 @@ module StatementTimeout
10
11
  module AbstractAdapterExtension
11
12
  def supports_statement_timeout? = false
12
13
  def statement_timeout = raise NotImplementedError
14
+
13
15
  def statement_timeout=(timeout)
14
16
  raise NotImplementedError
15
17
  end
18
+
19
+ def local_statement_timeout=(timeout)
20
+ raise NotImplementedError
21
+ end
16
22
  end
17
23
 
18
24
  module PostgreSQLAdapterExtension
19
25
  def supports_statement_timeout? = true
20
26
  def statement_timeout = @statement_timeout ||= query_value("SHOW statement_timeout")
27
+
21
28
  def statement_timeout=(timeout)
22
29
  @statement_timeout = nil
23
30
 
24
31
  internal_exec_query("SET statement_timeout = #{quote(timeout)}")
25
32
  end
33
+
34
+ def local_statement_timeout=(timeout)
35
+ @statement_timeout = nil
36
+
37
+ internal_exec_query("SET LOCAL statement_timeout = #{quote(timeout)}")
38
+ end
26
39
  end
27
40
 
28
41
  module QueryMethodsExtension
29
- def statement_timeout(timeout)
42
+ def statement_timeout(timeout, mode: StatementTimeout.config.default_mode)
30
43
  timeout = if timeout in ActiveSupport::Duration
31
44
  timeout.in_milliseconds
32
45
  else
@@ -37,11 +50,26 @@ module StatementTimeout
37
50
  raise ActiveRecord::AdapterError, "statement_timeout is not supported for the #{connection.class.inspect} adapter" unless
38
51
  connection.supports_statement_timeout?
39
52
 
40
- statement_timeout_was, connection.statement_timeout = connection.statement_timeout, timeout
53
+ case mode
54
+ when :transaction
55
+ connection.transaction do
56
+ statement_timeout_was, connection.local_statement_timeout = connection.statement_timeout, timeout
41
57
 
42
- yield connection
43
- ensure
44
- connection.statement_timeout = statement_timeout_was
58
+ yield connection
59
+ ensure
60
+ connection.local_statement_timeout = statement_timeout_was
61
+ end
62
+ when :session
63
+ begin
64
+ statement_timeout_was, connection.statement_timeout = connection.statement_timeout, timeout
65
+
66
+ yield connection
67
+ ensure
68
+ connection.statement_timeout = statement_timeout_was
69
+ end
70
+ else
71
+ raise ArgumentError, "mode is not supported: #{mode.inspect}"
72
+ end
45
73
  end
46
74
  end
47
75
  end
@@ -49,5 +77,10 @@ module StatementTimeout
49
77
  module QueryingExtension
50
78
  delegate :statement_timeout, to: :all
51
79
  end
80
+
81
+ def self.config = @config ||= Configuration.new
82
+ def self.configure
83
+ yield config
84
+ end
52
85
  end
53
86
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statement_timeout
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre.rc.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zeke Gabrielse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-13 00:00:00.000000000 Z
11
+ date: 2025-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -121,6 +121,7 @@ files:
121
121
  - README.md
122
122
  - SECURITY.md
123
123
  - lib/statement_timeout.rb
124
+ - lib/statement_timeout/configuration.rb
124
125
  - lib/statement_timeout/railtie.rb
125
126
  - lib/statement_timeout/version.rb
126
127
  homepage: https://github.com/keygen-sh/statement_timeout
@@ -138,11 +139,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
138
139
  version: '3.1'
139
140
  required_rubygems_version: !ruby/object:Gem::Requirement
140
141
  requirements:
141
- - - ">"
142
+ - - ">="
142
143
  - !ruby/object:Gem::Version
143
- version: 1.3.1
144
+ version: '0'
144
145
  requirements: []
145
- rubygems_version: 3.4.13
146
+ rubygems_version: 3.5.11
146
147
  signing_key:
147
148
  specification_version: 4
148
149
  summary: Wrap an Active Record transaction or query in a local statement timeout.