Rake task to list all migrations for a given model
Asked Answered
E

1

6

On the terminal, is there a rake task to list all the migrations which have been run on a particular model? If not, how do I go about building one?

When I ran rake -T, rake db:migrate:status seemed to be the right answer, but it gave me Migration Name as one of the columns. And though the name Add logo to company does indicate Company model, not all migrations have such explicit names. Case in point being Change data type for content. I have 400 odd migration files, so this feature would be really helpful.

So, the ideal output would be:

database: abcd_development

 Status   Migration ID    Migration Name     Model Name
----------------------------------------------------------

Thanks!

Ecclesiasticism answered 12/9, 2013 at 11:4 Comment(0)
E
12

If you've been sticking with the migration naming conventions, you could just pass the output of rake db:migrate:status through grep:

rake db:migrate:status | grep 'compan'

This isn't perfect, though - migration names aren't required to have anything to do with what they actually do - a migration might add the column 'name' to the 'companies' table and be named EvacuateWeaselTubes and still run just fine.

If you wanted to build a task that could overcome this problem, it would have to parse each of the migration files to see what it changed. Since there's many ways to specify a change in a migration (add_column, a create_table block, or calling execute('CREATE whatever'), for instance), you'd probably want to search for mentions of Model.table_name, then check the schema_migrations table to see if it had been run.

Eufemiaeugen answered 12/9, 2013 at 14:31 Comment(1)
Thanks, and though the first method does get work done, I would still love a neat tabular format like I mentioned above. In spite of being a Rails newbie, I'm gonna try to build the rake task you mentioned. I'll keep you posted.Ecclesiasticism

© 2022 - 2024 — McMap. All rights reserved.