Using Rails Migration on different database than standard “production” or “development”

前端 未结 20 1387
借酒劲吻你
借酒劲吻你 2020-11-29 18:18

I have a rails project running that defines the standard production:, :development and :test DB-connections in config/database.yml

In addition I have a quiz_developm

20条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 18:46

    In addition to running a migration in a different environment, I also want the schemas in separate files. You can do this from the command line:

    RAILS_ENV=quiz_development SCHEMA=db/schema_quiz_development.rb rake db:migrate
    

    But I like the custom rake task approach so I can type this instead:

    rake db:with[quiz_development, db:migrate]
    

    Here's the rake task:

    namespace :db do
      desc "Run :task against :database"
      task :with, [:database,:task] => [:environment] do |t, args|
        puts "Applying #{args.task} to #{args.database}"
        ENV['SCHEMA'] ||= "#{Rails.root}/db/schema_#{args.database}.rb"
        begin
          oldRailsEnv = Rails.env
          Rails.env = args.database
          ActiveRecord::Base.establish_connection(args.database)
          Rake::Task[args.task].invoke
        ensure
          Rails.env = oldRailsEnv
        end
      end
    end
    

提交回复
热议问题