class ActiveRecord::ConnectionAdapters::MysqlPtOscAdapter

Constants

ADAPTER_NAME

Public Instance Methods

adapter_name() click to toggle source
# File lib/active_record/connection_adapters/mysql_pt_osc_adapter.rb, line 23
def adapter_name
  'mysql2' # For compatibility with code that check adapter name
end
add_column(table_name, column_name, type, options = {}) click to toggle source
# File lib/active_record/connection_adapters/mysql_pt_osc_adapter.rb, line 35
def add_column(table_name, column_name, type, options = {})
  add_command(table_name, add_column_sql(table_name, column_name, type, options))
end
add_index(table_name, column_name, options = {}) click to toggle source

Adds a new index to the table. column_name can be a single Symbol, or an Array of Symbols.

The index will be named after the table and the column name(s), unless you pass :name as an option.

Examples
Creating a simple index
add_index(:suppliers, :name)

generates

CREATE INDEX suppliers_name_index ON suppliers(name)
Creating a unique index
add_index(:accounts, [:branch_id, :party_id], :unique => true)

generates

CREATE UNIQUE INDEX accounts_branch_id_party_id_index ON accounts(branch_id, party_id)
Creating a named index
add_index(:accounts, [:branch_id, :party_id], :unique => true, :name => 'by_branch_party')

generates

CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id)
Creating an index with specific key length
add_index(:accounts, :name, :name => 'by_name', :length => 10)

generates

CREATE INDEX by_name ON accounts(name(10))

add_index(:accounts, [:name, :surname], :name => 'by_name_surname', :length => {:name => 10, :surname => 15})

generates

CREATE INDEX by_name_surname ON accounts(name(10), surname(15))
Creating an index with a sort order (desc or asc, asc is the default)
add_index(:accounts, [:branch_id, :party_id, :surname], :order => {:branch_id => :desc, :part_id => :asc})

generates

CREATE INDEX by_branch_desc_party ON accounts(branch_id DESC, party_id ASC, surname)

Note: mysql doesn’t yet support index order (it accepts the syntax but ignores it)

# File lib/active_record/connection_adapters/mysql_pt_osc_adapter.rb, line 102
def add_index(table_name, column_name, options = {})
  index_name, index_type, index_columns = add_index_options(table_name, column_name, options)
  add_command(table_name, "ADD #{index_type} INDEX #{quote_column_name(index_name)} (#{index_columns})")
end
clear_commands() click to toggle source
# File lib/active_record/connection_adapters/mysql_pt_osc_adapter.rb, line 111
def clear_commands
  @osc_commands = {}
end
get_commanded_tables() click to toggle source
# File lib/active_record/connection_adapters/mysql_pt_osc_adapter.rb, line 120
def get_commanded_tables
  get_commands.keys
end
get_commands_string(table_name) click to toggle source
# File lib/active_record/connection_adapters/mysql_pt_osc_adapter.rb, line 115
def get_commands_string(table_name)
  get_commands[table_name] ||= []
  get_commands[table_name].join(',')
end
remove_column(table_name, *column_names) click to toggle source

Removes the column(s) from the table definition.

Examples
remove_column(:suppliers, :qualification)
remove_columns(:suppliers, :qualification, :experience)
# File lib/active_record/connection_adapters/mysql_pt_osc_adapter.rb, line 51
def remove_column(table_name, *column_names)
  if column_names.flatten!
    message = 'Passing array to remove_columns is deprecated, please use ' +
      'multiple arguments, like: `remove_columns(:posts, :foo, :bar)`'
    ActiveSupport::Deprecation.warn message, caller
  end

  columns_for_remove(table_name, *column_names).each do |column_name|
    add_command(table_name, "DROP COLUMN #{column_name}")
  end
end
rename_table(table_name, new_name) click to toggle source

Renames a table.

Example:

rename_table('octopuses', 'octopi')
# File lib/active_record/connection_adapters/mysql_pt_osc_adapter.rb, line 31
def rename_table(table_name, new_name)
  add_command(table_name, "RENAME TO #{quote_table_name(new_name)}")
end

Protected Instance Methods

add_command(table_name, command) click to toggle source
# File lib/active_record/connection_adapters/mysql_pt_osc_adapter.rb, line 125
      def add_command(table_name, command)
        warn (<<-WARN
        You are trying to ALTER table "#{table_name}" with the mysql_pt_osc adapter without using an PtOscMigration.
        Be aware that ALTER commands will only be executed via pt-online-schema-change inside of an ActiveRecord::PtOscMigration.
        It is likely that although your migration will complete, no schema alterations have been made.
        WARN
        ) if caller.any? { |c| c.include? 'active_record/migration.rb' } && caller.none? { |c| c.include? 'active_record/pt_osc_migration.rb' }
        get_commands[table_name] ||= []
        get_commands[table_name] << command
      end
get_commands(table_name = nil) click to toggle source
# File lib/active_record/connection_adapters/mysql_pt_osc_adapter.rb, line 144
def get_commands(table_name = nil)
  @osc_commands ||= {}
  table_name.nil? ? @osc_commands : @osc_commands[table_name]
end
warn(message) click to toggle source

Provides the opportunity to handle warnings in a custom way @param [String] message

Calls superclass method
# File lib/active_record/connection_adapters/mysql_pt_osc_adapter.rb, line 138
def warn(message)
  # Rake tasks loaded through Railties had warnings silenced before Rails 4.1
  # @see https://github.com/rails/rails/pull/11601
  defined?(Rails) && Rails.version < '4.1' ? enable_warnings { super } : super
end