activerecord-pedantmysql2-adapter 0.1.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/Gemfile +4 -1
- data/README.md +8 -23
- data/activerecord-pedantmysql2-adapter.gemspec +1 -1
- data/lib/active_record/connection_adapters/pedant_mysql2_adapter.rb +1 -1
- data/lib/pedant_mysql2.rb +32 -7
- data/lib/pedant_mysql2/version.rb +1 -1
- data/spec/adapter_spec.rb +26 -13
- data/spec/spec_helper.rb +0 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae59f56c8a3de9383b3b3adf3dac205a6f78f7be
|
4
|
+
data.tar.gz: 481ff991e458d0637294de4b8c410e23f4198df4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25ca80f3663d71ae4fec636c74de5b832828136fff457c01c35de5c21cf5f403734a3759b13936ca84aa52aaf366b60b51c2c30d37c0affa0981127c4a6820a3
|
7
|
+
data.tar.gz: e1824ac53a863b29d8f0da7104dfc1157d92c246a822d6ed4b8e72b8d7791ea6f5097c5feef35889df9c9ce07fe5879ae055be16a0dc8cd5fe8b98f97c12f633
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -29,18 +29,18 @@ Finally in your `database.yml`:
|
|
29
29
|
|
30
30
|
## Usage
|
31
31
|
|
32
|
-
By default it
|
32
|
+
By default it will raise warnings as errors. But you can define any behaviour yourself in an initializer.
|
33
33
|
|
34
|
-
You can
|
34
|
+
You can report them to your exception tracker:
|
35
35
|
|
36
36
|
```ruby
|
37
|
-
PedantMysql2.on_warning = lambda { |warning|
|
37
|
+
PedantMysql2.on_warning = lambda { |warning| Airbrake.notify(warning) }
|
38
38
|
```
|
39
39
|
|
40
|
-
|
40
|
+
or totally silence them:
|
41
41
|
|
42
42
|
```ruby
|
43
|
-
PedantMysql2.on_warning = lambda {
|
43
|
+
PedantMysql2.on_warning = lambda { |*| }
|
44
44
|
```
|
45
45
|
|
46
46
|
Or whatever else behaviour you want (logging).
|
@@ -48,28 +48,13 @@ Or whatever else behaviour you want (logging).
|
|
48
48
|
You can easilly whitelist some types of warnings:
|
49
49
|
|
50
50
|
```ruby
|
51
|
-
|
52
|
-
PedantMysql2.on_warning = lambda do |warning|
|
53
|
-
raise warning unless WARNINGS_WHITELIST.any? { |pattern| pattern =~ warning.message }
|
54
|
-
end
|
55
|
-
|
51
|
+
PedantMysql2.ignore(/Some warning I don't care about/i)
|
56
52
|
```
|
57
53
|
|
58
|
-
If you want to
|
59
|
-
do the following:
|
54
|
+
If you want to silence warnings for a limited scope, you can capture the warnings:
|
60
55
|
|
61
56
|
```ruby
|
62
|
-
|
63
|
-
PedantMysql2.on_warning = lambda do |warning|
|
64
|
-
if PedantMysql2.silence_warnings?
|
65
|
-
# handle the warning a certain way or return
|
66
|
-
else
|
67
|
-
# simply pass the warning to the caller
|
68
|
-
raise warning
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
PedantMysql2.silence_warnings do
|
57
|
+
warnings = PedantMysql2.capture_warnings do
|
73
58
|
# perform query that may raise an error you want to stifle
|
74
59
|
end
|
75
60
|
```
|
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
21
|
spec.add_dependency 'activerecord'
|
22
|
-
spec.add_dependency 'mysql2', '>= 0.3.
|
22
|
+
spec.add_dependency 'mysql2', '>= 0.3.12'
|
23
23
|
spec.add_development_dependency 'bundler', '~> 1.5'
|
24
24
|
spec.add_development_dependency 'rake'
|
25
25
|
spec.add_development_dependency 'rspec', '>= 3.0'
|
@@ -64,7 +64,7 @@ class ActiveRecord::ConnectionAdapters::PedantMysql2Adapter < ActiveRecord::Conn
|
|
64
64
|
result = @connection.query('SHOW WARNINGS')
|
65
65
|
result.each do |level, code, message|
|
66
66
|
warning = MysqlWarning.new(message, code, level, sql)
|
67
|
-
::PedantMysql2.on_warning.call(warning)
|
67
|
+
::PedantMysql2.on_warning.call(warning) unless PedantMysql2.ignored?(warning)
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
data/lib/pedant_mysql2.rb
CHANGED
@@ -2,18 +2,43 @@ module PedantMysql2
|
|
2
2
|
class << self
|
3
3
|
attr_accessor :on_warning
|
4
4
|
|
5
|
-
def
|
6
|
-
|
7
|
-
Thread.current[:
|
5
|
+
def capture_warnings
|
6
|
+
previous_callback = on_warning
|
7
|
+
previous_warnings = Thread.current[:mysql_warnings]
|
8
|
+
Thread.current[:mysql_warnings] = []
|
9
|
+
self.on_warning = lambda { |warning| Thread.current[:mysql_warnings] << warning }
|
8
10
|
yield
|
11
|
+
warnings = Thread.current[:mysql_warnings]
|
12
|
+
warnings
|
9
13
|
ensure
|
10
|
-
Thread.current[:
|
14
|
+
Thread.current[:mysql_warnings] = previous_warnings
|
15
|
+
self.on_warning = previous_callback
|
11
16
|
end
|
12
17
|
|
13
|
-
def
|
14
|
-
|
18
|
+
def raise_warnings!
|
19
|
+
self.on_warning = lambda{ |warning| raise warning }
|
20
|
+
end
|
21
|
+
|
22
|
+
def silence_warnings!
|
23
|
+
self.on_warning = nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def ignore(*matchers)
|
27
|
+
self.whitelist.concat(matchers.flatten)
|
28
|
+
end
|
29
|
+
|
30
|
+
def ignored?(warning)
|
31
|
+
on_warning.nil? || whitelist.any? { |matcher| matcher =~ warning.message }
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
|
36
|
+
def whitelist
|
37
|
+
@whitelist ||= []
|
15
38
|
end
|
16
39
|
|
17
40
|
end
|
18
|
-
|
41
|
+
|
42
|
+
raise_warnings!
|
43
|
+
|
19
44
|
end
|
data/spec/adapter_spec.rb
CHANGED
@@ -5,8 +5,12 @@ describe PedantMysql2 do
|
|
5
5
|
let(:connection) { ActiveRecord::Base.connection }
|
6
6
|
|
7
7
|
before :each do
|
8
|
+
PedantMysql2.raise_warnings!
|
9
|
+
PedantMysql2.instance_variable_set(:@whitelist, nil)
|
8
10
|
connection.execute('SET SESSION binlog_format = "STATEMENT"')
|
9
|
-
connection.execute('
|
11
|
+
if connection.execute('SHOW TABLES LIKE "comment"').size == 0
|
12
|
+
connection.execute('CREATE TABLE comment (id int)')
|
13
|
+
end
|
10
14
|
connection.execute('TRUNCATE TABLE comment')
|
11
15
|
@original_callback = PedantMysql2.on_warning
|
12
16
|
end
|
@@ -15,13 +19,21 @@ describe PedantMysql2 do
|
|
15
19
|
PedantMysql2.on_warning = @original_callback
|
16
20
|
end
|
17
21
|
|
18
|
-
it '
|
22
|
+
it 'raises warnings by default' do
|
23
|
+
expect {
|
24
|
+
connection.execute('SELECT 1 + "foo"')
|
25
|
+
}.to raise_error(MysqlWarning, "Truncated incorrect DOUBLE value: 'foo'")
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'can have a whitelist of warnings' do
|
29
|
+
PedantMysql2.ignore(/Truncated incorrect DOUBLE value/i)
|
19
30
|
expect {
|
20
31
|
connection.execute('SELECT 1 + "foo"')
|
21
32
|
}.to_not raise_error
|
22
33
|
end
|
23
34
|
|
24
35
|
it 'do not change the returned value' do
|
36
|
+
PedantMysql2.silence_warnings!
|
25
37
|
result = connection.execute('SELECT 1 + "foo"')
|
26
38
|
expect(result.to_a).to be == [[1.0]]
|
27
39
|
end
|
@@ -43,25 +55,26 @@ describe PedantMysql2 do
|
|
43
55
|
}.to raise_error(MysqlWarning)
|
44
56
|
end
|
45
57
|
|
46
|
-
it 'can
|
47
|
-
|
48
|
-
raise warning unless PedantMysql2.silence_warnings?
|
49
|
-
end
|
58
|
+
it 'can capture the warnings generated in a block' do
|
59
|
+
warnings = nil
|
50
60
|
expect {
|
51
|
-
PedantMysql2.
|
52
|
-
expect(PedantMysql2.silence_warnings?).to be true
|
61
|
+
warnings = PedantMysql2.capture_warnings do
|
53
62
|
connection.execute('SELECT 1 + "foo"')
|
54
63
|
end
|
55
64
|
}.to_not raise_error
|
65
|
+
|
66
|
+
expect(warnings.size).to be == 1
|
67
|
+
expect(warnings.first).to be_a MysqlWarning
|
68
|
+
expect(warnings.first.message).to be == "Truncated incorrect DOUBLE value: 'foo'"
|
56
69
|
end
|
57
70
|
|
58
|
-
it 'restores the old value that was stored in the thread_local
|
59
|
-
Thread.current[:
|
60
|
-
PedantMysql2.
|
61
|
-
expect(Thread.current[:
|
71
|
+
it 'restores the old value that was stored in the thread_local capture_warnings' do
|
72
|
+
Thread.current[:mysql_warnings] = 'abracadabra'
|
73
|
+
PedantMysql2.capture_warnings do
|
74
|
+
expect(Thread.current[:mysql_warnings]).to be_an Array
|
62
75
|
connection.execute('SELECT 1 + "foo"')
|
63
76
|
end
|
64
|
-
expect(Thread.current[:
|
77
|
+
expect(Thread.current[:mysql_warnings]).to be == 'abracadabra'
|
65
78
|
end
|
66
79
|
|
67
80
|
describe MysqlWarning do
|
data/spec/spec_helper.rb
CHANGED
@@ -15,7 +15,6 @@ require 'activerecord-pedantmysql2-adapter'
|
|
15
15
|
Dir[File.expand_path(File.join(File.dirname(__FILE__), 'support', '**', '*.rb'))].each { |f| require f }
|
16
16
|
|
17
17
|
RSpec.configure do |config|
|
18
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
19
18
|
config.run_all_when_everything_filtered = true
|
20
19
|
config.filter_run :focus
|
21
20
|
config.order = 'random'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-pedantmysql2-adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean Boussier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.3.
|
33
|
+
version: 0.3.12
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.3.
|
40
|
+
version: 0.3.12
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|