class ActiveRecord::ConnectionAdapters::Mysql2GhostAdapter

Constants

ADAPTER_NAME
ALTER_TABLE_PATTERN
CREATE_TABLE_PATTERN
DROP_SCHEMA_MIGRATION_PATTERN
DROP_TABLE_PATTERN
INSERT_SCHEMA_MIGRATION_PATTERN
QUERY_ALLOWABLE_CHARS

Attributes

database[R]
dry_run[R]

Public Class Methods

new(connection, logger, connection_options, config, dry_run: false) click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/mysql2_ghost_adapter.rb, line 39
def initialize(connection, logger, connection_options, config, dry_run: false)
  super(connection, logger, connection_options, config)
  @database = config[:database]
  @dry_run = dry_run
end

Public Instance Methods

add_index(table_name, column_name, options = {}) click to toggle source
# File lib/active_record/connection_adapters/mysql2_ghost_adapter.rb, line 58
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
execute(sql, name = nil) click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/mysql2_ghost_adapter.rb, line 45
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)
  end
end
remove_index(table_name, options = {}) click to toggle source
# File lib/active_record/connection_adapters/mysql2_ghost_adapter.rb, line 63
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)}"
end

Private Instance Methods

clean_query(query) click to toggle source
# File lib/active_record/connection_adapters/mysql2_ghost_adapter.rb, line 90
def clean_query(query)
  cleaned = query.gsub(QUERY_ALLOWABLE_CHARS, '')
  cleaned.gsub('"', '\"')
end
create_or_drop_table?(sql) click to toggle source
# File lib/active_record/connection_adapters/mysql2_ghost_adapter.rb, line 105
def create_or_drop_table?(sql)
  CREATE_TABLE_PATTERN =~ sql ||
    DROP_TABLE_PATTERN =~ sql
end
parse_sql(sql) click to toggle source
# File lib/active_record/connection_adapters/mysql2_ghost_adapter.rb, line 79
def parse_sql(sql)
  capture = sql.match(ALTER_TABLE_PATTERN)
  return if capture.nil?

  captured_names = capture.names
  return unless captured_names.include? 'table_name'
  return unless captured_names.include? 'query'

  [capture[:table_name], clean_query(capture[:query])]
end
schema_migration_update?(sql) click to toggle source
# File lib/active_record/connection_adapters/mysql2_ghost_adapter.rb, line 110
def schema_migration_update?(sql)
  INSERT_SCHEMA_MIGRATION_PATTERN =~ sql ||
    DROP_SCHEMA_MIGRATION_PATTERN =~ sql
end
should_skip_for_dry_run?(sql) click to toggle source
# File lib/active_record/connection_adapters/mysql2_ghost_adapter.rb, line 95
def should_skip_for_dry_run?(sql)
  if create_or_drop_table?(sql)
    puts 'Skipping CREATE TABLE or DROP TABLE for dry run'
    puts 'SQL:'
    puts sql
  end

  create_or_drop_table?(sql) || schema_migration_update?(sql)
end