Module: ActiveRecord::ModelSpaces::DefaultTableSchemaCopier

Defined in:
lib/active_record/model_spaces/table_manager.rb

Class Method Summary collapse

Class Method Details

.change_table_name(base_table_name, table_name, schema) ⇒ Object

change the table_name in a schema.rb fragment



104
105
106
107
108
# File 'lib/active_record/model_spaces/table_manager.rb', line 104

def change_table_name(base_table_name, table_name, schema)
  schema.
    gsub(/create_table \"#{base_table_name}\"/, "create_table \"#{table_name}\"").
    gsub(/add_index \"#{base_table_name}\"/, "add_index \"#{table_name}\"")
end

.copy_table_schema(connection, from_table_name, to_table_name) ⇒ Object



92
93
94
95
96
# File 'lib/active_record/model_spaces/table_manager.rb', line 92

def copy_table_schema(connection, from_table_name, to_table_name)
  from_table_schema = table_schema(connection, from_table_name)
  to_table_schema = change_table_name(from_table_name, to_table_name, from_table_schema)
  connection.instance_eval(to_table_schema)
end

.table_schema(connection, table_name) ⇒ Object

retrieve a schema.rb fragment pertaining to the table called table_name. uses a private Rails API



99
100
101
# File 'lib/active_record/model_spaces/table_manager.rb', line 99

def table_schema(connection, table_name)
  ActiveRecord::SchemaDumper.send(:new, connection).send(:table, table_name, StringIO.new).string
end