mysql2-aurora 0.5.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7aba7e92a9390880096853ce979ea85de81a8e2b4e055ff952ac1ee7b070e0f3
4
+ data.tar.gz: 0bac5961903f4723608a43ba16c46a9d51d9be8181c2dc83c46224f01e77b25c
5
+ SHA512:
6
+ metadata.gz: 671cd2029f7e47a9e08c2951e60dc98f1f378ce0f222f9475127e037f01d6453540ca5defc0c999fe2e91ee696492c2006a42b968a2c82758d2615b0d8276dac
7
+ data.tar.gz: 1327aed779d6e4876ff7a32f8d5aa993c3c0213609eac6afe1da7b6b282bc83d832d117d0a52c749625948ef8d80b205ce396989763a9204a22481a296739751
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -0,0 +1,27 @@
1
+ # Temporary files.
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+
10
+ # Temporary directories.
11
+ pkg/
12
+ tmp/
13
+ temp/
14
+ vendor/bundle/
15
+
16
+ # Test result files.
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ coverage/
21
+ .rspec_status
22
+
23
+ # Document files.
24
+ rdoc
25
+ _yardoc
26
+ doc
27
+ lib/bundler/man
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,105 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ Exclude:
4
+ - 'vendor/**/*'
5
+ - 'bin/{console,setup}'
6
+
7
+ Style/BlockDelimiters:
8
+ EnforcedStyle: braces_for_chaining
9
+
10
+ Style/BracesAroundHashParameters:
11
+ EnforcedStyle: context_dependent
12
+
13
+ Style/ClassAndModuleChildren:
14
+ Enabled: false
15
+
16
+ Style/ClassCheck:
17
+ EnforcedStyle: kind_of?
18
+
19
+ Style/ConditionalAssignment:
20
+ Enabled: false
21
+
22
+ Style/EmptyCaseCondition:
23
+ Enabled: false
24
+
25
+ Style/FrozenStringLiteralComment:
26
+ Enabled: false
27
+
28
+ Style/GuardClause:
29
+ Enabled: false
30
+
31
+ Style/MultilineBlockChain:
32
+ Enabled: false
33
+
34
+ Style/NumericLiterals:
35
+ Enabled: false
36
+
37
+ Style/PerlBackrefs:
38
+ Enabled: false
39
+
40
+ Style/RedundantSelf:
41
+ Enabled: false
42
+
43
+ Style/RegexpLiteral:
44
+ AllowInnerSlashes: true
45
+
46
+ Layout/AlignParameters:
47
+ EnforcedStyle: with_fixed_indentation
48
+
49
+ Layout/AlignHash:
50
+ EnforcedColonStyle: table
51
+ EnforcedHashRocketStyle: table
52
+
53
+ Lint/AmbiguousBlockAssociation:
54
+ Exclude:
55
+ - 'spec/**/*'
56
+
57
+ Layout/ExtraSpacing:
58
+ AllowForAlignment: true
59
+
60
+ Layout/IndentArray:
61
+ EnforcedStyle: consistent
62
+
63
+ Layout/IndentHash:
64
+ EnforcedStyle: consistent
65
+
66
+ Layout/MultilineMethodCallBraceLayout:
67
+ Enabled: false
68
+
69
+ Layout/SpaceInsideBlockBraces:
70
+ EnforcedStyleForEmptyBraces: space
71
+
72
+ Lint/HandleExceptions:
73
+ Exclude:
74
+ - 'spec/**/*'
75
+
76
+ Metrics/AbcSize:
77
+ Max: 25
78
+
79
+ Metrics/BlockLength:
80
+ Exclude:
81
+ - 'spec/**/*'
82
+ ExcludedMethods:
83
+ - 'Gem::Specification.new'
84
+
85
+ Metrics/ClassLength:
86
+ Max: 100
87
+
88
+ Metrics/CyclomaticComplexity:
89
+ Max: 10
90
+
91
+ Metrics/LineLength:
92
+ Max: 100
93
+ IgnoredPatterns:
94
+ - "'.+'" # Text
95
+ - '".+"' # Text
96
+ - '#.+' # Comment
97
+ - '/.+/' # Regex
98
+ - '([A-Z][A-Za-z\d]+::){2,}[A-Z]' # Long module name
99
+ - '([A-Z]+_){4,}[A-Z]' # Long constant name
100
+
101
+ Metrics/MethodLength:
102
+ Max: 25
103
+
104
+ Performance/TimesMap:
105
+ Enabled: false
@@ -0,0 +1,10 @@
1
+ sudo: false
2
+ language: ruby
3
+ script: ./bin/test
4
+ rvm:
5
+ - 2.5.0
6
+ - 2.6.0
7
+ env:
8
+ - TEST_DB_HOST='127.0.0.1' TEST_DB_USER='root' TEST_DB_PASS=''
9
+ services:
10
+ - mysql
@@ -0,0 +1,17 @@
1
+ FROM ruby:2.6.0-alpine3.8
2
+
3
+ # Create application directory.
4
+ RUN mkdir /app
5
+ WORKDIR /app
6
+
7
+ # Install package
8
+ RUN apk upgrade && apk add --update build-base git linux-headers libxml2-dev libxslt-dev mariadb-dev ruby-dev tzdata yaml-dev zlib-dev
9
+
10
+ # Deploy application
11
+ ADD . /app
12
+
13
+ # Install gem
14
+ RUN bundle install
15
+
16
+ # Run test
17
+ CMD ["/app/bin/test"]
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mysql2-aurora.gemspec
6
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2019 alfa-jpn
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.
@@ -0,0 +1,67 @@
1
+ # Mysql2::Aurora
2
+ mysql2 plugin supporting aurora failover.
3
+
4
+ [![Build Status](https://travis-ci.org/alfa-jpn/mysql2-aurora.svg?branch=master)](https://travis-ci.org/alfa-jpn/mysql2-aurora)
5
+ [![Coverage Status](https://coveralls.io/repos/github/alfa-jpn/mysql2-aurora/badge.svg?branch=master)](https://coveralls.io/github/alfa-jpn/mysql2-aurora?branch=master)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/57d8ee7b6f30d413314b/maintainability)](https://codeclimate.com/github/alfa-jpn/mysql2-aurora/maintainability)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'mysql2-aurora'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install mysql2-aurora
23
+
24
+ ## Usage
25
+ This gem extends Mysql2::Client. You can use `aurora_max_retry` option.
26
+
27
+ ```ruby
28
+ Mysql2::Client.new(
29
+ host: 'localhost',
30
+ username: 'root',
31
+ password: 'change_me',
32
+ reconnect: true,
33
+ aurora_max_retry: 5
34
+ )
35
+ ```
36
+
37
+ with Rails, in `database.yml`
38
+
39
+ ```yml
40
+ development:
41
+ adapter: mysql2
42
+ host: localhost
43
+ username: root
44
+ password: change_me
45
+ reconnect: true
46
+ aurora_max_retry: 5
47
+ ```
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alfa-jpn/mysql2-aurora.
52
+
53
+ 1. Fork it
54
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
55
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
56
+ 4. Push to the branch (`git push origin my-new-feature`)
57
+ 5. Create new Pull Request
58
+
59
+ ## Testing
60
+
61
+ ```shell
62
+ # Image build
63
+ docker-compose build
64
+
65
+ # Run tests
66
+ docker-compose run --rm app ./bin/test
67
+ ```
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,3 @@
1
+ #!/bin/sh
2
+ set -e;
3
+ docker-compose run --rm app bundle exec pry
@@ -0,0 +1,3 @@
1
+ #!/bin/sh
2
+ set -e;
3
+ docker-compose run --rm app /bin/sh -c "sleep 5 && /app/bin/test";
@@ -0,0 +1,3 @@
1
+ #!/bin/sh
2
+ set -e;
3
+ bundle exec rspec && bundle exec rubocop && bundle exec yard doc --fail-on-warning
@@ -0,0 +1,17 @@
1
+ version: '3'
2
+ services:
3
+ app:
4
+ build:
5
+ context: .
6
+ environment:
7
+ TEST_DB_HOST: 'mysql'
8
+ TEST_DB_USER: 'root'
9
+ TEST_DB_PASS: 'change_me'
10
+ volumes:
11
+ - .:/app
12
+ depends_on:
13
+ - mysql
14
+ mysql:
15
+ image: mysql:5.7
16
+ environment:
17
+ MYSQL_ROOT_PASSWORD: 'change_me'
@@ -0,0 +1,83 @@
1
+ require 'mysql2/aurora/version'
2
+ require 'mysql2'
3
+
4
+ module Mysql2
5
+ # mysql2 aurora module
6
+ # @note This module patch Mysql2::Client
7
+ module Aurora
8
+ # Implement client patch
9
+ class Client
10
+ RETRY_INTERVAL_SECONDS = 1.5
11
+
12
+ attr_reader :client
13
+
14
+ # Initialize class
15
+ # @note [Override] with reconnect options
16
+ # @param [Hash] opts Options
17
+ # @option opts [Integer] aurora_max_retry Max retry count, when failover. (Default: 5)
18
+ def initialize(opts)
19
+ @opts = Mysql2::Util.key_hash_as_symbols(opts)
20
+ @max_retry = @opts.delete(:aurora_max_retry) || 5
21
+ reconnect!
22
+ end
23
+
24
+ # Execute query with reconnect
25
+ # @note [Override] with reconnect.
26
+ def query(*args)
27
+ try_count = 0
28
+
29
+ begin
30
+ client.query(*args)
31
+ rescue Mysql2::Error => e
32
+ try_count += 1
33
+
34
+ if e.message&.include?('--read-only') && try_count <= @max_retry
35
+ warn "[mysql2-aurora] Database is readonly. Retry after #{RETRY_INTERVAL_SECONDS}seconds"
36
+ sleep RETRY_INTERVAL_SECONDS
37
+ reconnect!
38
+ retry
39
+ else
40
+ raise e
41
+ end
42
+ end
43
+ end
44
+
45
+ # Reconnect to database and Set `@client`
46
+ # @note If client is not connected, Connect to database.
47
+ def reconnect!
48
+ begin
49
+ @client&.close
50
+ rescue StandardError
51
+ nil
52
+ end
53
+ @client = Mysql2::Aurora::ORIGINAL_CLIENT_CLASS.new(@opts)
54
+ end
55
+
56
+ # Delegate method call to client.
57
+ # @param [String] name Method name
58
+ # @param [Array] args Method arguments
59
+ # @param [Proc] block Method block
60
+ def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissingSuper, Style/MissingRespondToMissing
61
+ @client.public_send(name, *args, &block)
62
+ end
63
+
64
+ # Delegate method call to Mysql2::Client.
65
+ # @param [String] name Method name
66
+ # @param [Array] args Method arguments
67
+ # @param [Proc] block Method block
68
+ def self.method_missing(name, *args, &block) # rubocop:disable Style/MethodMissingSuper, Style/MissingRespondToMissing
69
+ Mysql2::Aurora::ORIGINAL_CLIENT_CLASS.public_send(name, *args, &block)
70
+ end
71
+
72
+ # Delegate const reference to class.
73
+ # @param [Symbol] name Const name
74
+ def self.const_missing(name)
75
+ Mysql2::Aurora::ORIGINAL_CLIENT_CLASS.const_get(name)
76
+ end
77
+ end
78
+
79
+ # Swap Mysql2::Client
80
+ ORIGINAL_CLIENT_CLASS = Mysql2.send(:remove_const, :Client)
81
+ Mysql2.const_set(:Client, Mysql2::Aurora::Client)
82
+ end
83
+ end
@@ -0,0 +1,8 @@
1
+ module Mysql2
2
+ module Aurora
3
+ # Major Version: Support `mysql2` major version.
4
+ # Minor Version: Support `mysql2` minor version.
5
+ # Tiny Version: Mysql2::Aurora version.
6
+ VERSION = '0.5.0'.freeze
7
+ end
8
+ end
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'mysql2/aurora/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'mysql2-aurora'
7
+ spec.version = Mysql2::Aurora::VERSION
8
+ spec.authors = ['alfa-jpn']
9
+ spec.email = ['alfa.jpn@gmail.com']
10
+ spec.summary = 'mysql2 plugin supporting aurora failover.'
11
+ spec.description = 'mysql2 plugin supporting aurora failover.'
12
+ spec.homepage = 'https://github.com/alfa-jpn/mysql2-aurora'
13
+ spec.license = 'MIT'
14
+ spec.bindir = 'exe'
15
+ spec.executables = spec.files.grep(/^exe\//) { |f| File.basename(f) }
16
+ spec.require_paths = ['lib']
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(/^(test|spec|features)\//) }
19
+ end
20
+
21
+ spec.required_ruby_version = '>= 2.5.0'
22
+
23
+ spec.add_dependency 'mysql2', '~> 0.5.2'
24
+
25
+ spec.add_development_dependency 'bundler', '>= 1.16'
26
+ spec.add_development_dependency 'coveralls', '~> 0.8'
27
+ spec.add_development_dependency 'pry', '~> 0.12'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'rspec', '~> 3.0'
30
+ spec.add_development_dependency 'rubocop', '~> 0.62'
31
+ spec.add_development_dependency 'simplecov', '~> 0.16'
32
+ spec.add_development_dependency 'yard', '~> 0.9'
33
+ end
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql2-aurora
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - alfa-jpn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mysql2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.62'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.62'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.16'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.16'
125
+ - !ruby/object:Gem::Dependency
126
+ name: yard
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.9'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.9'
139
+ description: mysql2 plugin supporting aurora failover.
140
+ email:
141
+ - alfa.jpn@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".coveralls.yml"
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".rubocop.yml"
150
+ - ".travis.yml"
151
+ - Dockerfile
152
+ - Gemfile
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - bin/docker-pry
157
+ - bin/docker-test
158
+ - bin/test
159
+ - docker-compose.yml
160
+ - lib/mysql2/aurora.rb
161
+ - lib/mysql2/aurora/version.rb
162
+ - mysql2-aurora.gemspec
163
+ homepage: https://github.com/alfa-jpn/mysql2-aurora
164
+ licenses:
165
+ - MIT
166
+ metadata: {}
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: 2.5.0
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubygems_version: 3.0.1
183
+ signing_key:
184
+ specification_version: 4
185
+ summary: mysql2 plugin supporting aurora failover.
186
+ test_files: []