2

由于 Ryan Bates 部署到 Railscasts 上的 VPS 剧集,我在 VPS 上启动并运行了一个演示 Rails 应用程序。在我的服务器上,我创建了一个这样的目录结构/home/username/apps,并将应用程序部署到/home/username/apps/appname.应用程序名称文件夹内是由 Ryan 的脚本创建的另外三个文件夹(我想)

current  releases  shared

文件夹内current是通常的 Rails 目录。如果我 cd 进入current文件夹并运行rails c,或者rake db:seed我得到错误

 configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)

从共享目录触发

 apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/connection_adapters

我发现这是一个不寻常的错误,因为演示应用程序(它是脚手架)正在保存新创建的条目并将它们列出来,所以我假设我尝试运行 rake 和 rails 控制台命令的方式有问题,而不是我对适配器的声明有问题。

你能告诉我它可能是什么吗?

请注意,已经有一个问题数据库配置未指定处理此一般主题的适配器,但是,该问题的答案似乎处理数据库根本不工作的情况,但是,在我的情况下,这似乎不是案子。因此,我将这个问题与那个线程区分开来。

这是我的 database.yml 文件

production:
  adapter:  postgresql
  encoding:  unicode
  database:  dodeploy_production
  pool:  5
  host:  localhost
  username:  michael
  password:  secretpassword

这是 Ryan 用来部署的脚本。我将其包括在内,因为也许这里有一些信息可以帮助您理解我缺少的东西。

require "bundler/capistrano"

server "198XXXX", :web, :app, :db, primary: true

set :application, "dodeploy"
set :user, "michael"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false

set :scm, "git"
set :repository, "git@github.com:Username/appname.git"
set :branch, "master"

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

after "deploy", "deploy:cleanup" # keep only the last 5 releases

namespace :deploy do
  %w[start stop restart].each do |command|
    desc "#{command} unicorn server"
    task command, roles: :app, except: {no_release: true} do
      run "/etc/init.d/unicorn_#{application} #{command}"
    end
  end

  task :setup_config, roles: :app do
    sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
    sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
    run "mkdir -p #{shared_path}/config"
    put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
    puts "Now edit the config files in #{shared_path}."
  end
  after "deploy:setup", "deploy:setup_config"

  task :symlink_config, roles: :app do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
  end
  after "deploy:finalize_update", "deploy:symlink_config"

  desc "Make sure local git is in sync with remote."
  task :check_revision, roles: :web do
    unless `git rev-parse HEAD` == `git rev-parse origin/master`
      puts "WARNING: HEAD is not the same as origin/master"
      puts "Run `git push` to sync changes."
      exit
    end
  end
  before "deploy", "deploy:check_revision"
end

错误

当我运行rails c(或rake db:seed)时,我得到了这个

home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/connection_adapters/connection_specification.rb:52:in `resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/connection_adapters/connection_specification.rb:46:in `resolve_string_connection'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/connection_adapters/connection_specification.rb:30:in `spec'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/connection_handling.rb:39:in `establish_connection'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/railtie.rb:170:in `block (2 levels) in <class:Railtie>'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/lazy_load_hooks.rb:44:in `each'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/base.rb:322:in `<module:ActiveRecord>'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/base.rb:22:in `<top (required)>'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/dependencies.rb:228:in `require'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/dependencies.rb:228:in `block in require'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/dependencies.rb:213:in `load_dependency'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/dependencies.rb:228:in `require'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/railtie.rb:61:in `block in <class:Railtie>'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/railtie.rb:188:in `call'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/railtie.rb:188:in `block in run_console_blocks'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/railtie.rb:188:in `each'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/railtie.rb:188:in `run_console_blocks'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/application.rb:264:in `block in run_console_blocks'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/engine/railties.rb:17:in `each'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/engine/railties.rb:17:in `each'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/application.rb:264:in `run_console_blocks'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/engine.rb:431:in `load_console'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/commands/console.rb:51:in `initialize'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/commands/console.rb:9:in `new'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/commands/console.rb:9:in `start'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/commands.rb:66:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

更新

我不确定 Ryan 为什么在 Railscast 中创建 database.example.yml。他没有配置它,我只是在添加用户名和密码之前将其作为原始 database.yml 文件的副本保留。

数据库.example.yml

development:
  adapter: postgresql
  encoding: unicode
  database: dodeploy_development
  pool: 5
  password:
test:
  adapter: postgresql
  encoding: unicode
  database: dodeploy_test
  pool: 5
  password:

production:
  adapter: postgresql
  encoding: unicode
  database: dodeploy_production
  pool: 5
  password:

这是 nginx.conf 文件

upstream unicorn {
  server unix:/tmp/unicorn.dodeploy.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  # server_name example.com;
  root /home/michael/apps/dodeploy/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

更新。

这是来自 connection_specification.rb 的方法

     def resolve_hash_connection(spec) # :nodoc:
          spec = spec.symbolize_keys

          raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)

          path_to_adapter = "active_record/connection_adapters/#{spec[:adapter]}_adapter"
          begin
            require path_to_adapter
          rescue Gem::LoadError => e
            raise Gem::LoadError, "Specified '#{spec[:adapter]}' for database adapter, but the gem is not loaded. Add `$
          rescue LoadError => e
 raise LoadError, "Could not load '#{path_to_adapter}'. Make sure that the adapter in config/database.yml is$
          end

          adapter_method = "#{spec[:adapter]}_connection"

          ConnectionSpecification.new(spec, adapter_method)
        end
4

1 回答 1

4

我通过在运行命令时指定生产环境来让它工作

RAILS_ENV=production rails console

RAILS_ENV=production rake db:seed

我发现这很不寻常,因为该应用程序正在运行。

于 2013-06-15T14:42:54.877 回答