Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Migration: indexes on a renamed table

I created a table and added an index to it. On a second migration I renamed the table. Will the index keep on working?

like image 726
Miotsu Avatar asked Sep 09 '13 14:09

Miotsu


People also ask

Can I rename a migration file Rails?

You can change the migration file name, but you have to perform a few steps: rake db:rollback to the point that queries table is rolled back. Now change the name of migration file, also the contents. Change the name of any Model that may use the table.

What is index in Rails migration?

An index is used to speed up the performance of queries on a database. Rails allows us to create index on a database column by means of a migration. By default, the sort order for the index is ascending. But consider the case where we are fetching reports from the database.

How does Rails know which migration to run?

Rails uses this timestamp to determine which migration should be run and in what order, so if you're copying a migration from another application or generate a file yourself, be aware of its position in the order. This generator can do much more than append a timestamp to the file name.


1 Answers

Rails 3

No, you'll need to take care of the indexes yourself since the index is based on the table name. For example:

remove_index :old_table_name, :column_name
rename_table :old_table_name, :new_table_name
add_index :new_table_name, :column_name

Rails 4+

From the Rails 4 upgrade guide:

In Rails 4.0 when a column or a table is renamed the related indexes are also renamed. If you have migrations which rename the indexes, they are no longer needed.

like image 63
pdobb Avatar answered Nov 07 '22 05:11

pdobb