In rails 3.2, adding a connection method to your migration does NOT work. So all the answers are kind of
def connection @connection ||= ActiveRecord::Base.establish_connection end
just doesn't work (can't down , doesn't work with change , lost connection, etc.). The reason for this is because the ActiveRecord :: Migration and Migrator classes have hardcoded connections to ActiveRecord :: Base all through place .
Fortunately, this post pointed me to this ticket , which has a good solution, namely redefining the actual rake task .
In the end, I used a slightly different rake task so that I could be specific regarding the migration I was running in a different database (we tried to support several versions of db):
Here is my lib / task / database.rake
# Augment the main migration to migrate your engine, too. task 'db:migrate', 'nine_four:db:migrate' namespace :nine_four do namespace :db do desc 'Migrates the 9.4 database' task :migrate => :environment do with_engine_connection do ActiveRecord::Migrator.migrate("#{File.dirname(__FILE__)}/../../nine_four/migrate", ENV['VERSION'].try(:to_i)) end end end end
This allows us to transfer migrations into one database into our own subdirectory (nine_four / migrations instead of db / migrations). It also gives a general isolation of each database in terms of their schemas and migration versions. The only drawback is running two rake tasks (db: migrate and nine_four: db: migrate).
Ryan Feb 13 '15 at 17:20 2015-02-13 17:20
source share