Thank you! Your feedback has been delivered
Thank you! Your feedback has been sent

Why Does First Deploy with Capistrano Run db:migrate?

I'm using Capistrano to deploy to production for the first time, and I'm getting an error when I run

cap production deploy

The error is:

** Invoke deploy:migrate (first_time)
** Invoke deploy:set_rails_env
** Execute deploy:migrate
DEBUG [048f89c6] Running /usr/bin/env if test ! -d /home/deployer/apps/apoxeia_production/releases/20140209005208; then echo "Directory does not exist '/home/deployer/apps/apoxeia_production/releases/20140209005208'" 1>&2; false; fi on
DEBUG [048f89c6] Command: if test ! -d /home/deployer/apps/apoxeia_production/releases/20140209005208; then echo "Directory does not exist '/home/deployer/apps/apoxeia_production/releases/20140209005208'" 1>&2; false; fi
DEBUG [048f89c6] Finished in 0.160 seconds with exit status 0 (successful).
INFO [52f75214] Running ~/.rbenv/bin/rbenv exec bundle exec rake db:migrate on
DEBUG [52f75214] Command: cd /home/deployer/apps/apoxeia_production/releases/20140209005208 && ( RBENV_ROOT=~/.rbenv RBENV_VERSION=2.1.0 RAILS_ENV=production ~/.rbenv/bin/rbenv exec bundle exec rake db:migrate )
DEBUG [52f75214]    rake aborted!
DEBUG [52f75214]    An error has occurred, this and all later migrations canceled:
DEBUG [52f75214]    
DEBUG [52f75214]    PG::UndefinedTable: ERROR:  relation "client_infos" does not exist
DEBUG [52f75214]    : ALTER TABLE "client_infos" RENAME TO "clients

The error actually makes complete sense. The referenced table doesn't exist. What I don't understand is why the migration is running at all? Why isn't the the database just being created from the schema on the first run. Did I inadvertently remove a file that says what migrations have been run? Either by just deleting it, or by ".gitignoring" it?

Note: This is an unanswered question from Stack Overflow posted by codenoob. We sponsor this question. Eligible answers for the bounty should be posted both in this thread and in the original post on Stack Overflow. The author of an answer that will be accepted on Stack Overflow will receive the bounty.

User Gravatar


Posted Feb 9 2014 6:21 UTC


  • Assigned To guyisra
  • Solved
  • ruby

3 Replies

Check your database.yml config, production part, if it is correct. Then ensure Postgresql server is running on the production server and the application (as a user) has access rights. I assume "deployer" is the user, so check this user has all the necessary rights.

User Gravatar


Posted Feb 9 2014 6:27 UTC

From the error it looks like you are trying to rename the client_infos table to clients but the client_infos table does not exist, but do you not have an earlier migration which created the client_infos table?

If you don't have a migration which creates the client_infos table, then where did it come from? Did you create it manually? All changes to the database should have an accompanying migration.

If you do have a migration for the client_infos table, the schema_migrations database table (which is where Rails keeps a record of what migrations have run) may of somehow got out of sync. Given you are deploying for the first time, its probably worth dropping the whole database and starting again.

What I don't understand is why the migration is running at all? Why isn't the the database just being created from the schema on the first run

If written correctly, the migrations should have the same effect as loading the schema, they will just do it incrementally. You can of course manually load the schema if you wish, but Capistrano will not do this as it is quite a dangerous thing to run (you likely never want to recreate your production database)

User Gravatar


Posted Feb 9 2014 6:36 UTC

db:migrate takes the migration files and executes them. so if a table doesn't exist, it will tell you that so. If your deploy is the first deploy to that machine, or that the DB configured is yet have been initialized you should do:

  1. Create DB

    rake db:create

this is will create all tables

2 Run the Migrations

 `rake db:migrate`

Here obviously, you need to have migrations in place.

It is highly recommended not to load a schema (unless you have no choice) since its hard to work on the schema after (rollbacking, etc) but if you have no choice you can do rake db:schema:load

see this for more info

User Gravatar


Posted Feb 9 2014 14:41 UTC


This didn't solve your task? Get your own custom solution.

Add a reply

By posting a reply on CodersClan you agree to our Terms & Conditions