tpope / vim-rails

rails.vim: Ruby on Rails power tools
http://www.vim.org/scripts/script.php?script_id=1567
4.1k stars 383 forks source link

Multiple databases support for migrations #555

Closed teoljungberg closed 4 years ago

teoljungberg commented 4 years ago

I have a rails project that uses multiple databases, the rails.vim projections doesn't support these multiple databases since the file paths differ.

If a normal migration path like this db/migrate/TIMESTAMP_name_of_migration.rb a migration for another database in the multiple database setup would look like this db/NAME_OF_DATABASE_migrate/TIMESTAMP_name_of_another_migration.rb.

Rather than changing the existing regexes in rails.vim I thought I'd write my own projections, or set a custom function to dispatch.vim to call to determine what the command we pass to dispatch executes.

I started with the latter, but couldn't get it to work. Here's what I got so far, the other database in the app is called "apple" in the example:

function! AppleDispatch(count)
  let migration_version = matchstr(expand('%'), '\migrate/0*\zs\d*\ze_')

  if count > 0
    let migration_command = 'db:migrate:down:apple'
  else
    let migration_command = 'db:migrate:redo:apple'
  endif

  return '-dir=/PATH/TO/APP bin/rails ' . migration_command . ' VERSION=' . migration_version
endfunction

augroup NAME_OF_APP
  autocmd!

  autocmd BufReadPost *db/apple_migrate/* let b:dispatch = "`=AppleDispatch(exists('l#') ? l# : 0)`"
augroup END

The commands to migrate are suffixed by the name of the custom database, which also is the same as the name in the file path of each migration.

This issue falls between dispatch.vim and rails.vim, so I was unsure where to open the issue. If there is a neat way of adding support for multiple databases in rails.vim, it felt appropriate to open the issue here.

Help and pointers are appreciated!

tpope commented 4 years ago

For starters, you can't include -dir in an expansion like `=, and beyond that, `= in particular is deprecated (and I don't even think it works with l#, at least not intentionally). Try a b:dispatch like the rails.vim built-in, with -dir=... bin/rails %:s/.*/\=AppleDispatch(submatch(0), exists('l#') ? l# : 0)/.

teoljungberg commented 4 years ago

Great, thanks!