class ActiveRecord::ModelSpaces::TableManager

manages the creation and destruction of tables, and the bulk handling of data in those tables

Constants

TABLE_SCHEMA_COPIERS

Attributes

connection[R]
model[R]

Public Class Methods

new(model) click to toggle source
# File lib/active_record/model_spaces/table_manager.rb, line 14
def initialize(model)
  @model = model_from_name(model)
  @connection = @model.connection
end

Public Instance Methods

copy_table(from, to) click to toggle source

copy all data from one table to another

# File lib/active_record/model_spaces/table_manager.rb, line 45
def copy_table(from, to)
  connection.execute("insert into #{to} select * from #{from}") if from != to
end
create_table(base_table_name, table_name) click to toggle source

create a new table with the same schema as the base_table, but a different name

# File lib/active_record/model_spaces/table_manager.rb, line 20
def create_table(base_table_name, table_name)
  if table_name != base_table_name && !connection.table_exists?(table_name)
    get_table_schema_copier(connection).copy_table_schema(connection, base_table_name, table_name)
  end
end
drop_table(table_name) click to toggle source

drop a table

# File lib/active_record/model_spaces/table_manager.rb, line 27
def drop_table(table_name)
  connection.execute("drop table #{table_name}") if connection.table_exists?(table_name)
end
recreate_table(base_table_name, table_name) click to toggle source

drop and recreate a table

# File lib/active_record/model_spaces/table_manager.rb, line 32
def recreate_table(base_table_name, table_name)
  if table_name != base_table_name
    drop_table(table_name)
    create_table(base_table_name, table_name)
 end
end
truncate_table(table_name) click to toggle source

truncate a table

# File lib/active_record/model_spaces/table_manager.rb, line 40
def truncate_table(table_name)
  connection.execute("truncate table #{table_name}")
end

Private Instance Methods

get_table_schema_copier(connection) click to toggle source
# File lib/active_record/model_spaces/table_manager.rb, line 53
def get_table_schema_copier(connection)
  adapter_name = connection.adapter_name

  if !TABLE_SCHEMA_COPIERS[adapter_name]
    klassname = "ActiveRecord::ModelSpaces::#{adapter_name}TableSchemaCopier"
    klass = class_from_classname(klassname)
    if klass
      TABLE_SCHEMA_COPIERS[adapter_name] = klass
    else
      TABLE_SCHEMA_COPIERS[adapter_name] = DefaultTableSchemaCopier
    end
  end

  TABLE_SCHEMA_COPIERS[adapter_name]
end