statement_timeout 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +61 -13
- data/lib/statement_timeout/railtie.rb +14 -0
- data/lib/statement_timeout/version.rb +1 -1
- data/lib/statement_timeout.rb +47 -0
- metadata +73 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c33ca4e42a1dd76ee45b457a32f3e4a24a621a1d45e29d3a57923bba78a6cabd
|
4
|
+
data.tar.gz: e754fc2c5e11d13060f68e32f148163a71ed82a351c6b139337d504604a6ddfe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7360c0d30ee40f815d5fc06bd98c8c62185dc260e3ff8d48e436634786cabadd025cda5d967279cf2136783d6de068a9f2e32d16a87c26f6fe9ba9b605ba4e5c
|
7
|
+
data.tar.gz: 5562e661ae902e2ac6492bfd69231ee9df56234c9e4527d30507b92a21cc16adae3f05aac2d3bc728643885b5908c2e28254d23f17286d11311b34bb16266392
|
data/README.md
CHANGED
@@ -1,31 +1,79 @@
|
|
1
|
-
#
|
1
|
+
# statement_timeout
|
2
2
|
|
3
|
-
|
3
|
+
[![CI](https://github.com/keygen-sh/statement_timeout/actions/workflows/test.yml/badge.svg)](https://github.com/keygen-sh/statement_timeout/actions)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/statement_timeout.svg)](https://badge.fury.io/rb/statement_timeout)
|
4
5
|
|
5
|
-
|
6
|
+
Use `statement_timeout` to wrap an Active Record transaction or query in a
|
7
|
+
local statement timeout.
|
8
|
+
|
9
|
+
This gem was extracted from [Keygen](https://keygen.sh).
|
10
|
+
|
11
|
+
Sponsored by:
|
12
|
+
|
13
|
+
<a href="https://keygen.sh?ref=statement_timeout">
|
14
|
+
<div>
|
15
|
+
<img src="https://keygen.sh/images/logo-pill.png" width="200" alt="Keygen">
|
16
|
+
</div>
|
17
|
+
</a>
|
18
|
+
|
19
|
+
_A fair source software licensing and distribution API._
|
6
20
|
|
7
21
|
## Installation
|
8
22
|
|
9
|
-
|
23
|
+
Add this line to your application's `Gemfile`:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
gem 'statement_timeout'
|
27
|
+
```
|
10
28
|
|
11
|
-
|
29
|
+
And then execute:
|
12
30
|
|
13
|
-
|
31
|
+
```bash
|
32
|
+
$ bundle
|
33
|
+
```
|
14
34
|
|
15
|
-
|
35
|
+
Or install it yourself as:
|
16
36
|
|
17
|
-
|
37
|
+
```bash
|
38
|
+
$ gem install statement_timeout
|
39
|
+
```
|
18
40
|
|
19
41
|
## Usage
|
20
42
|
|
21
|
-
|
43
|
+
```ruby
|
44
|
+
User.statement_timeout 42.minutes do
|
45
|
+
some_expensive_operation
|
46
|
+
end
|
47
|
+
|
48
|
+
User.posts.statement_timeout 10.seconds do
|
49
|
+
some_cheap_operation
|
50
|
+
end
|
22
51
|
|
23
|
-
|
52
|
+
Tag.statement_timeout 6.hours do |conn|
|
53
|
+
conn.execute 'VACUUM ANALYZE tags'
|
54
|
+
end
|
55
|
+
```
|
24
56
|
|
25
|
-
|
57
|
+
## Supported databases
|
26
58
|
|
27
|
-
|
59
|
+
We currently support PostgreSQL. We'd love contributions that add MySQL,
|
60
|
+
MariaDB, and SQLite support, but we probably won't add it ourselves.
|
61
|
+
|
62
|
+
## Supported Rubies
|
63
|
+
|
64
|
+
**`statement_timeout` supports Ruby 3.1 and above.** We encourage you to upgrade
|
65
|
+
if you're on an older version. Ruby 3 provides a lot of great features, like
|
66
|
+
better pattern matching and a new shorthand hash syntax.
|
67
|
+
|
68
|
+
## Is it any good?
|
69
|
+
|
70
|
+
Yes.
|
28
71
|
|
29
72
|
## Contributing
|
30
73
|
|
31
|
-
|
74
|
+
If you have an idea, or have discovered a bug, please open an issue or create a
|
75
|
+
pull request.
|
76
|
+
|
77
|
+
## License
|
78
|
+
|
79
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record/connection_adapters/postgresql_adapter'
|
4
|
+
require 'active_record/relation'
|
5
|
+
|
6
|
+
module StatementTimeout
|
7
|
+
ActiveSupport.on_load :active_record do
|
8
|
+
ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(AbstractAdapterExtension)
|
9
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(PostgreSQLAdapterExtension)
|
10
|
+
ActiveRecord::QueryMethods.prepend(QueryMethodsExtension)
|
11
|
+
ActiveRecord::Querying.prepend(QueryingExtension)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
data/lib/statement_timeout.rb
CHANGED
@@ -1,6 +1,53 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_record'
|
5
|
+
|
3
6
|
require_relative 'statement_timeout/version'
|
7
|
+
require_relative 'statement_timeout/railtie'
|
4
8
|
|
5
9
|
module StatementTimeout
|
10
|
+
module AbstractAdapterExtension
|
11
|
+
def supports_statement_timeout? = false
|
12
|
+
def statement_timeout = raise NotImplementedError
|
13
|
+
def statement_timeout=(timeout)
|
14
|
+
raise NotImplementedError
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module PostgreSQLAdapterExtension
|
19
|
+
def supports_statement_timeout? = true
|
20
|
+
def statement_timeout = @statement_timeout ||= query_value("SHOW statement_timeout")
|
21
|
+
def statement_timeout=(timeout)
|
22
|
+
@statement_timeout = nil
|
23
|
+
|
24
|
+
internal_exec_query("SET statement_timeout = #{quote(timeout)}")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module QueryMethodsExtension
|
29
|
+
def statement_timeout(timeout)
|
30
|
+
timeout = if timeout in ActiveSupport::Duration
|
31
|
+
timeout.in_milliseconds
|
32
|
+
else
|
33
|
+
timeout
|
34
|
+
end
|
35
|
+
|
36
|
+
connection_pool.with_connection do |connection|
|
37
|
+
raise ActiveRecord::AdapterError, "statement_timeout is not supported for the #{connection.class.inspect} adapter" unless
|
38
|
+
connection.supports_statement_timeout?
|
39
|
+
|
40
|
+
statement_timeout_was, connection.statement_timeout = connection.statement_timeout, timeout
|
41
|
+
|
42
|
+
yield connection
|
43
|
+
ensure
|
44
|
+
connection.statement_timeout = statement_timeout_was
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
module QueryingExtension
|
50
|
+
delegate :statement_timeout, to: :all
|
51
|
+
end
|
6
52
|
end
|
53
|
+
|
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: 0.0
|
4
|
+
version: 1.0.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-
|
11
|
+
date: 2024-08-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -38,6 +38,76 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: temporary_tables
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sql_matchers
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.4'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.4'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: mysql2
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pg
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
41
111
|
description: Wrap an Active Record transaction or query in a local statement timeout.
|
42
112
|
email:
|
43
113
|
- oss@keygen.sh
|
@@ -51,6 +121,7 @@ files:
|
|
51
121
|
- README.md
|
52
122
|
- SECURITY.md
|
53
123
|
- lib/statement_timeout.rb
|
124
|
+
- lib/statement_timeout/railtie.rb
|
54
125
|
- lib/statement_timeout/version.rb
|
55
126
|
homepage: https://github.com/keygen-sh/statement_timeout
|
56
127
|
licenses:
|