Fast Change Table

Use fast_change_table instead of change_table in your migrations on large tables of data. Uses a duplication pattern to speed things up.

Known issues:

  • Currently fast_change_table transformations do not support Rails 3 reversible migration method.

Usage:

uses ordinary change_table syntax but adds two options

  • "replace_keys" to remove all indexes; new indexes will be specified
  • "disable_keys" to remove indexes and apply them after data load; this is a tremendous performance enhancement for a dbms with fast index creation; it is active by default. set it to false to prevent its usage

the bulk option is set by default; set it to false to prevent its usage.

Example:

fast_change_table :table_name, :disable_keys => true do |t|
  t.change :old_string, :string, :limit => 64
  t.rename :old_string, :new_string
  t.integer :an_integer
end

other methods:

create_table_like(orignal_table, new_table)

  • creates a table with the same structure.
  • BE CAREFUL!, create_table_like always drops the new table if it exists

Example of creating a similar table but without the indexes and an added column:

create_table_like :original_table, :new_table, :remove_keys => true do |t|
  t.integer :an_integer
end

disable_indexes(table)

  • removes all indexes from a table, returns a list of index objects removed. uses bulk alter when possible

enable_indexes(table, list_of_indexes)

  • restores a list of indexes to a table. uses bulk alter when possible

fast_add_indexes(table, &block)

  • allows you to pass a block to add indexes. uses bulk alter when possible

Example:

fast_add_indexes :sometable do |t|
 t.index :some_column
 t.index [:some_other_column, :column_three], :name => "a_multicolumn_index"
end

copy_table_data(from_table, to_table, remaps = [])

  • copies rows from one table into another. by default copies data from column of from_table to to_table of same name. will not copy data where there is no corresponding column. the remaps argument can be supplied to tell copy table how to handle unmatched columns or override this behavior for multiple remap columns provide an array of 2 element arrays

Examples:

copy_table_data(old_users_without_email_hash, new_table, ['MD5(email)', 'email_hash'])

copy_table_data(old_users_without_total, new_table, [['sum(payments)', 'total_payments'], ['avg(payments)', 'average_payments']])