Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Rails7): Update for Rails 7 and MySQL 8 #2

Merged
merged 15 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/specs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Specs
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
run-rspec:
runs-on: ubuntu-22.04
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup Ruby and install gems
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.1
bundler-cache: true
- name: Install gems with Appraisal
run: bundle exec appraisal install
- name: Run RSpec
run: bundle exec appraisal rspec
27 changes: 5 additions & 22 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
appraise "activerecord-4.2" do
gem "activerecord", "~> 4.2.0"
gem "mysql2", "< 0.5"
appraise "activerecord-7.0" do
gem "activerecord", "~> 7.0.0"
end

appraise "activerecord-5.0" do
gem "activerecord", "~> 5.0.0"
end

appraise "activerecord-5.1" do
gem "activerecord", "~> 5.1.0"
end

appraise "activerecord-5.2" do
gem "activerecord", "~> 5.2.0"
end

appraise "activerecord-6.0" do
gem "activerecord", "~> 6.0.2"
end

appraise "activerecord-master" do
gem "activerecord", git: "https://github.com/rails/rails.git"
end
#appraise "activerecord-master" do
# gem "activerecord", git: "https://github.com/rails/rails.git"
#end
51 changes: 51 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
## 1.0.0 (unreleased)

- Feature: Add support for Rails 7
- Change: Drop support for older versions
- Chore: Test against MySQL 8
- Fix: Handle ActiveRecord::ConnectionNotEstablished errors too

## 0.5.0 (Jan 29, 2020)

- Change error message for Rails 6.0
- Load database config from db_config instead
- Add Active Record 6.0 and master to build matrix

## 0.4.2 (Aug 15, 2018)

- Add activerecord-5.2 test
- Add error messages accessor
- Fix mysql2 dependency
- Added "Connection was killed" as an error string to reconnect on.

## 0.4.1 (Aug 8, 2016)

- Support AR 5.0 (RP#5 @ssig33)
- Fix test (use docker-compose)

## 0.4.0 (Mar 22, 2016)

- Remove `retryable_transaction`
- Disable support AR 3.x 4.0

## 0.3.3 (Jan 18, 2014)

- use BigDecimal for sleep
- add handling error ('The MySQL server is running with the --read-only...')

## 0.3.1 (Jan 9, 2014)

- Retry mode is added

## 0.3.0 (Jan 9, 2014)

- Retry is disabled by default
- Read-only mode is added

## 0.2.0 (Jan 4, 2014)

- Retry transaction is supported

## 0.1.0 (Oct 11, 2013)

- activerecord-mysql-reconnect is released
31 changes: 0 additions & 31 deletions ChangeLog

This file was deleted.

5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# activerecord-mysql-reconnect

It is the library to reconnect automatically when ActiveRecord is disconnected from MySQL.
Reconnect automatically when ActiveRecord is disconnected from MySQL. Supports Rails 7+ and MySQL 8.

[![Gem Version](https://badge.fury.io/rb/activerecord-mysql-reconnect.svg)](http://badge.fury.io/rb/activerecord-mysql-reconnect)
[![Build Status](https://travis-ci.org/winebarrel/activerecord-mysql-reconnect.svg?branch=master)](https://travis-ci.org/winebarrel/activerecord-mysql-reconnect)
[![Specs](https://github.com/planningcenter/activerecord-mysql-reconnect/actions/workflows/specs.yml/badge.svg)](https://github.com/planningcenter/activerecord-mysql-reconnect/actions/workflows/specs.yml)

## Installation

Expand Down
3 changes: 1 addition & 2 deletions activerecord-mysql-reconnect.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

# '~> 4.2.6'
spec.add_dependency 'activerecord'
spec.add_dependency 'activerecord', '>= 7'
spec.add_dependency 'mysql2'
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
Expand Down
13 changes: 7 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mysql_for_ar_mysql_reconn:
image: "mysql:5.6"
ports:
- "14407:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
services:
mysql:
image: "mysql:8"
ports:
- "14407:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
8 changes: 0 additions & 8 deletions gemfiles/activerecord_4.2.gemfile

This file was deleted.

7 changes: 0 additions & 7 deletions gemfiles/activerecord_5.0.gemfile

This file was deleted.

7 changes: 0 additions & 7 deletions gemfiles/activerecord_5.1.gemfile

This file was deleted.

7 changes: 0 additions & 7 deletions gemfiles/activerecord_6.0.gemfile

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

source "https://rubygems.org"

gem "activerecord", "~> 5.2.0"
gem "activerecord", "~> 7.0.0"

gemspec path: "../"
7 changes: 0 additions & 7 deletions gemfiles/activerecord_master.gemfile

This file was deleted.

2 changes: 2 additions & 0 deletions lib/activerecord/mysql/reconnect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Activerecord::Mysql::Reconnect

HANDLE_ERROR = [
ActiveRecord::ConnectionNotEstablished,
ActiveRecord::DatabaseConnectionError,
ActiveRecord::StatementInvalid,
Mysql2::Error,
]
Expand All @@ -47,6 +48,7 @@ module Activerecord::Mysql::Reconnect
lost_connection: "Lost connection to MySQL server at 'reading initial communication packet'",
not_connected: "MySQL client is not connected",
killed: 'Connection was killed',
issue_connecting: 'There is an issue connecting with your hostname',
}

READ_SQL_REGEXP = /\A\s*(?:SELECT|SHOW|SET)\b/i
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Activerecord::Mysql::Reconnect::ExecuteWithReconnect
def execute(sql, name = nil)
def raw_execute(sql, name, async: false)
retryable(sql, name) do |sql_names|
retval = nil

sql_names.each do |s, n|
retval = super(s, n)
retval = super(s, n, async:)
end

retval
Expand Down
2 changes: 1 addition & 1 deletion lib/activerecord/mysql/reconnect/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Activerecord
module Mysql
module Reconnect
VERSION = '0.5.0'
VERSION = '1.0.0'
end
end
end
67 changes: 24 additions & 43 deletions spec/activerecord-mysql-reconnect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@
end
end

context 'when count on same thead' do
context 'when count on same thread' do
specify do
expect(Employee.count).to eq 1000
MysqlServer.restart
expect(Employee.count).to eq 1000
end
end

context 'wehn select on other thread' do
context 'when select on other thread' do
specify do
th = thread_start {
expect(Employee.where(:id => 1).pluck('sleep(10) * 0 + 3')).to eq [3]
expect(Employee.where(:id => 1).pluck(Arel.sql('sleep(10) * 0 + 3'))).to eq [3]
}

MysqlServer.restart
Expand Down Expand Up @@ -113,14 +113,8 @@

specify do
th = thread_start {
emp = Employee.create(
:emp_no => 9999,
:birth_date => Time.now,
# wait 10 sec
:first_name => "' + sleep(10) + '",
:last_name => 'Tiger',
:hire_date => Time.now
)
id = ActiveRecord::Base.connection.insert("insert into employees (emp_no, birth_date, hire_date, first_name, last_name) values (sleep(10) + 9998, '2000-01-01', '2023-08-16', 'Daniel', 'Tiger')", returning: 'id')
emp = Employee.find(id)

expect(emp.id).to eq 1001
expect(emp.emp_no).to eq 9999
Expand Down Expand Up @@ -214,6 +208,9 @@

MysqlServer.restart

# record is lost
expect { emp.reload }.to raise_error(ActiveRecord::RecordNotFound)

emp = Employee.create(
:emp_no => 9998,
:birth_date => Time.now,
Expand All @@ -223,7 +220,8 @@
)

# NOTE: Ignore the transaction on :rw mode
expect(emp.id).to eq 1001

expect(emp.id).to eq 1002 # auto_increment still goes up
expect(emp.emp_no).to eq 9998
end

Expand Down Expand Up @@ -451,63 +449,46 @@
"%s (cause: %s, sql: SELECT `employees`.* FROM `employees`, connection: host=127.0.0.1;database=employees;username=root)"
end

let(:mysql_error) do
Mysql2::Error.const_defined?(:ConnectionError) ? Mysql2::Error::ConnectionError : Mysql2::Error
end

before do
allow_any_instance_of(mysql_error).to receive(:message).and_return('Lost connection to MySQL server during query')
allow_any_instance_of(Mysql2::Error::ConnectionError).to receive(:message).and_return('Lost connection to MySQL server during query')
allow_any_instance_of(ActiveRecord::StatementInvalid).to receive(:message).and_return('Lost connection to MySQL server during query')
allow_any_instance_of(ActiveRecord::DatabaseConnectionError).to receive(:message).and_return('Lost connection to MySQL server during query')
end

context "when retry failed " do
specify do
if ActiveRecord::VERSION::MAJOR < 6
expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
"MySQL server has gone away. Trying to reconnect in 0.5 seconds.",
"#{mysql_error}: Lost connection to MySQL server during query: SELECT `employees`.* FROM `employees` [ActiveRecord::StatementInvalid]",
])
else
expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
"MySQL server has gone away. Trying to reconnect in 0.5 seconds.",
"#{mysql_error}: Lost connection to MySQL server during query [ActiveRecord::StatementInvalid]",
])
end

expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
"MySQL server has gone away. Trying to reconnect in 0.5 seconds.",
"Lost connection to MySQL server during query [ActiveRecord::StatementInvalid]",
])

(1.0..4.5).step(0.5).each do |sec|
expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
"MySQL server has gone away. Trying to reconnect in #{sec} seconds.",
"Lost connection to MySQL server during query [#{mysql_error}]",
"Lost connection to MySQL server during query [ActiveRecord::DatabaseConnectionError]",
])
end

expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
"Query retry failed.",
"Lost connection to MySQL server during query [#{mysql_error}]",
"Lost connection to MySQL server during query [ActiveRecord::DatabaseConnectionError]",
])

expect(Employee.all.length).to eq 1000
MysqlServer.stop

expect {
Employee.all.length
}.to raise_error(mysql_error)
}.to raise_error(ActiveRecord::DatabaseConnectionError)
end
end

context "when retry succeeded" do
specify do
if ActiveRecord::VERSION::MAJOR < 6
expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
"MySQL server has gone away. Trying to reconnect in 0.5 seconds.",
"#{mysql_error}: Lost connection to MySQL server during query: SELECT `employees`.* FROM `employees` [ActiveRecord::StatementInvalid]",
])
else
expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
"MySQL server has gone away. Trying to reconnect in 0.5 seconds.",
"#{mysql_error}: Lost connection to MySQL server during query [ActiveRecord::StatementInvalid]",
])
end
expect(ActiveRecord::Base.logger).to receive(:warn).with(warning_template % [
"MySQL server has gone away. Trying to reconnect in 0.5 seconds.",
"Lost connection to MySQL server during query [ActiveRecord::StatementInvalid]",
])

expect(Employee.all.length).to eq 1000
MysqlServer.restart
Expand Down
Loading