I am working on a multi-database Rails 3 application. Each database has a different schema (and in production are located in different locations). I've set the app to talk to different databases like so:
我正在研究多数据库Rails 3应用程序。每个数据库都有不同的模式(并且生产中位于不同的位置)。我已经将应用程序设置为与不同的数据库通信,如下所示:
database.yml
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: main_development
pool: 5
username: someuser
password: somepassword
socket: /tmp/mysql.sock
other_development:
adapter: mysql2
encoding: utf8
reconnect: false
database: other_development
pool: 5
username: someuser
password: somepassword
socket: /tmp/mysql.sock
models/other_base.rb
class OtherBase < ActiveRecord::Base
self.abstract_class = true
establish_connection "other_#{Rails.env}"
end
models/some_model.rb
class SomeModel < OtherBase
# Regular stuff here
end
Now, this works fine for web app, but not so well for running rake tasks, including tests (fixtures aren't loaded correctly). Is there a gem available for this? Any help appreciated.
现在,这适用于Web应用程序,但不太适合运行rake任务,包括测试(夹具未正确加载)。有没有可用的宝石?任何帮助赞赏。
Also, it would be nice to create a schema.rb file that could handle the different schemas for different DBs - that is, would allow me to do things like rake db:create or db:setup and have it create multiple databases with the database-specific schema.
此外,创建一个可以处理不同数据库的不同模式的schema.rb文件会很好 - 也就是说,允许我执行rake db:create或db:setup之类的操作,并让它使用数据库创建多个数据库特定的架构。
4 个解决方案
#1
3
I find the use of environments a non acceptable hack in this case. You want to have two databases in the same environment.
在这种情况下,我发现使用环境是不可接受的黑客。您希望在同一环境中拥有两个数据库。
While I was researching that question myself I came across a gist of the github user rafaelchiti. Unfortunately he removed the original gist. You can find a copy of it here.
当我自己研究这个问题时,我遇到了github用户rafaelchiti的要点。不幸的是,他删除了原来的要点。你可以在这里找到它的副本。
#2
1
I had the same problem, and after spending half an hour looking for the 'loopy_multiple_database' plugin, eventually concluded that it had vanished off the face of the web and resorted to monkey-patching the Rails rake tasks. The only task I wanted to fix was db:migrate
, but the same (ugly) process would allow you to patch other tasks too.
我遇到了同样的问题,花了半个小时寻找'loopy_multiple_database'插件后,最终得出结论说它已经从网络上消失了,并采用猴子修补Rails rake任务。我想修复的唯一任务是db:migrate,但同样(丑陋)的过程也允许你修补其他任务。
On Rails 3, add create a file db_migrate_override.rake
(any *.rake name will do) in lib/tasks
like so:
在Rails 3上,在lib / tasks中添加创建文件db_migrate_override.rake(任何* .rake名称都可以),如下所示:
Rake::TaskManager.class_eval do
def remove_task(task_name)
@tasks.delete(task_name.to_s)
end
end
def remove_task(task_name)
Rake.application.remove_task(task_name)
end
namespace :db do
remove_task 'db:migrate'
desc "Migrate the database (options: VERSION=x, SRCDIR=path, VERBOSE=false)."
task :migrate => :environment do
srcdir = (ENV["SRCDIR"] || "db/migrate/")
ActiveRecord::Migrator.migrate(srcdir, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
Rake::Task["db:schema:dump"].invoke if (ActiveRecord::Base.schema_format == :ruby && ENV['SRCDIR'].nil?)
end
end
Then you can run
然后你就可以跑了
rake db:migrate RAILS_ENV=other_development SRCDIR=db_other/migrate
#3
1
You can try using octopus gem(https://github.com/thiagopradi/octopus), it helps you to setup multiple databases and is compatible with Rails 3.2 and Rails 4. With this gem you can easily specify the database you would like to run the migrations for.
您可以尝试使用章鱼宝石(https://github.com/thiagopradi/octopus),它可以帮助您设置多个数据库并与Rails 3.2和Rails 4兼容。使用此gem可以轻松指定您想要的数据库运行迁移。
#4
0
You can try loopy multiple databases plugin. It seems to allow for designating different rake configurations easily
您可以尝试循环多个数据库插件。它似乎允许轻松指定不同的rake配置
#1
3
I find the use of environments a non acceptable hack in this case. You want to have two databases in the same environment.
在这种情况下,我发现使用环境是不可接受的黑客。您希望在同一环境中拥有两个数据库。
While I was researching that question myself I came across a gist of the github user rafaelchiti. Unfortunately he removed the original gist. You can find a copy of it here.
当我自己研究这个问题时,我遇到了github用户rafaelchiti的要点。不幸的是,他删除了原来的要点。你可以在这里找到它的副本。
#2
1
I had the same problem, and after spending half an hour looking for the 'loopy_multiple_database' plugin, eventually concluded that it had vanished off the face of the web and resorted to monkey-patching the Rails rake tasks. The only task I wanted to fix was db:migrate
, but the same (ugly) process would allow you to patch other tasks too.
我遇到了同样的问题,花了半个小时寻找'loopy_multiple_database'插件后,最终得出结论说它已经从网络上消失了,并采用猴子修补Rails rake任务。我想修复的唯一任务是db:migrate,但同样(丑陋)的过程也允许你修补其他任务。
On Rails 3, add create a file db_migrate_override.rake
(any *.rake name will do) in lib/tasks
like so:
在Rails 3上,在lib / tasks中添加创建文件db_migrate_override.rake(任何* .rake名称都可以),如下所示:
Rake::TaskManager.class_eval do
def remove_task(task_name)
@tasks.delete(task_name.to_s)
end
end
def remove_task(task_name)
Rake.application.remove_task(task_name)
end
namespace :db do
remove_task 'db:migrate'
desc "Migrate the database (options: VERSION=x, SRCDIR=path, VERBOSE=false)."
task :migrate => :environment do
srcdir = (ENV["SRCDIR"] || "db/migrate/")
ActiveRecord::Migrator.migrate(srcdir, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
Rake::Task["db:schema:dump"].invoke if (ActiveRecord::Base.schema_format == :ruby && ENV['SRCDIR'].nil?)
end
end
Then you can run
然后你就可以跑了
rake db:migrate RAILS_ENV=other_development SRCDIR=db_other/migrate
#3
1
You can try using octopus gem(https://github.com/thiagopradi/octopus), it helps you to setup multiple databases and is compatible with Rails 3.2 and Rails 4. With this gem you can easily specify the database you would like to run the migrations for.
您可以尝试使用章鱼宝石(https://github.com/thiagopradi/octopus),它可以帮助您设置多个数据库并与Rails 3.2和Rails 4兼容。使用此gem可以轻松指定您想要的数据库运行迁移。
#4
0
You can try loopy multiple databases plugin. It seems to allow for designating different rake configurations easily
您可以尝试循环多个数据库插件。它似乎允许轻松指定不同的rake配置