我有一个新的Rails 4.2应用程序。

为了执行从旧数据库到新数据库的迁移,我添加了一个rake任务,该任务定义了一些连接到旧数据库的OldXX类,将数据转换并将其保存到新数据库。该任务从开发中的localhost针对服务器上的旧数据库运行。

转换耙

class OldActiveRecord < ActiveRecord::Base
  self.abstract_class = true
  establish_connection "old_#{Rails.env}".to_sym
end

class OldUser < OldActiveRecord
  ...
end

...


不在生产中的/链接到shared的database.yml。

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5

production:
  <<: *default
  host: localhost
  database: "<%= ENV['DATABASE_NAME'] %>"
  username: "<%= ENV['DATABASE_USERNAME'] %>"
  password: "<%= ENV['DATABASE_PASSWORD'] %>"

old_production: &old_prod
  <<: *default
  host: my.old.dbhost
  database: "<%= ENV['OLD_DATABASE_NAME'] %>"
  username: "<%= ENV['OLD_DATABASE_USERNAME'] %>"
  password: "<%= ENV['OLD_DATABASE_PASSWORD'] %>"

old_development:
  <<: *old_prod

old_test:
  <<: *old_prod


和env vars在生产中的shared中的.env中设置。

问题

当我使用capistrano进行部署时,出现错误

(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as me@my.old.dbhost: rake exit status: 1
rake stderr: rake aborted!
ActiveRecord::AdapterNotSpecified: 'old_production' database is not configured. Available: []

/var/www/vhosts/domain.org/www.domain.org/shared/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:250:in `resolve_symbol_connection'
/var/www/vhosts/domain.org/www.domain.org/shared/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:211:in `resolve_connection'
/var/www/vhosts/domain.org/www.domain.org/shared/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:139:in `resolve'
/var/www/vhosts/domain.org/www.domain.org/shared/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:169:in `spec'


/var/www/vhosts/domain.org/www.domain.org/shared/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:50:位于“建立连接”中
/

do bundle exec rake assets:precompile之后。

p Rails.configuration.database_configuration之前添加establish_connection表示old_production的所有字段都已相应设置,包括adapter。也可以从新主机连接到mysql的旧主机,并在命令行上使用相同的凭据进行测试。

为什么会出现此错误以及如何解决?

更新资料

当我在p configurations顶部的/var/www/vhosts/domain.org/www.domain.org/shared/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:245中添加#resolve_symbol_connection时,它说

{}

最佳答案

我不得不用

ActiveRecord::Base.configurations = Rails.application.config.database_configuration


convert.rake的顶部以使其正常运行,但是实际上我仍然不知道为什么需要这样做。

关于mysql - 获得ActiveRecord::AdapterNotSpecified:尝试部署Rails应用程序时未配置'old_production'数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37127196/

10-14 14:08
查看更多