Abundnce10
Abundnce10

Reputation: 2210

Capistrano's `cap production deploy:migrate` won't work

I'm having trouble getting Capistrano to run my database migrations.

I'm using a VPS offered by DigitalOcean to host my Rails application. Previously I would use git push heroku master to host my side projects on Heroku but now I want something a little cheaper. I'm trying to use Capistrano to deploy my code to the server (using this tutorial to get up and running). I can successfully get my new commits onto the server, however, I can't get Capistrano to run my database migrations.

To showcase my problem I created a new model, committed the changes, pushed to the Github repo, and then ran cap production deploy. I looked on the server and I can see the new migration file. Here is the output from that command in case it's helpful in debugging the problem.

When I run cap production deploy:migrate nothing happens to the database:

~/Projects/rails/testapp $ cap production deploy:migrate
DEBUG[aec67347] Running /usr/bin/env [ -d ~/.rbenv/versions/2.1.3 ] on 104.236.181.65
DEBUG[aec67347] Command: [ -d ~/.rbenv/versions/2.1.3 ]
DEBUG[aec67347] Finished in 1.107 seconds with exit status 0 (successful).

And here's my PostgeSQL database on the production server (nothing changed):

testapp_production=> \d
               List of relations
 Schema |       Name        |   Type   | Owner  
--------+-------------------+----------+--------
 public | cars              | table    | deploy
 public | cars_id_seq       | sequence | deploy
 public | schema_migrations | table    | deploy
 public | users             | table    | deploy
 public | users_id_seq      | sequence | deploy
(5 rows)

Now, when I run RAILS_ENV=production bundle exec rake db:migrate on the server it successfully runs the migration, evidenced by the new manufacturers table:

testapp_production=> \d
                 List of relations
 Schema |         Name         |   Type   | Owner  
--------+----------------------+----------+--------
 public | cars                 | table    | deploy
 public | cars_id_seq          | sequence | deploy
 public | manufacturers        | table    | deploy
 public | manufacturers_id_seq | sequence | deploy
 public | schema_migrations    | table    | deploy
 public | users                | table    | deploy
 public | users_id_seq         | sequence | deploy
(7 rows)

How come cap production deploy:migrate won't run the database migration? Also, I was under the impression that cap production deploy would automatically run any new migrations, is this true?

My app's configurations can be found here: https://github.com/Abundnce10/testapp

Here is my Capfile:

require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/rbenv'

set :rbenv_type, :user # or :system, depends on your rbenv setup
set :rbenv_ruby, '2.1.3'

Here's my /config/deploy.rb file:

lock '3.1.0'
set :application, 'testapp'
set :repo_url, 'https://github.com/Abundnce10/testapp'
set :deploy_to, '/home/deploy/testapp'
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

namespace :deploy do

  before :publishing, 'deploy:migrate'

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  after :publishing, 'deploy:restart'
  after :finishing, 'deploy:cleanup'
end

Here's my /config/deploy/production.rb file:

set :stage, :production
server '104.236.181.65', user: 'deploy', roles: %w{web app}

Any help is much appreciated!

Upvotes: 1

Views: 2884

Answers (1)

Abundnce10
Abundnce10

Reputation: 2210

I didn't include db in my /config/deploy/production.rb file. It now looks like:

set :stage, :production
server '104.236.181.65', user: 'deploy', roles: %w{web app db}

Adding db to :roles allowed the database migrations to run automatically when I run cap production deploy.

Upvotes: 10

Related Questions