diff --git a/README.md b/README.md index 42f0b8c..cb0ead5 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/lib/active_record/connection_adapters/mysql2_ghost_adapter.rb b/lib/active_record/connection_adapters/mysql2_ghost_adapter.rb index 02c1841..af5e5e7 100644 --- a/lib/active_record/connection_adapters/mysql2_ghost_adapter.rb +++ b/lib/active_record/connection_adapters/mysql2_ghost_adapter.rb @@ -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) @@ -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 @@ -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, diff --git a/lib/ghost_adapter.rb b/lib/ghost_adapter.rb index 9454697..4ca27b2 100644 --- a/lib/ghost_adapter.rb +++ b/lib/ghost_adapter.rb @@ -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 @@ -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) diff --git a/lib/ghost_adapter/version.rb b/lib/ghost_adapter/version.rb index 2b73e9b..9680fdc 100644 --- a/lib/ghost_adapter/version.rb +++ b/lib/ghost_adapter/version.rb @@ -1,3 +1,3 @@ module GhostAdapter - VERSION = '0.4.2'.freeze + VERSION = '0.5.0'.freeze end diff --git a/spec/active_record/connection_adapters/mysql2_ghost_adapter_spec.rb b/spec/active_record/connection_adapters/mysql2_ghost_adapter_spec.rb index baa8d4a..5b7cd25 100644 --- a/spec/active_record/connection_adapters/mysql2_ghost_adapter_spec.rb +++ b/spec/active_record/connection_adapters/mysql2_ghost_adapter_spec.rb @@ -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)