获取ActiveRecord :: AdapterNotSpecified:尝试部署rails应用程序时未配置'old_production'数据库

时间:2016-05-09 23:52:33

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

我有一个新的rails 4.2 app。

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

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 vars在 .env 中设置,shared生产中的

问题

当我使用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` establish_connection” /

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时,它说

{}

1 个答案:

答案 0 :(得分:0)

我必须使用

public class SpringProfileDocumentMatcher implements DocumentMatcher, EnvironmentAware {

private static final String[] DEFAULT_PROFILES = new String[] {
        "^\\s*$"
};

private String[] activeProfiles = new String[0];

public SpringProfileDocumentMatcher() {
}

@Override
public void setEnvironment(Environment environment) {
    if (environment != null) {
        addActiveProfiles(environment.getActiveProfiles());
    }
}

public void addActiveProfiles(String... profiles) {
    LinkedHashSet<String> set = new LinkedHashSet<String>(Arrays.asList(this.activeProfiles));
    Collections.addAll(set, profiles);
    this.activeProfiles = set.toArray(new String[set.size()]);
}

@Override
public MatchStatus matches(Properties properties) {
    String[] profiles = this.activeProfiles;
    if (profiles.length == 0) {
        profiles = DEFAULT_PROFILES;
    }
    return new ArrayDocumentMatcher("spring.profiles", profiles).matches(properties);
}

位于Id的顶部以使其正常工作,但实际上我仍然不知道为什么需要这样做。