activerecord-pedantmysql2-adapter 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f3c87e06b817d9fe4abede31aef58d34fbbbbe1a
4
+ data.tar.gz: f1779845a2be79bc7e6f6f53ef4239250124553a
5
+ SHA512:
6
+ metadata.gz: ef20bc6859df95afd6f07f6895ab43ce2c30ed13219aa733d94291054b50dd13d6fd08d73c7f9dfb7a86f8c2a837320638fc506a5b0088e3419497806a141829
7
+ data.tar.gz: 25b911c43b7cfe34659a8653ca30c1df72c7695140d9cf3f51a4842dc29e31d473c62d7c60ac4505863034c0bc4b2b9035a34be64a867d210487be2175a5de6b
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-pedantmysql2-adapter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jean Boussier
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Activerecord::PedantMysql2
2
+
3
+ ActiveRecord adapter for MySQL that report warnings.
4
+
5
+ The main usage is to progressively identify and fix MySQL warnings generated by legacy rails applications, and ultimately enable strict mode.
6
+
7
+ Alternatively it can be used to treat all MySQL warnings as errors.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'activerecord-pedantmysql2-adapter'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Finally in your `database.yml`:
20
+
21
+ adapter: pedant_mysql2
22
+
23
+
24
+ ## Usage
25
+
26
+ By default it do nothing with the warnings, you have to define the behaviour yourself in an initializer.
27
+
28
+ You can either raise on all errors:
29
+
30
+ ```ruby
31
+ PedantMysql2.on_warning = lambda { |warning| raise warning }
32
+ ```
33
+
34
+ Or report them to your exception tracker:
35
+
36
+ ```ruby
37
+ PedantMysql2.on_warning = lambda { |warning| Airbrake.notify(warning) }
38
+ ```
39
+
40
+ Or whatever else behaviour you want (logging).
41
+
42
+ You can easilly whitelist some types of warnings:
43
+
44
+ ```ruby
45
+
46
+ PedantMysql2.on_warning = lambda do |warning|
47
+ raise warning unless WARNINGS_WHITELIST.any? { |pattern| pattern =~ warning.message }
48
+ end
49
+
50
+ ```
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it ( http://github.com/<my-github-username>/activerecord-pedantmysql2-adapter/fork )
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pedant_mysql2/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'activerecord-pedantmysql2-adapter'
8
+ spec.version = PedantMysql2::VERSION
9
+ spec.authors = ['Jean Boussier']
10
+ spec.email = ['jean.boussier@gmail.com']
11
+ spec.summary = %q{ActiveRecord adapter for MySQL that report warnings.}
12
+ spec.description = %q{Gives a hook on MySQL warnings that allow you to either raise or log them.}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'activerecord'
22
+ spec.add_dependency 'mysql2', '>= 0.3.11'
23
+ spec.add_development_dependency 'bundler', '~> 1.5'
24
+ spec.add_development_dependency 'rake'
25
+ end
@@ -0,0 +1,60 @@
1
+ require 'active_record/connection_adapters/mysql2_adapter'
2
+
3
+ module ActiveRecord
4
+ module ConnectionHandling
5
+ def pedant_mysql2_connection(config)
6
+ config = config.symbolize_keys
7
+
8
+ config[:username] = 'root' if config[:username].nil?
9
+
10
+ if Mysql2::Client.const_defined? :FOUND_ROWS
11
+ config[:flags] = Mysql2::Client::FOUND_ROWS
12
+ end
13
+
14
+ client = Mysql2::Client.new(config)
15
+ options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
16
+ ActiveRecord::ConnectionAdapters::PedantMysql2Adapter.new(client, logger, options, config)
17
+ rescue Mysql2::Error => error
18
+ if error.message.include?("Unknown database") && defined?(ActiveRecord::NoDatabaseError)
19
+ raise ActiveRecord::NoDatabaseError.new(error.message, error)
20
+ else
21
+ raise
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ class MysqlWarning < StandardError
28
+
29
+ attr_reader :code, :level, :query
30
+
31
+ def initialize(message, code, level, query)
32
+ super(message)
33
+ @code = code
34
+ @level = level
35
+ @query = query
36
+ end
37
+
38
+ end
39
+
40
+ class ActiveRecord::ConnectionAdapters::PedantMysql2Adapter < ActiveRecord::ConnectionAdapters::Mysql2Adapter
41
+
42
+ def execute(sql, name = nil)
43
+ value = super
44
+ log_warnings(sql)
45
+ value
46
+ end
47
+
48
+ private
49
+
50
+ def log_warnings(sql)
51
+ return unless @connection.warning_count > 0
52
+
53
+ result = @connection.query('SHOW WARNINGS')
54
+ result.each do |level, code, message|
55
+ warning = MysqlWarning.new(message, code, level, sql)
56
+ ::PedantMysql2.on_warning.call(warning)
57
+ end
58
+ end
59
+
60
+ end
@@ -0,0 +1,4 @@
1
+ require 'active_record'
2
+ require 'pedant_mysql2'
3
+ require 'pedant_mysql2/version'
4
+ require 'active_record/connection_adapters/pedant_mysql2_adapter'
@@ -0,0 +1,6 @@
1
+ module PedantMysql2
2
+ class << self
3
+ attr_accessor :on_warning
4
+ end
5
+ self.on_warning = lambda{ |*| }
6
+ end
@@ -0,0 +1,3 @@
1
+ module PedantMysql2
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe PedantMysql2 do
4
+
5
+ let(:connection) { ActiveRecord::Base.connection }
6
+
7
+ before :each do
8
+ @original_callback = PedantMysql2.on_warning
9
+ end
10
+
11
+ after :each do
12
+ PedantMysql2.on_warning = @original_callback
13
+ end
14
+
15
+ it 'just do nothing by default' do
16
+ expect {
17
+ connection.execute('SELECT 1 + "foo"')
18
+ }.to_not raise_error
19
+ end
20
+
21
+ it 'do not change the returned value' do
22
+ result = connection.execute('SELECT 1 + "foo"')
23
+ expect(result.to_a).to be == [[1.0]]
24
+ end
25
+
26
+ it 'can easily be raised' do
27
+ PedantMysql2.on_warning = lambda { |warning| raise warning }
28
+ expect {
29
+ connection.execute('SELECT 1 + "foo"')
30
+ }.to raise_error(MysqlWarning)
31
+ end
32
+
33
+ describe MysqlWarning do
34
+
35
+ subject do
36
+ begin
37
+ PedantMysql2.on_warning = lambda { |warning| raise warning }
38
+ connection.execute('SELECT 1 + "foo"')
39
+ rescue MysqlWarning => exception
40
+ exception
41
+ end
42
+ end
43
+
44
+ its(:message) { should be == "Truncated incorrect DOUBLE value: 'foo'" }
45
+
46
+ its(:code) { should be == 1292 }
47
+
48
+ its(:level) { should be == 'Warning' }
49
+
50
+ its(:query) { should be == 'SELECT 1 + "foo"' }
51
+ end
52
+
53
+ end
@@ -0,0 +1,12 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'activerecord-pedantmysql2-adapter'
4
+
5
+ Dir[File.expand_path(File.join(File.dirname(__FILE__), 'support', '**', '*.rb'))].each { |f| require f }
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ config.order = 'random'
12
+ end
@@ -0,0 +1,12 @@
1
+ ActiveRecord::Base.configurations = {
2
+ 'test' => {
3
+ adapter: 'pedant_mysql2',
4
+ database: 'pedant_mysql2_test',
5
+ username: nil,
6
+ port: '13306',
7
+ host: '127.0.0.1',
8
+ encoding: 'utf8',
9
+ strict: false,
10
+ },
11
+ }
12
+ ActiveRecord::Base.establish_connection(:test)
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-pedantmysql2-adapter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jean Boussier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mysql2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.11
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.11
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Gives a hook on MySQL warnings that allow you to either raise or log
70
+ them.
71
+ email:
72
+ - jean.boussier@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - activerecord-pedantmysql2-adapter.gemspec
84
+ - lib/active_record/connection_adapters/pedant_mysql2_adapter.rb
85
+ - lib/activerecord-pedantmysql2-adapter.rb
86
+ - lib/pedant_mysql2.rb
87
+ - lib/pedant_mysql2/version.rb
88
+ - spec/adapter_spec.rb
89
+ - spec/spec_helper.rb
90
+ - spec/support/database.rb
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: ActiveRecord adapter for MySQL that report warnings.
115
+ test_files:
116
+ - spec/adapter_spec.rb
117
+ - spec/spec_helper.rb
118
+ - spec/support/database.rb