views:

20

answers:

2

Hello. Me and a colleage are working in different projects that share some models. So, we are sharing the models thru a git submodule.

Additionally, we'd like to be able to also share migrations:

In this way, my collegue's migrations would be in the folder db/migrate/other_db of my project.

How can I configure rails migrations to also run the migrations in this extra folder?

+1  A: 

I do not know of a very clean way to do it, but the code that runs migrations looks something like:

@migrations ||= begin                                                                                                                                            
        files = Dir["#{@migrations_path}/[0-9]*_*.rb"]                                                                                                                 

        migrations = files.inject([]) do |klasses, file|                                                                                                               
          version, name = file.scan(/([0-9]+)_([_a-z0-9]*).rb/).first 

Where,

@migrations_path = 'db/migrate'

So if you change this to read from config file instead, it may work out in your favour. But as I said, this is definitely not a very clean way to do it.

Swanand
A: 

based on the answer by Swanand, we can write a migration to load the migrations in an external dir:

class MigrateMetadata < ActiveRecord::Migration
  MIGRATIONS_PATH='db/migrate/metadata'
  def self.up
   Dir["#{MIGRATIONS_PATH}/[0-9]*_*.rb"].
   sort.map{|filename|require filename}.flatten.
   each{|class_name| const_get(class_name).up}
  end

  def self.down
    Dir["#{MIGRATIONS_PATH}/[0-9]*_*.rb"].sort.reverse.
    map{|filename|require filename}.flatten.
    each{|class_name| const_get(class_name).down}
  end
end
Pedro Morte Rolo