How to start the circuit: download the initial capistrano 3 application for my rails application

I would like to run db:schema:load instead of db:migrate on the initial deployment of my rails application.

This was pretty trivial, as shown in this stack overflow question , but in Capistrano 3 they did not recommend the deploy:cold task. The initial deployment is no different from all subsequent deployments.

Any suggestions? Thanks!

+8
ruby-on-rails capistrano capistrano3
source share
2 answers

You need to define deploy:cold as basically a duplicate of a typical deployment 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 yourself, since the tasks included in the default deploy value may change over time.

I really use db:setup for fresh deployments because it creates the database after creating the tables:

 desc 'Setup database' task :db_setup do ... execute :rake, 'db:setup' ... end 
+2
source share

I am also new to Capistrano and am trying to use it for the first time to deploy a Rails application to production servers configured using Puppet.

I finally had to break into the source of Capistrano (and capistrano / bundler, and capistrano / rails, and even sshkit and net-ssh to debug auth problems) to pinpoint how everything works before I felt that the trustee decide what changes I wanted to make. I just finished these changes and I am pleased with the results:

 # lib/capistrano/tasks/cold.rake namespace :deploy do desc "deploy app for the first time (expects pre-created but empty DB)" task :cold do before 'deploy:migrate', 'deploy:initdb' invoke 'deploy' end desc "initialize a brand-new database (db:schema:load, db:seed)" task :initdb do on primary :web do |host| within release_path do if test(:psql, 'portal_production -c "SELECT table_name FROM information_schema.tables WHERE table_schema=\'public\' AND table_type=\'BASE TABLE\';"|grep schema_migrations') puts '*** THE PRODUCTION DATABASE IS ALREADY INITIALIZED, YOU IDIOT! ***' else execute :rake, 'db:schema:load' execute :rake, 'db:seed' end end end end end 

The deploy: cold task simply captures my own deployment: the inidb task to run before deployment: migrate. Thus, the schema and seeds are loaded, and the deploy: migrate step that follows does nothing (safe), because there are no new migrations to run. As a safety measure, I check if the schema_migrations table exists before loading the schema if the deployment starts: it's cold again.

Note. I choose to create the database using Puppet, so I can avoid giving the CREATEDB privilege to my postgresql user for production, but if you want Capistrano to do this, just add "execute: rake, db: create", "before db: schema: load or replace all three lines with "db: setup".

+2
source share

All Articles