0

Every time I have to run/redo a particular migration in my rails app, I have to go through the following steps to obtain the exact version number:

  1. grep - Search the files in the db/migrate folder containing the model/table name that I need, or
  2. locate - Similar to grep, I search for files containing the model/table name and pipe this with a grep to filter out db/migrate results, or
  3. ls - Use ls db/migrate/2xxx and keep using auto-complete until I narrow down the exact migration I am looking for

I want to know if there is an easier way of doing this. That is, a plugin that might suggest version numbers when I begin entering rake db:migrate:redo VERSION=

Is there any bash/rails plugin that would do this?

sridharraman
  • 365
  • 1
  • 4
  • 16

2 Answers2

0

You can do it like this. Without specifying version number:

Edit: I have updated list of all you could use. I am not aware of plugins, but this might be helpful as well.

rake db:rollback
rake db:rollback STEP=3
rake db:migrate:down

rake db:migrate:redo
rake db:migrate:redo STEP=3

And it will go 3 steps back. I will think of other options , and if I find, I will post the answer here.

Also, there is another way. But if you want something like plugin, let me know what editor you are using. It could be usefull if you are using sublime. You have an plugin for that. At least to find migrations https://github.com/KELiON/RailsMigrationsList

Aleks
  • 4,866
  • 3
  • 38
  • 69
  • I am using gedit. I am able to open the migration file in my editor, so that's never a problem. But if I need to redo a particular version, getting that version number is tedious. For example, as I mentioned in the question, if I need to redo a particular migration, the only way is to get the version number - 20100727200521 - by the process I had mentioned. I was wondering if there is an easier way of getting this number. – sridharraman Mar 13 '13 at 11:37
  • ok, I understand what you mean now. Ok, then a question: how would you describe how do you know you are searching for version 20100727200521 and not for version 20100727200522? I would be able to suggest bach, but I need answer how exactly you determine the version you need. I understand how you are searching, but could you describe me how you know what version you need? – Aleks Mar 13 '13 at 13:38
  • A typical use-case is that I am forced to modify the migration due to some changes from the client. So, I modify the migration XXXX_create_articles (for example). In the editor, it's easy to get this migration file by searching for "articles". So when I open the file, I look at the file name and keep switching between editor and terminal and type in the version number (from the filename). – sridharraman Mar 25 '13 at 06:16
0

This is not a solution but it's a first step and it saves a little time. It lists the migrations when you press tab, then you must copy and paste. It uses the trick of defining functions to alias the db:migrate tasks.

Type migrate_down or migrate_up and press TAB and see what's happen.

function _migrations_complete() {
  local migrations=`ls db/migrate/*rb | cut -b12-`
  COMPREPLY=($(compgen -W "${migrations}" -- ${COMP_WORDS[COMP_CWORD]}))
  return 0
}
function migrate_up() {
  bundle exec rake db:migrate:up VERSION=$1
}
function migrate_down() {
  bundle exec rake db:migrate:down VERSION=$1
}
complete -o default -o nospace -F _migrations_complete migrate_up
complete -o default -o nospace -F _migrations_complete migrate_down

What I'd like to do would be completing on the name of the migration and getting the timestamp. I don't know complete well enough to assess if it's possible.

By the way, this https://github.com/jweslley/rails_completion adds completions for many rails commands but not for migration versions.

pmontrasio
  • 541
  • 6
  • 10