具有不同模式的多个数据库的 Rake 任务

发布于 2024-10-18 16:35:17 字数 988 浏览 2 评论 0原文

我正在开发一个多数据库 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

现在,这对于 Web 应用程序来说效果很好,但对于运行 rake 任务(包括测试)来说不太好(固定装置是未正确加载)。有可用的宝石吗?任何帮助表示赞赏。

另外,创建一个能够处理不同数据库的不同模式的 schema.rb 文件会很好 - 也就是说,允许我执行诸如 rake db:create 或 db:setup 之类的操作,并让它使用数据库创建多个数据库-特定模式。

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:

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一杯敬自由 2024-10-25 16:35:17

我发现在这种情况下使用环境是不可接受的。您希望在同一环境中拥有两个数据库。

当我自己研究这个问题时,我发现了 github 用户 rafaelchiti 的要点。不幸的是他删除了原来的要点。您可以此处找到它的副本

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.

尾戒 2024-10-25 16:35:17

我遇到了同样的问题,花了半个小时寻找“loopy_multiple_database”插件,最终得出结论,它已经从网络上消失了,并求助于 猴子修补 Rails rake 任务。我想要修复的唯一任务是 db:migrate,但相同的(丑陋的)过程也允许您修补其他任务。

在 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

然后您可以运行

rake db:migrate RAILS_ENV=other_development SRCDIR=db_other/migrate

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.

On Rails 3, add create a file db_migrate_override.rake (any *.rake name will do) in lib/tasks like so:

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
月依秋水 2024-10-25 16:35:17

您可以尝试使用 octopus gem(https://github.com/thiagopradi/octopus),它可以帮助您设置多个数据库,并与 Rails 3.2 和 Rails 4 兼容。
使用此 gem,您可以轻松指定要为其运行迁移的数据库。

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.

ζ澈沫 2024-10-25 16:35:17

您可以尝试循环多个数据库插件。它似乎允许轻松指定不同的耙子配置

You can try loopy multiple databases plugin. It seems to allow for designating different rake configurations easily

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文