Rake task for listing all migrations for a given model

Is there a rake task on the terminal to list all the migrations that were run in a particular model? If not, how do I do this?

When I ran rake -T , rake db:migrate:status seemed to be the correct answer, but it gave me the migration name as one of the columns. Although the Add logo to company name indicates the company model, not all migrations have such explicit names. The point is Change data type for content . I have 400 odd migration files, so this feature will be really useful.

Thus, the ideal solution would be:

 database: abcd_development Status Migration ID Migration Name Model Name ---------------------------------------------------------- 

Thanks!

+6
source share
1 answer

If you adhere to migration naming conventions, you can simply pass the output of rake db:migrate:status via grep :

 rake db:migrate:status | grep 'compan' 

This is not ideal, though - migration names should have nothing to do with what they actually do - migration can add a column name to the company table and be called EvacuateWeaselTubes and it still works just fine.

If you want to create a task that could overcome this problem, she would have to analyze each of the migration files to see what changed. Since there are many ways to indicate a change in migration ( add_column , create_table or call execute('CREATE whatever') , for example), you probably want to find references to Model.table_name , and then check the schema_migrations check box to see if it was executed .

+7
source

Source: https://habr.com/ru/post/953694/


All Articles