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

前端 未结 20 1414
借酒劲吻你
借酒劲吻你 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 19:07

    You could use this version, which also supports rake db:rollback:

    class ChangeQuiz < ActiveRecord::Migration
      def connection
        ActiveRecord::Base.establish_connection("quiz_#{Rails.env}").connection
      end
    
      def reset_connection
        ActiveRecord::Base.establish_connection(Rails.env)
      end
    
      def up
        # make changes
    
        reset_connection
      end
    
      def self.down
        # reverse changes
    
        reset_connection
      end
    end
    

提交回复
热议问题