本文介绍了db:schema:load vs db:使用capistrano迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个rails应用程序,我移动到另一个服务器,我想我应该使用db:schema:load创建mysql数据库,因为它是建议。我的问题是,我使用capistrano部署,它似乎是默认的rake db:migrate。有没有办法改变这个或者是capistrano使用db:migrate有很好的理由?

I have a rails app that I'm moving to another server and I figure I should use db:schema:load to create the mysql database because it's recommended. My problem is that I'm using capistrano to deploy and it seems to be defaulting to rake db:migrate instead. Is there a way to change this or is capistrano using db:migrate for a good reason?

推荐答案

:load



我发现我自己的迁移最终会做一些数据混洗(假设我将first_name和last_name列合并成一个full_name列)。一旦我做任何这些,我开始使用ActiveRecord筛选数据库记录,你的模型最终做出关于某些列的假设。例如,我的Person表,后来被赋予了一个位置列,人们被排序。

Why to use db:schema:load

I find that my own migrations eventually do some shuffling of data (suppose I combine first_name and last_name columns into a full_name column, for instance). As soon as I do any of this, I start using ActiveRecord to sift through database records, and your models eventually make assumptions about certain columns. My "Person" table, for instance, was later given a "position" column by which people are sorted. Earlier migrations now fail to select data, because the "position" column doesn't exist yet.

总之,我相信 deploy:cold 应该使用 db:schema:load 而不是 db:migrate 。我通过改变中间的步骤解决这个问题,Capistrano在冷部署执行。对于Capistrano v2.5.9,库代码中的默认任务看起来像这样。

In conclusion, I believe deploy:cold should use db:schema:load instead of db:migrate. I solved this problem by changing the middle step which Capistrano performs on a cold deploy. For Capistrano v2.5.9, the default task in the library code looks like this.

namespace :deploy do
  ...
  task :cold do
    update
    migrate  # This step performs `rake db:migrate`.
    start
  end
  ...
end

我重写了我的 deploy.rb 中的任务如下。

I overrode the task in my deploy.rb as follows.

namespace :deploy do
  task :cold do       # Overriding the default deploy:cold
    update
    load_schema       # My own step, replacing migrations.
    start
  end

  task :load_schema, :roles => :app do
    run "cd #{current_path}; rake db:schema:load"
  end
end

这篇关于db:schema:load vs db:使用capistrano迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 04:55