activerecord-pedantmysql2-adapter 0.1.1 → 1.0.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
  SHA1:
3
- metadata.gz: e590eeb3922b4dec13eb497568619d252decce72
4
- data.tar.gz: 4fe690e1ea1684d749c0c97e0c9a7b6dc5bc1bdb
3
+ metadata.gz: ae59f56c8a3de9383b3b3adf3dac205a6f78f7be
4
+ data.tar.gz: 481ff991e458d0637294de4b8c410e23f4198df4
5
5
  SHA512:
6
- metadata.gz: 6808c691edaf9a7c01ee54180b5fc48a4fd4d390046c0898841a4645f8ff6bf7f0de62f10fd1a0a22b8531db42f3f30205a0f8bed97feb9c18059b50662fbed9
7
- data.tar.gz: e1a521d2c7b817059695f6442839daaf77c66cb7208e06e6bac97ba255fbf59a4b9c04c034c649bde00ec21c7ec49d15b9d397ec2d114ef01cfbf6567a4c186f
6
+ metadata.gz: 25ca80f3663d71ae4fec636c74de5b832828136fff457c01c35de5c21cf5f403734a3759b13936ca84aa52aaf366b60b51c2c30d37c0affa0981127c4a6820a3
7
+ data.tar.gz: e1824ac53a863b29d8f0da7104dfc1157d92c246a822d6ed4b8e72b8d7791ea6f5097c5feef35889df9c9ce07fe5879ae055be16a0dc8cd5fe8b98f97c12f633
data/Gemfile CHANGED
@@ -1,4 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in activerecord-pedantmysql2-adapter.gemspec
3
+ if RUBY_VERSION >= '2.0.0'
4
+ gem 'byebug', group: :development
5
+ end
6
+
4
7
  gemspec
data/README.md CHANGED
@@ -29,18 +29,18 @@ Finally in your `database.yml`:
29
29
 
30
30
  ## Usage
31
31
 
32
- By default it do nothing with the warnings, you have to define the behaviour yourself in an initializer.
32
+ By default it will raise warnings as errors. But you can define any behaviour yourself in an initializer.
33
33
 
34
- You can either raise on all errors:
34
+ You can report them to your exception tracker:
35
35
 
36
36
  ```ruby
37
- PedantMysql2.on_warning = lambda { |warning| raise warning }
37
+ PedantMysql2.on_warning = lambda { |warning| Airbrake.notify(warning) }
38
38
  ```
39
39
 
40
- Or report them to your exception tracker:
40
+ or totally silence them:
41
41
 
42
42
  ```ruby
43
- PedantMysql2.on_warning = lambda { |warning| Airbrake.notify(warning) }
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 be able to have some queries react silently to MySQL warnings you can
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.11'
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 silence_warnings
6
- previous_value = Thread.current[:silence_warnings]
7
- Thread.current[:silence_warnings] = true
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[:silence_warnings] = previous_value
14
+ Thread.current[:mysql_warnings] = previous_warnings
15
+ self.on_warning = previous_callback
11
16
  end
12
17
 
13
- def silence_warnings?
14
- Thread.current[:silence_warnings]
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
- self.on_warning = lambda{ |*| }
41
+
42
+ raise_warnings!
43
+
19
44
  end
@@ -1,3 +1,3 @@
1
1
  module PedantMysql2
2
- VERSION = '0.1.1'
2
+ VERSION = '1.0.0'
3
3
  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('CREATE TABLE IF NOT EXISTS comment (id int)')
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 'just do nothing by default' do
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 allow blocks of queries to be silenced' do
47
- PedantMysql2.on_warning = lambda do |warning|
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.silence_warnings do
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 silence_warnings' do
59
- Thread.current[:silence_warnings] = 'abracadabra'
60
- PedantMysql2.silence_warnings do
61
- expect(Thread.current[:silence_warnings]).to be true
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[:silence_warnings]).to be == 'abracadabra'
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.1.1
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-08-05 00:00:00.000000000 Z
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.11
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.11
40
+ version: 0.3.12
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement