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

Support trilogy adapter #103

Draft
wants to merge 23 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ jobs:
- 3.1
- 3.2
- 3.3
adapter:
- trilogy
- mysql2
gemfile:
- gemfiles/rails_6_1.gemfile
- gemfiles/rails_7_0.gemfile
- gemfiles/rails_7_1.gemfile
env:
PERCONA_DB_USER: root
PERCONA_DB_PASSWORD: root
PERCONA_DB_ADAPTER: ${{ matrix.adapter }}
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Metrics/AbcSize:
Metrics/BlockLength:
Max: 20
Exclude:
- 'departure.gemspec'
- 'spec/*'
- 'spec/**/*'

Expand Down
2 changes: 2 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
appraise 'rails-6-1' do
gem 'activerecord-trilogy-adapter', '>= 3.1.2', require: false
gem 'rails', '6.1.7.6'
end

appraise 'rails-7-0' do
gem 'activerecord-trilogy-adapter', '>= 3.1.2', require: false
gem 'rails', '7.0.8'
end

Expand Down
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ PATH
specs:
departure (6.7.0)
activerecord (>= 6.0.0, < 7.2.0, != 7.0.0)
mysql2 (>= 0.4.0, < 0.6.0)
railties (>= 6.0.0, < 7.2.0, != 7.0.0)

GEM
Expand Down Expand Up @@ -168,6 +167,7 @@ GEM
stringio (3.1.0)
thor (1.3.0)
timeout (0.4.1)
trilogy (2.7.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
Expand All @@ -185,12 +185,14 @@ DEPENDENCIES
climate_control (~> 0.0.3)
codeclimate-test-reporter (~> 1.0.3)
departure!
mysql2 (>= 0.4.0, <= 0.5.5)
pry-byebug
rake (>= 10.0)
rspec (~> 3.4, >= 3.4.0)
rspec-its (~> 1.2)
rubocop (~> 1.60.2)
rubocop-performance (~> 1.20.2)
trilogy (>= 2.7.0)

BUNDLED WITH
2.4.22
1 change: 1 addition & 0 deletions config.yml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ password: <%= ENV['PERCONA_DB_PASSWORD'] || '' %>
database: <%= ENV['PERCONA_DB_NAME'] || 'departure_test' %>
hostname: <%= ENV['PERCONA_DB_HOST'] || 'localhost' %>
socket: <%= ENV['PERCONA_DB_SOCKET'] || '' %>
original_adapter: <%= ENV['PERCONA_DB_ADAPTER'] || 'mysql2' %>
9 changes: 5 additions & 4 deletions departure.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ Gem::Specification.new do |spec|

spec.add_runtime_dependency 'railties', '>= 6.0.0', '!= 7.0.0', '< 7.2.0'
spec.add_runtime_dependency 'activerecord', '>= 6.0.0', '!= 7.0.0', '< 7.2.0'
spec.add_runtime_dependency 'mysql2', '>= 0.4.0', '< 0.6.0'

spec.add_development_dependency 'appraisal', '~> 2.4.1'
spec.add_development_dependency 'climate_control', '~> 0.0.3'
spec.add_development_dependency 'mysql2', '>= 0.4.0', '<= 0.5.5'
spec.add_development_dependency 'pry-byebug'
spec.add_development_dependency 'rake', '>= 10.0'
spec.add_development_dependency 'rspec', '~> 3.4', '>= 3.4.0'
spec.add_development_dependency 'rspec-its', '~> 1.2'
spec.add_development_dependency 'pry-byebug'
spec.add_development_dependency 'climate_control', '~> 0.0.3'
spec.add_development_dependency 'rspec', '~> 3.4', '>= 3.4.0'
spec.add_development_dependency 'trilogy', '>= 2.7.0'
end
1 change: 1 addition & 0 deletions gemfiles/rails_6_1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

source 'https://rubygems.org'

gem 'activerecord-trilogy-adapter', '>= 3.1.2', require: false
gem 'codeclimate-test-reporter', '~> 1.0.3', group: :test, require: nil
gem 'rails', '6.1.7.6'
gem 'rubocop', '~> 1.60.2', require: false
Expand Down
8 changes: 7 additions & 1 deletion gemfiles/rails_6_1.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ PATH
specs:
departure (6.7.0)
activerecord (>= 6.0.0, < 7.2.0, != 7.0.0)
mysql2 (>= 0.4.0, < 0.6.0)
railties (>= 6.0.0, < 7.2.0, != 7.0.0)

GEM
Expand Down Expand Up @@ -55,6 +54,9 @@ GEM
activerecord (6.1.7.6)
activemodel (= 6.1.7.6)
activesupport (= 6.1.7.6)
activerecord-trilogy-adapter (3.1.2)
activerecord (>= 6.0.a, < 7.1.a)
trilogy (>= 2.4.0)
activestorage (6.1.7.6)
actionpack (= 6.1.7.6)
activejob (= 6.1.7.6)
Expand Down Expand Up @@ -214,6 +216,7 @@ GEM
strscan (3.1.0)
thor (1.3.1)
timeout (0.4.1)
trilogy (2.7.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
Expand All @@ -227,17 +230,20 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
activerecord-trilogy-adapter (>= 3.1.2)
appraisal (~> 2.4.1)
climate_control (~> 0.0.3)
codeclimate-test-reporter (~> 1.0.3)
departure!
mysql2 (>= 0.4.0, <= 0.5.5)
pry-byebug
rails (= 6.1.7.6)
rake (>= 10.0)
rspec (~> 3.4, >= 3.4.0)
rspec-its (~> 1.2)
rubocop (~> 1.60.2)
rubocop-performance (~> 1.20.2)
trilogy (>= 2.7.0)

BUNDLED WITH
2.4.22
1 change: 1 addition & 0 deletions gemfiles/rails_7_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

source 'https://rubygems.org'

gem 'activerecord-trilogy-adapter', '>= 3.1.2', require: false
gem 'codeclimate-test-reporter', '~> 1.0.3', group: :test, require: nil
gem 'rails', '7.0.8'
gem 'rubocop', '~> 1.60.2', require: false
Expand Down
8 changes: 7 additions & 1 deletion gemfiles/rails_7_0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ PATH
specs:
departure (6.7.0)
activerecord (>= 6.0.0, < 7.2.0, != 7.0.0)
mysql2 (>= 0.4.0, < 0.6.0)
railties (>= 6.0.0, < 7.2.0, != 7.0.0)

GEM
Expand Down Expand Up @@ -62,6 +61,9 @@ GEM
activerecord (7.0.8)
activemodel (= 7.0.8)
activesupport (= 7.0.8)
activerecord-trilogy-adapter (3.1.2)
activerecord (>= 6.0.a, < 7.1.a)
trilogy (>= 2.4.0)
activestorage (7.0.8)
actionpack (= 7.0.8)
activejob (= 7.0.8)
Expand Down Expand Up @@ -213,6 +215,7 @@ GEM
strscan (3.1.0)
thor (1.3.1)
timeout (0.4.1)
trilogy (2.7.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
Expand All @@ -226,17 +229,20 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
activerecord-trilogy-adapter (>= 3.1.2)
appraisal (~> 2.4.1)
climate_control (~> 0.0.3)
codeclimate-test-reporter (~> 1.0.3)
departure!
mysql2 (>= 0.4.0, <= 0.5.5)
pry-byebug
rails (= 7.0.8)
rake (>= 10.0)
rspec (~> 3.4, >= 3.4.0)
rspec-its (~> 1.2)
rubocop (~> 1.60.2)
rubocop-performance (~> 1.20.2)
trilogy (>= 2.7.0)

BUNDLED WITH
2.4.22
4 changes: 3 additions & 1 deletion gemfiles/rails_7_1.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ PATH
specs:
departure (6.7.0)
activerecord (>= 6.0.0, < 7.2.0, != 7.0.0)
mysql2 (>= 0.4.0, < 0.6.0)
railties (>= 6.0.0, < 7.2.0, != 7.0.0)

GEM
Expand Down Expand Up @@ -244,6 +243,7 @@ GEM
strscan (3.1.0)
thor (1.3.1)
timeout (0.4.1)
trilogy (2.7.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
Expand All @@ -262,13 +262,15 @@ DEPENDENCIES
climate_control (~> 0.0.3)
codeclimate-test-reporter (~> 1.0.3)
departure!
mysql2 (>= 0.4.0, <= 0.5.5)
pry-byebug
rails (= 7.1.3)
rake (>= 10.0)
rspec (~> 3.4, >= 3.4.0)
rspec-its (~> 1.2)
rubocop (~> 1.60.2)
rubocop-performance (~> 1.20.2)
trilogy (>= 2.7.0)

BUNDLED WITH
2.4.22
15 changes: 4 additions & 11 deletions lib/active_record/connection_adapters/percona_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'active_record/connection_adapters/abstract_mysql_adapter'
require 'active_record/connection_adapters/statement_pool'
require 'active_record/connection_adapters/mysql2_adapter'
require 'active_support/core_ext/string/filters'
require 'departure'
require 'forwardable'
Expand All @@ -14,7 +13,8 @@ def percona_connection(config)
config = config.dup if config.frozen?
config[:username] = 'root'
end
mysql2_connection = mysql2_connection(config)
adapter = config[:original_adapter]
connection = send(Departure.connection_method(adapter), config)

connection_details = Departure::ConnectionDetails.new(config)
verbose = ActiveRecord::Migration.verbose
Expand All @@ -23,19 +23,12 @@ def percona_connection(config)
]
percona_logger = Departure::LoggerFactory.build(sanitizers: sanitizers, verbose: verbose)
cli_generator = Departure::CliGenerator.new(connection_details)

runner = Departure::Runner.new(
percona_logger,
cli_generator,
mysql2_connection
)

connection_options = { mysql_adapter: mysql2_connection }
runner = Departure::Runner.new(percona_logger, cli_generator, connection)

ConnectionAdapters::DepartureAdapter.new(
runner,
logger,
connection_options,
{ mysql_adapter: connection },
config
)
end
Expand Down
14 changes: 14 additions & 0 deletions lib/departure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,22 @@
end

module Departure
SUPPORTED_ADAPTERS = %w[trilogy mysql2].freeze

class << self
attr_accessor :configuration

def connection_method(adapter)
return "#{adapter}_connection" if Departure::SUPPORTED_ADAPTERS.include?(adapter)

if adapter.blank?
raise ArgumentError, 'You must supply the original_adapter when connecting ' \
"using the percona adapter. Supported adapters: #{SUPPORTED_ADAPTERS}"
end

raise ArgumentError, "Unsupported adater #{adapter}. Supported Departure " \
"adapters are #{Departure::SUPPORTED_ADAPTERS.inspect}"
end
end

def self.configure
Expand Down
6 changes: 5 additions & 1 deletion lib/departure/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def migrate(direction)

# Includes the Foreigner's Mysql2Adapter implemention in
# DepartureAdapter to support foreign keys
#
# Warning: Foreigner only works with MySQL2, and therefore
# trilogy is not supported
def include_foreigner
Foreigner::Adapter.safe_include(
:DepartureAdapter,
Expand All @@ -74,7 +77,8 @@ def include_foreigner
# instead of the current adapter.
def reconnect_with_percona
return if connection_config[:adapter] == 'percona'
Departure::ConnectionBase.establish_connection(connection_config.merge(adapter: 'percona'))
Departure::ConnectionBase.establish_connection(connection_config.merge(adapter: 'percona',
original_adapter: original_adapter))
end

# Reconnect without percona adapter when Departure is disabled but was
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'spec_helper'

describe ActiveRecord::ConnectionAdapters::DepartureAdapter::Column do
let(:field) { double(:field) }
let(:default) { double(:default) }
let(:cast_type) do
if defined?(ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::MysqlString)
ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::MysqlString.new
else
ActiveRecord::Type.lookup(:string, adapter: :mysql2)
end
end
let(:metadata) do
ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(
type: cast_type.type,
sql_type: type,
limit: cast_type.limit
)
end
let(:mysql_metadata) do
ActiveRecord::ConnectionAdapters::MySQL::TypeMetadata.new(metadata)
end
let(:type) { 'VARCHAR' }
let(:null) { double(:null) }
let(:collation) { double(:collation) }

let(:column) do
if ActiveRecord::VERSION::STRING >= '6.1'
described_class.new('field', 'default', mysql_metadata, null, collation: 'collation')
else
described_class.new(field, default, mysql_metadata, null, collation: collation)
end
end

describe '#adapter' do
subject { column.adapter }
it do
is_expected.to eq(
ActiveRecord::ConnectionAdapters::DepartureAdapter
)
end
end
end
Loading
Loading