如何在Rails中为自定义数据库rake任务动态设置schema_format?

Mat*_*lič 6 ruby-on-rails

我正在运行带有两个不同数据库的Rails 4.2.5应用程序.第一个,我们称之为db_main,使用sql模式格式(因为这个DB有一些ruby模式无法处理的数据库结构).我通过把实现此config.active_record.schema_format = :sqlapplication.rb.

现在,还有第二个DB,我们称之为db_other.这个DB有一个ruby模式,但问题是它的db任务需要一个sql模式.我db为此数据库定义了所有相关任务,如下所示:

namespace :other do
  task :set_custom_db_config_paths do
    ENV['SCHEMA'] = 'db_other/schema.rb'
    Rails.application.config.paths['db'] = ['other']
    Rails.application.config.paths['db/migrate'] = ['db_other/migrate']
    Rails.application.config.paths['config/database'] = ['config/database_other.yml']
 end

 namespace :db do
   task :migrate => :set_custom_db_config_paths do
   Rake::Task["db:migrate"].invoke
 end

 ... other tasks

 namespace :test do
  task :prepare => :set_custom_db_config_paths do
    # Tried setting ActiveRecord::Base.schema_format = :ruby here, but problem remains
      Rake::Task["db:test:prepare"].invoke
     end
   end
  end
end
Run Code Online (Sandbox Code Playgroud)

通话时rake other:test:prepare,会发生以下情况

`psql:db_other/schema.rb:135:错误:语法错误在"#"`或附近

我如何配置以便rake other:test:prepare采用ruby模式而不是SQL模式?

Tam*_*ash 2

尝试这个:

  task :set_custom_db_config_paths do
    ENV['SCHEMA'] = 'db_other/schema.rb'
    Rails.application.config.paths['db'] = ['other']
    Rails.application.config.paths['db/migrate'] = ['db_other/migrate']
    Rails.application.config.paths['config/database'] = ['config/database_other.yml']
    Rails.application.config.active_record.schema_format = :ruby
  end
Run Code Online (Sandbox Code Playgroud)

注意最后一行设置Rails.application.config.active_record.schema_format = :ruby