【问题标题】:Getting ActiveRecord::AdapterNotSpecified: 'old_production' database is not configured when trying to deploy rails application获取 ActiveRecord::AdapterNotSpecified: 'old_production' 数据库在尝试部署 rails 应用程序时未配置
【发布时间】:2016-05-09 23:52:32
【问题描述】:

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

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

convert.rake

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

class OldUser < OldActiveRecord
  ...
end

...

database.yml,它存在/链接到生产中的shared

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 中设置的,而 isshared 生产环境中。

问题

当我使用 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:in `建立连接' /

就在do bundle exec rake assets:precompile之后。

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

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

更新

当我在/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 中添加p configurations 时,它会说

{}

【问题讨论】:

    标签: mysql ruby-on-rails ruby-on-rails-4 activerecord capistrano3


    【解决方案1】:

    我不得不使用

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

    convert.rake的顶部为了使它工作,但实际上我仍然不知道为什么需要这样做。

    【讨论】:

      猜你喜欢
      • 2015-07-01
      • 1970-01-01
      • 2022-08-19
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-25
      相关资源
      最近更新 更多