Skip to content

Commit

Permalink
ActiveRecord >= 6.1 compatibility (#61)
Browse files Browse the repository at this point in the history
* Update rubocop to be a bit more lenient with class/method length

* Update gemspec to ensure ActiveRecord < 6.2

* Add compatibility for ActiveRecord >= 6.1

* Re-remove Gemfile.lock

* Add tests for add_index and remove_index methods

* Fix specs

* Fix add_index for ruby3/AR6

* Fix test matrix since activerecord 6.0 is incompatible with ruby 3

* v0.3.0

* Add custom matcher replacing matching method

* Update script to automatically test against all gemfiles in the gemfiles directory
  • Loading branch information
acroos authored Nov 4, 2021
1 parent 7c8e7c5 commit 61c7b31
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 53 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ jobs:
- "gemfiles/activerecord-6.0.Gemfile"
- "gemfiles/activerecord-6.1.Gemfile"
exclude:
# Ruby 3 is not supported for ActiveRecord < 6
# Ruby 3 is not supported for ActiveRecord < 6.1
- ruby-version: "3.0"
gemfile: "gemfiles/activerecord-5.0.Gemfile"
- ruby-version: "3.0"
gemfile: "gemfiles/activerecord-5.1.Gemfile"
- ruby-version: "3.0"
gemfile: "gemfiles/activerecord-5.2.Gemfile"
- ruby-version: "3.0"
gemfile: "gemfiles/activerecord-6.0.Gemfile"
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
Expand All @@ -43,4 +45,4 @@ jobs:
- name: Run rubocop
run: bundle exec rake rubocop
- name: Run tests
run: bundle exec rake spec
run: bundle exec --gemfile ${{ matrix.gemfile }} rspec
20 changes: 20 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,36 @@ Style/Documentation:
Style/RescueModifier:
Enabled: false

Layout/LineLength:
Exclude:
- spec/**/*_spec.rb

Metrics/BlockLength:
Exclude:
- spec/**/*_spec.rb

Metrics/ClassLength:
CountAsOne:
- 'array'
- 'hash'
- 'heredoc'
Max: 150

Metrics/MethodLength:
Max: 15
CountAsOne:
- 'array'
- 'hash'
- 'heredoc'
IgnoredMethods:
- build_ghost_command
- mysql2_ghost_connection

Metrics/ModuleLength:
CountAsOne:
- 'array'
- 'hash'
- 'heredoc'
Exclude:
- lib/ghost_adapter/config.rb

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 0.3.0
- Fix compatibility for ActiveRecord 6.1 ([#61](https://github.com/WeTransfer/ghost_adapter/pull/61))

## 0.2.3
- Add comma to allowed characters when cleaning SQL queries ([#52](https://github.com/WeTransfer/ghost_adapter/pull/52))

Expand Down
18 changes: 18 additions & 0 deletions bin/test_all_versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

test_version() {
local version=$(echo $1 | sed "s/[^0-9.]*//g")
echo
echo "----------------------------------------------------------------------------------------------------------------------"
echo -e "\033[1;34mTesting against ActiveRecord ${version}\033[0m"

local gemfile="${1}"
shift

bundle install --gemfile "${gemfile}" --quiet
bundle exec --gemfile "${gemfile}" rspec "$@"
}

for gemfile in $(find gemfiles -type f -regex ".*\.Gemfile$"); do
test_version "${gemfile}"
done
4 changes: 2 additions & 2 deletions ghost_adapter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ Gem::Specification.new do |spec|
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|doc)/}) }
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|doc|bin)/}) }
end
spec.require_paths = ['lib']

spec.add_dependency 'activerecord', '>= 5'
spec.add_dependency 'activerecord', '>= 5', '< 6.2'
spec.add_dependency 'mysql2', '>= 0.4.0', '< 0.6.0'

spec.add_development_dependency 'bump', '~> 0'
Expand Down
66 changes: 59 additions & 7 deletions lib/active_record/connection_adapters/mysql2_ghost_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,47 @@ def execute(sql, name = nil)
end
end

def add_index(table_name, column_name, options = {})
index_name, index_type, index_columns, index_options = add_index_options(table_name, column_name, options)
execute "ALTER TABLE #{quote_table_name(table_name)} ADD #{index_type} INDEX #{quote_column_name(index_name)} (#{index_columns})#{index_options}" # rubocop:disable Layout/LineLength
end
if Gem.loaded_specs['activerecord'].version >= Gem::Version.new('6.1')
def add_index(table_name, column_name, **options)
index, algorithm, if_not_exists = add_index_options(table_name, column_name, **options)
return if if_not_exists && index_exists?(table_name, column_name, name: index.name)

index_type = index.type&.to_s&.upcase || (index.unique ? 'UNIQUE' : nil)

sql = build_add_index_sql(
table_name, quoted_columns(index), index.name,
index_type: index_type,
using: index.using,
algorithm: algorithm
)

execute sql
end

def remove_index(table_name, column_name = nil, **options)
return if options[:if_exists] && !index_exists?(table_name, column_name, **options)

index_name = index_name_for_remove(table_name, column_name, options)
execute "ALTER TABLE #{quote_table_name(table_name)} DROP INDEX #{quote_column_name(index_name)}"
end
else
def add_index(table_name, column_name, options = {})
index_name, index_type, index_columns, _index_options = add_index_options(table_name, column_name, options)

def remove_index(table_name, options = {})
index_name = index_name_for_remove(table_name, options)
execute "ALTER TABLE #{quote_table_name(table_name)} DROP INDEX #{quote_column_name(index_name)}"
sql = build_add_index_sql(
table_name, index_columns, index_name,
index_type: index_type&.upcase,
using: options[:using]
)

execute sql
end

def remove_index(table_name, options = {})
options = { column: options } unless options.is_a?(Hash)
index_name = index_name_for_remove(table_name, options)
execute "ALTER TABLE #{quote_table_name(table_name)} DROP INDEX #{quote_column_name(index_name)}"
end
end

private
Expand Down Expand Up @@ -111,6 +144,25 @@ def schema_migration_update?(sql)
INSERT_SCHEMA_MIGRATION_PATTERN =~ sql ||
DROP_SCHEMA_MIGRATION_PATTERN =~ sql
end

def build_add_index_sql(table_name, column_names, index_name, # rubocop:disable Metrics/ParameterLists
index_type: nil, using: nil, algorithm: nil)
sql = %w[ALTER TABLE]
sql << quote_table_name(table_name)
sql << 'ADD'
sql << index_type
sql << 'INDEX'
sql << quote_column_name(index_name)
sql << "USING #{using}" if using
sql << "(#{column_names})"
sql << algorithm

sql.compact.join(' ').gsub(/\s+/, ' ')
end

def quoted_columns(index)
index.columns.is_a?(String) ? index.columns : quoted_columns_for_index(index.columns, index.column_options)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/ghost_adapter/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GhostAdapter
VERSION = '0.2.3'.freeze
VERSION = '0.3.0'.freeze
end
Loading

0 comments on commit 61c7b31

Please sign in to comment.