db:schema:load vs db:migrate with capistrano

前端 未结 4 1880
说谎
说谎 2021-01-31 18:54

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 u

4条回答
  •  孤独总比滥情好
    2021-01-31 19:30

    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.

    How to change the default behavior in Capistrano

    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
    

    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
    

提交回复
热议问题