Module: Switcheroo::SchemaStatements

Included in:
ActiveRecord::ConnectionAdapters::AbstractAdapter
Defined in:
lib/switcheroo/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#switcheroo(table_name, options = {}) {|ActiveRecord::ConnectionAdapters::Table.new(switch_options[:clone_table_name], switch_options[:recorder])| ... } ⇒ Object

Yields:

  • (ActiveRecord::ConnectionAdapters::Table.new(switch_options[:clone_table_name], switch_options[:recorder]))


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/switcheroo/schema_statements.rb', line 3

def switcheroo(table_name, options = {})
  switch_options = {
    :table_name => table_name,
    :clone_table_name => "clone_#{table_name}",
    :columns => columns(table_name),
    :indexes => indexes(table_name),
    :pk_and_sequence => pk_and_sequence_for(table_name),
    :recorder => ActiveRecord::Migration::CommandRecorder.new(self)
  }
  switch_options[:column_mappings] = switch_options[:columns].inject({}) do |hsh,column|
    hsh[column.name] = column.name
    hsh
  end

  yield ActiveRecord::ConnectionAdapters::Table.new(switch_options[:clone_table_name], switch_options[:recorder])

  prepare_clone_table(switch_options)
  clone_data(switch_options)
  switch_tables(switch_options)
  build_new_indexes(switch_options)
  vacuum_table(switch_options)
end