db:schema:load vs db:migrate with capistrano

前端 未结 4 1950
说谎
说谎 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:47

    Climbing up on the shoulders of Andres Jaan Tack, Adam Spiers, and Kamiel Wanrooij, I've built the following task to overwrite deploy:cold.

    task :cold do
      transaction do
        update
        setup_db  #replacing migrate in original
        start
      end
    end
    
    task :setup_db, :roles => :app do
      raise RuntimeError.new('db:setup aborted!') unless Capistrano::CLI.ui.ask("About to `rake db:setup`. Are you sure to wipe the entire database (anything other than 'yes' aborts):") == 'yes'
      run "cd #{current_path}; bundle exec rake db:setup RAILS_ENV=#{rails_env}"
    end
    

    My enhancements here are...

    • wrap it in transaction do, so that Capistrano will do a proper rollback after aborting.
    • doing db:setup instead of db:schema:load, so that if the database doesn't already exist, it will be created before loading the schema.

提交回复
热议问题