views:

279

answers:

2

I have a table with a compound index that wasn't created through a rails migration. Now, I need to create a rails migration that will delete this index and create a new one, but I don't necessarily know what the name of the index will be.

I know that it is possible to get a list of table names and column names within a migration step. Is it possible to get a list of index names on a particular table? Or, looking at it another way, is it possible to delete all indexes on a table? Or is the only option to write my own database-specific SQL queries to get this info?

+1  A: 

You could get the info directly from the database. If you're using MySQL:

>> select TABLE_NAME, INDEX_NAME from information_schema.statistics WHERE TABLE_SCHEMA = 'your_database_name';

You only need to replace the *your_database_name* bit. You'll need priviledges for the information_schema database (or be logging in as root).

Delameko
+1  A: 

You can get details of all the indexes on a table with:

ActiveRecord::Base.connection.indexes('tablename')

This returns an array of ActiveRecord::ConnectionAdapters::IndexDefinition objects, each of which has a #name and #columns method.

showaltb