Question

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!

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top