Skip to content

Commit

Permalink
Fix for rails 7 (#72)
Browse files Browse the repository at this point in the history
* Fix typo

* Fix execute method, allow for async option

* Use splat operator to ensure Ruby 3 compatibility

Only relevant for Rails 6

* Add some documentation for running the tests

* Fix Rubocop warnings

* Improve ENV.fetch default

* v0.5.0
  • Loading branch information
jansi-z authored Mar 9, 2023
1 parent 3f278fc commit 8c5b233
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ If you have used the rails generator, you can set the variable to a falsey value
- "truthy" values: `[1, t, true, y, yes]`
- "falsey" values: `[0, f, false, n, no]`

### Running tests

To run the tests for all versions, run this script:

``` shell
bin/test_all_versions
```

Make sure to use the appropriate Ruby version! ActiveRecord <6.0 is not compatible with Ruby 3, so specs for those versions will only run successfully in a Ruby 2 environment.

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/wetransfer/ghost_adapter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](./CODE_OF_CONDUCT.md).
Expand Down
41 changes: 27 additions & 14 deletions lib/active_record/connection_adapters/mysql2_ghost_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def mysql2_ghost_connection(config)
end

client = Mysql2::Client.new(config)
if GhostAdapter::Internal.ghost_migration_enabeld?
dry_run = ENV['DRY_RUN'] == '1'
GhostAdapter::VersionChecker.validate_executable! unless ENV['SKIP_GHOST_VERSION_CHECK'] == '1'
if GhostAdapter::Internal.ghost_migration_enabled?
dry_run = ENV.fetch('DRY_RUN', nil) == '1'
GhostAdapter::VersionChecker.validate_executable! unless ENV.fetch('SKIP_GHOST_VERSION_CHECK', nil) == '1'
ConnectionAdapters::Mysql2GhostAdapter.new(client, logger, nil, config, dry_run: dry_run)
else
ConnectionAdapters::Mysql2Adapter.new(client, logger, nil, config)
Expand All @@ -42,16 +42,29 @@ def initialize(connection, logger, connection_options, config, dry_run: false)
@dry_run = dry_run
end

def execute(sql, name = nil)
# Only ALTER TABLE statements are automatically skipped by gh-ost
# We need to manually skip CREATE TABLE, DROP TABLE, and
# INSERT/DELETE (to schema migrations) for dry runs
return if dry_run && should_skip_for_dry_run?(sql)

if (table, query = parse_sql(sql))
GhostAdapter::Migrator.execute(table, query, database, dry_run)
else
super(sql, name)
if Gem.loaded_specs['activerecord'].version >= Gem::Version.new('7.0')
def execute(sql, name = nil, async: false)
# Only ALTER TABLE statements are automatically skipped by gh-ost
# We need to manually skip CREATE TABLE, DROP TABLE, and
# INSERT/DELETE (to schema migrations) for dry runs
return if dry_run && should_skip_for_dry_run?(sql)

if (table, query = parse_sql(sql))
GhostAdapter::Migrator.execute(table, query, database, dry_run)
else
super(sql, name, async: async)
end
end
else
def execute(sql, name = nil)
# See comment above -- some tables need to be skipped manually for dry runs
return if dry_run && should_skip_for_dry_run?(sql)

if (table, query = parse_sql(sql))
GhostAdapter::Migrator.execute(table, query, database, dry_run)
else
super(sql, name)
end
end
end

Expand Down Expand Up @@ -80,7 +93,7 @@ def remove_index(table_name, column_name = nil, **options)
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)
index_name, index_type, index_columns, _index_options = add_index_options(table_name, column_name, **options)

sql = build_add_index_sql(
table_name, index_columns, index_name,
Expand Down
6 changes: 3 additions & 3 deletions lib/ghost_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

require 'ghost_adapter/config'

require 'ghost_adapter/railtie' if defined? ::Rails::Railtie
require 'ghost_adapter/railtie' if defined? Rails::Railtie

module GhostAdapter
def self.config
Expand Down Expand Up @@ -43,8 +43,8 @@ def self.enable_ghost_migration!
@@ghost_migration_enabled = true # rubocop:disable Style/ClassVars
end

def self.ghost_migration_enabeld?
env_val = ENV['GHOST_MIGRATE']&.downcase
def self.ghost_migration_enabled?
env_val = ENV.fetch('GHOST_MIGRATE', 'false').downcase
return false if %w[0 n no f false].include?(env_val)

!!@@ghost_migration_enabled || %w[1 y yes t true].include?(env_val)
Expand Down
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.4.2'.freeze
VERSION = '0.5.0'.freeze
end
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
describe 'clean_query' do
it 'parses query correctly' do
sql =
'ADD index_type INDEX `bar_index_name` (`bar_id`), '\
'ADD index_type INDEX `bar_index_name` (`bar_id`), ' \
'ADD index_type INDEX `baz_index_name` (`baz_id`);;;'

sanatized_sql =
'ADD index_type INDEX `bar_index_name` (`bar_id`), '\
'ADD index_type INDEX `bar_index_name` (`bar_id`), ' \
'ADD index_type INDEX `baz_index_name` (`baz_id`)'

expect(GhostAdapter::Migrator).to receive(:execute)
Expand Down

0 comments on commit 8c5b233

Please sign in to comment.