How to run the schema: load on the initial capistrano 3 application of my rails

advertisements

I would like to run db:schema:load in place of db:migrate on the initial deploy of my rails app.

This used to be fairly trivial, as seen in this stack overflow question, but in Capistrano 3, they have deprecated the deploy:cold task. The initial deploy isn't any different than all subsequent deploys.

Any suggestions? Thanks!


You'll have to define deploy:cold as basically a duplicate of the normal deploy task but with deploy:db_load_schema instead of deploy:migrations. For example:

desc 'Deploy app for first time'
task :cold do
  invoke 'deploy:starting'
  invoke 'deploy:started'
  invoke 'deploy:updating'
  invoke 'bundler:install'
  invoke 'deploy:db_load_schema' # This replaces deploy:migrations
  invoke 'deploy:compile_assets'
  invoke 'deploy:normalize_assets'
  invoke 'deploy:publishing'
  invoke 'deploy:published'
  invoke 'deploy:finishing'
  invoke 'deploy:finished'
end

desc 'Setup database'
task :db_load_schema do
  on roles(:db) do
    within release_path do
      with rails_env: (fetch(:rails_env) || fetch(:stage)) do
        execute :rake, 'db:schema:load'
      end
    end
  end
end

It might even be better to run the deploy:db_schema_load task independently, as the tasks included in the default deploy might change over time.

I actually using db:setup for fresh deploys because it seeds the database after creating tables:

desc 'Setup database'
task :db_setup do
  ...
        execute :rake, 'db:setup'
  ...
end