问题描述
我有一个 Rails 项目正在运行,它在 config/database.yml 中定义了标准生产:、:开发和:测试数据库连接
I have a rails project running that defines the standard production:, :development and :test DB-connections in config/database.yml
此外,我还有一个 quiz_development: 和 quiz_production: 定义指向不同的主机/数据库/用户/密码
In addition I have a quiz_development: and quiz_production: definition pointing to a differnet host/db/user/password
我现在的目标是定义一个使用quiz_#{RAILS_ENV
}`"作为其数据库配置的迁移.
My goal now is to define a Migration that uses "quiz_#{RAILS_ENV
}`" as its database configuration.
我已经尝试过(但失败了):
What I have tried (and failed):
- 在迁移文件中设置 ActiveRecord::Base.connection
- 更改 rails 中的 db:migrate 任务以在此处设置 ActiveRecord::Base.connection
问题:
如何让 rake db:migrate 使用其他数据库定义?
How can I make rake db:migrate use that other database definition?
谢谢,弗兰克
推荐答案
有点晚了,但是我今天正在处理这个问题,我想出了这个自定义 rake 任务:
A bit late, but I was dealing with this problem today and I came up with this custom rake task:
namespace :db do
desc "Apply db tasks in custom databases, for example rake db:alter[db:migrate,test-es] applies db:migrate on the database defined as test-es in databases.yml"
task :alter, [:task,:database] => [:environment] do |t, args|
require 'activerecord'
puts "Applying #{args.task} on #{args.database}"
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[args.database])
Rake::Task[args.task].invoke
end
end
这篇关于在与标准“生产"不同的数据库上使用 Rails Migration或“发展"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!