【问题标题】:Capistrano deploy removes tableCapistrano 部署删除表
【发布时间】:2014-11-04 13:36:45
【问题描述】:

我使用 capistrano 将我的 Rails 应用程序部署到我的 VPS,但每次部署后,当我访问我的页面时,我都会收到错误消息。日志说:

I, [2014-11-04T08:20:16.659289 #12482]  INFO -- : Started GET "/" for 82.73.170.71 at 2014-11-04 08:20:16 -0500
I, [2014-11-04T08:20:16.662717 #12482]  INFO -- : Processing by HomeController#index as HTML
I, [2014-11-04T08:20:16.665979 #12482]  INFO -- : Completed 500 Internal Server Error in 3ms
F, [2014-11-04T08:20:16.670152 #12482] FATAL -- :
ActiveRecord::StatementInvalid (Could not find table 'users'):
  app/controllers/application_controller.rb:18:in `current_user'
  app/helpers/sessions_helper.rb:26:in `logged_in?'
  app/controllers/home_controller.rb:4:in `index'

我必须通过 ssh 进入我的 VPS 并转到我的 Rails 根目录并运行 RAILS_ENV=production bundle exec rake db:migrate 。在我的 db 文件夹中,我仍然有 production.sqlite3 文件,但它是空的。

我的deploy.rb

# config valid only for Capistrano 3.1
lock '3.1.0'

set :application, 'movieseat'
set :repo_url, 'git@github.com:alucardu/movieseat.git'

set :deploy_to, '/home/deploy/movieseat'

set :linked_files, %w{config/database.yml config/secrets.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

require 'capistrano-rbenv'

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  after :publishing, 'deploy:restart'
  after :finishing, 'deploy:cleanup'
end

那么为什么 Capistrano 会在我部署时删除我的数据库?

【问题讨论】:

    标签: ruby-on-rails deployment capistrano


    【解决方案1】:

    Capistrano 不会涉及数据库迁移,除非它由您的Capfile 中的deploy:migrate 任务或通过调用bundle exec cap deploy:migrate 指定。

    您的数据库“消失”,因为 SQLite 只是您的 db 目录中的一个文件。由于您没有指定它应该在版本之间共享(在shared 目录中),因此它会消失并保留在以前的版本中。将db/production.sqlite3 添加到您的linked_files 声明中。

    【讨论】:

    • 你是对的。我刚刚检查了部署的输出,在rake migraterake create 中找不到任何东西。但它发生在部署之后,所以出了点问题。
    • 有意义,但是当我现在运行它时,我得到了明显的错误 /home/deploy/movieseat/shared/db/production.sqlite3 does not excist 。但是仅仅将 sqlite 文件放在那里不会有太大的作用,因为应用程序没有引用使用共享的 sqlite 文件而不是 db 文件夹中的 on。
    • 通过将一些文件添加到linked_files,Capistrano 会创建一个指向该文件的符号链接。你必须在那里手动创建它,我的意思是/home/deploy/movieseat/shared/db/production.sqlite3。为此,我将进入您的 current 版本,然后是 RAILS_ENV=production rake db:create,然后是 cd .. && mkdir -p shared/db && cp current/db/production.sqlite3 shared/db/production.sqlite3
    • 它工作得很好,但我还没有完全理解它。因为production.sqlite3linked_files 中,它的优先级高于root/db 文件夹中的production.sqlite3 文件?因为所有数据现在也都在 shared/db/production.sqlite3 文件中。
    • 你需要知道 Unix 符号链接是如何工作的。符号链接是原始文件的一种快捷方式。请注意您的current 目录也是一个符号链接,它指向您的releases 目录中的最新版本。您可以通过cd /home/deploy/movieseat/ && ls -la查看。 current 的结果将类似于 current -> /home/deploy/movieseat/releases/20141028175151。这同样适用于linked_fileslinked_dirs 中指定的所有文件和目录,您也可以用同样的方式确认。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 2016-03-08
    • 2014-07-07
    • 2012-07-19
    相关资源
    最近更新 更多