Skip to content

Commit

Permalink
Merge pull request #46 from sikachu/rails-6
Browse files Browse the repository at this point in the history
Add Rails 6 support
  • Loading branch information
wyhaines authored Jun 5, 2020
2 parents c643ee4 + 62153a7 commit 803563b
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 79 deletions.
20 changes: 16 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
language: ruby

rvm:
- 2.2.8

- 2.5.5
- 2.6.3
- 2.7.0

env:
- RAILS_VERSION="~> 6.0.0"
- RAILS_VERSION="~> 5.2.0"

jobs:
include:
- rvm: 2.4.6
env: RAILS_VERSION="~> 5.2.0"

services:
- mysql

before_install:
- sudo apt-key adv --keyserver keys.gnupg.net --recv-keys 8507EFA5
- travis_retry sudo apt-key adv --keyserver keys.gnupg.net --recv-keys 8507EFA5
- echo "deb http://repo.percona.com/apt `lsb_release -cs` main" | sudo tee -a /etc/apt/sources.list
- sudo apt-get update -qq
- sudo apt-get install percona-toolkit
Expand Down
8 changes: 6 additions & 2 deletions departure.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require 'departure/version'

# This environment variable is set on CI to facilitate testing with multiple
# versions of Rails.
RAILS_DEPENDENCY_VERSION = ENV.fetch('RAILS_VERSION', ['>= 5.2.0', '< 6.1'])

Gem::Specification.new do |spec|
spec.name = 'departure'
spec.version = Departure::VERSION
Expand All @@ -19,8 +23,8 @@ Gem::Specification.new do |spec|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.require_paths = ['lib']

spec.add_runtime_dependency 'railties', '~> 5.2.0'
spec.add_runtime_dependency 'activerecord', '~> 5.2.0'
spec.add_runtime_dependency 'railties', *Array(RAILS_DEPENDENCY_VERSION)
spec.add_runtime_dependency 'activerecord', *Array(RAILS_DEPENDENCY_VERSION)
spec.add_runtime_dependency 'mysql2', '>= 0.4.0', '<= 0.5.3'

spec.add_development_dependency 'rake', '~> 10.0'
Expand Down
10 changes: 10 additions & 0 deletions lib/active_record/connection_adapters/percona_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ def change_table(table_name, _options = {})
def error_number(_exception); end

def full_version
if ActiveRecord::VERSION::MAJOR < 6
get_full_version
else
schema_cache.database_version.full_version_string
end
end

# This is a method defined in Rails 6.0, and we have no control over the
# naming of this method.
def get_full_version # rubocop:disable Naming/AccessorMethodName
mysql_adapter.raw_connection.server_info[:version]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
let(:collation) { double(:collation) }

let(:column) do
described_class.new(field, default, mysql_metadata, type, null, collation)
if ActiveRecord::VERSION::MAJOR < 6
described_class.new(field, default, mysql_metadata, type, null, collation)
else
described_class.new(field, default, mysql_metadata, null, collation: collation)
end
end

describe '#adapter' do
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/change_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class Comment < ActiveRecord::Base; end

let(:migration_fixtures) do
ActiveRecord::MigrationContext.new([MIGRATION_FIXTURES]).migrations.select do |m|
ActiveRecord::MigrationContext.new([MIGRATION_FIXTURES], ActiveRecord::SchemaMigration).migrations.select do |m|
m.version == version
end
end
Expand All @@ -19,7 +19,7 @@ def column_metadata(table, name)

context 'creating column' do
before(:each) do
ActiveRecord::Migrator.new(direction, migration_fixtures, version).migrate
ActiveRecord::Migrator.new(direction, migration_fixtures, ActiveRecord::SchemaMigration, version).migrate
end

it 'adds the column in the DB table' do
Expand Down
30 changes: 17 additions & 13 deletions spec/integration/columns_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class Comment < ActiveRecord::Base; end

let(:migration_fixtures) do
ActiveRecord::MigrationContext.new([MIGRATION_FIXTURES]).migrations
ActiveRecord::MigrationContext.new([MIGRATION_FIXTURES], ActiveRecord::SchemaMigration).migrations
end
let(:migration_paths) { [MIGRATION_FIXTURES] }

Expand All @@ -20,6 +20,7 @@ class Comment < ActiveRecord::Base; end
ActiveRecord::Migrator.new(
direction,
migration_fixtures,
ActiveRecord::SchemaMigration,
version
).migrate

Expand All @@ -30,6 +31,7 @@ class Comment < ActiveRecord::Base; end
ActiveRecord::Migrator.new(
direction,
migration_fixtures,
ActiveRecord::SchemaMigration,
version
).migrate

Expand All @@ -41,13 +43,14 @@ class Comment < ActiveRecord::Base; end
let(:direction) { :down }

before do
ActiveRecord::Migrator.new(:up, migration_fixtures, version).migrate
ActiveRecord::Migrator.new(:up, migration_fixtures, ActiveRecord::SchemaMigration, version).migrate
end

it 'drops the column from the DB table' do
ActiveRecord::Migrator.new(
direction,
migration_fixtures,
ActiveRecord::SchemaMigration,
version - 1
).migrate

Expand All @@ -58,6 +61,7 @@ class Comment < ActiveRecord::Base; end
ActiveRecord::Migrator.new(
direction,
migration_fixtures,
ActiveRecord::SchemaMigration,
version - 1
).migrate

Expand All @@ -78,12 +82,12 @@ class Comment < ActiveRecord::Base; end
end

it 'changes the column name' do
ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(:comments).to have_column('new_id_field')
end

it 'does not keep the old column' do
ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(:comments).not_to have_column('some_id_field')
end
end
Expand All @@ -96,19 +100,19 @@ class Comment < ActiveRecord::Base; end
end

before do
ActiveRecord::Migrator.new(:up, migration_fixtures, 1).migrate
ActiveRecord::Migrator.new(:up, migration_fixtures, ActiveRecord::SchemaMigration, 1).migrate
end

context 'when null is true' do
let(:version) { 14 }

it 'sets the column to allow nulls' do
ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(column.null).to be_truthy
end

it 'marks the migration as up' do
ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(ActiveRecord::Migrator.current_version).to eq(version)
end
end
Expand All @@ -117,12 +121,12 @@ class Comment < ActiveRecord::Base; end
let(:version) { 15 }

it 'sets the column not to allow nulls' do
ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(column.null).to be_falsey
end

it 'marks the migration as up' do
ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(ActiveRecord::Migrator.current_version).to eq(version)
end
end
Expand All @@ -132,12 +136,12 @@ class Comment < ActiveRecord::Base; end
let(:version) { 22 }

it 'adds a created_at column' do
ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(:comments).to have_column('created_at')
end

it 'adds a updated_at column' do
ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(:comments).to have_column('updated_at')
end
end
Expand All @@ -154,12 +158,12 @@ class Comment < ActiveRecord::Base; end
end

it 'removes the created_at column' do
ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(:comments).not_to have_column('created_at')
end

it 'removes the updated_at column' do
ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(:comments).not_to have_column('updated_at')
end
end
Expand Down
16 changes: 8 additions & 8 deletions spec/integration/data_migrations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Comment < ActiveRecord::Base; end
let(:version) { 9 }

it 'updates all the required data' do
ActiveRecord::MigrationContext.new(migration_fixtures).run(
ActiveRecord::MigrationContext.new(migration_fixtures, ActiveRecord::SchemaMigration).run(
direction,
version
)
Expand All @@ -34,7 +34,7 @@ class Comment < ActiveRecord::Base; end
end

it 'marks the migration as up' do
ActiveRecord::MigrationContext.new(migration_fixtures).run(
ActiveRecord::MigrationContext.new(migration_fixtures, ActiveRecord::SchemaMigration).run(
direction,
version
)
Expand All @@ -47,7 +47,7 @@ class Comment < ActiveRecord::Base; end
let(:version) { 10 }

it 'updates all the required data' do
ActiveRecord::MigrationContext.new(migration_fixtures).run(
ActiveRecord::MigrationContext.new(migration_fixtures, ActiveRecord::SchemaMigration).run(
direction,
version
)
Expand All @@ -56,7 +56,7 @@ class Comment < ActiveRecord::Base; end
end

it 'marks the migration as up' do
ActiveRecord::MigrationContext.new(migration_fixtures).run(
ActiveRecord::MigrationContext.new(migration_fixtures, ActiveRecord::SchemaMigration).run(
direction,
version
)
Expand All @@ -69,7 +69,7 @@ class Comment < ActiveRecord::Base; end
let(:version) { 11 }

it 'updates all the required data' do
ActiveRecord::MigrationContext.new(migration_fixtures).run(
ActiveRecord::MigrationContext.new(migration_fixtures, ActiveRecord::SchemaMigration).run(
direction,
version
)
Expand All @@ -78,7 +78,7 @@ class Comment < ActiveRecord::Base; end
end

it 'marks the migration as up' do
ActiveRecord::MigrationContext.new(migration_fixtures).run(
ActiveRecord::MigrationContext.new(migration_fixtures, ActiveRecord::SchemaMigration).run(
direction,
version
)
Expand All @@ -91,7 +91,7 @@ class Comment < ActiveRecord::Base; end
let(:version) { 12 }

it 'updates all the required data' do
ActiveRecord::MigrationContext.new(migration_fixtures).run(
ActiveRecord::MigrationContext.new(migration_fixtures, ActiveRecord::SchemaMigration).run(
direction,
version
)
Expand All @@ -100,7 +100,7 @@ class Comment < ActiveRecord::Base; end
end

it 'marks the migration as up' do
ActiveRecord::MigrationContext.new(migration_fixtures).run(
ActiveRecord::MigrationContext.new(migration_fixtures, ActiveRecord::SchemaMigration).run(
direction,
version
)
Expand Down
12 changes: 6 additions & 6 deletions spec/integration/foreign_keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class Comment < ActiveRecord::Base; end

let(:migration_fixtures) do
ActiveRecord::MigrationContext.new([MIGRATION_FIXTURES]).migrations
ActiveRecord::MigrationContext.new([MIGRATION_FIXTURES], ActiveRecord::SchemaMigration).migrations
end
let(:migration_paths) { [MIGRATION_FIXTURES] }

Expand All @@ -24,7 +24,7 @@ class Comment < ActiveRecord::Base; end
end

it 'adds a foreign key' do
ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(:comments).to have_foreign_key_on('product_id')
end
end
Expand All @@ -45,28 +45,28 @@ class Comment < ActiveRecord::Base; end
it 'when foreign key has default name' do
ActiveRecord::Base.connection.add_foreign_key(:comments, :products)

ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(:comments).not_to have_foreign_key_on('product_id')
end

it 'when foreign key has a custom name' do
ActiveRecord::Base.connection.add_foreign_key(:comments, :products, name: "fk_123456")

ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(:comments).not_to have_foreign_key_on('product_id')
end

it 'when foreign key has a custom name prefixed with _' do
ActiveRecord::Base.connection.add_foreign_key(:comments, :products, name: "_fk_123456")

ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(:comments).not_to have_foreign_key_on('product_id')
end

it 'when foreign key has a custom name prefixed with __ (double _)' do
ActiveRecord::Base.connection.add_foreign_key(:comments, :products, name: "__fk_123456")

ActiveRecord::MigrationContext.new(migration_paths).run(direction, version)
ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::SchemaMigration).run(direction, version)
expect(:comments).not_to have_foreign_key_on('product_id')
end
end
Expand Down
Loading

0 comments on commit 803563b

Please sign in to comment.