8

I'm having trouble getting Rails, postgres travis to work. Keep getting a database connection error when the tests start running.

Errno::ECONNREFUSED: Connection refused - connect(2)

.travis.yml

language: ruby
rvm:
  - "1.9.3"
before_script:
  - cp config/database.travis.yml config/database.yml
  - psql -c 'create database myapp_test;' -U postgres
  - bundle exec rake db:migrate --trace
  - bundle exec rake db:test:prepare --trace
script:
  - RAILS_ENV=test bundle exec rake spec

gemfile: Gemfile.ci

and database.travis.yml

test:
  adapter: postgresql
  database: myapp_test
  username: postgres

I have to use separate database config.

Any clue what I'm doing wrong? Following the documentation almost exactly in http://about.travis-ci.org/docs/user/database-setup/ except I have to copy database config over to the right place.

mehulkar
  • 4,895
  • 5
  • 35
  • 55

3 Answers3

2

Why are you doing the

bundle exec rake db:migrate
bundle exec rake db:test:prepare

The db:test:prepare is going to try and access the development database, which doesn't exist. And the rake db:migrate should be automatically run by Travis.

Peter Goldstein
  • 4,479
  • 2
  • 19
  • 17
  • close, both the above commands will be run for the dev environment. – rb512 Oct 15 '13 at 03:35
  • rake db:test:prepare does not access the development db I don't think. http://stackoverflow.com/a/15170024/986415 – mehulkar Oct 15 '13 at 03:38
  • Yeah, without running migrations, travis doesn't do anything. https://travis-ci.org/collegedesis/collegedesis.com/builds/12551777 – mehulkar Oct 15 '13 at 03:43
  • Travis runs in the test environment. rake db:test:prepare will attempt to dump the dev database and load the schema into the test environment. It shouldn't be necessary. Travis should run migrations directly against the test db. – Peter Goldstein Oct 15 '13 at 03:55
  • Just to be clear, run the bundle exec rake db:migrate, leave out the db:test:prepare – Peter Goldstein Oct 15 '13 at 04:02
  • @PeterGoldstein, nope https://travis-ci.org/collegedesis/collegedesis.com/builds/12578085 – mehulkar Oct 15 '13 at 17:15
1

The problem was that I needed to enable the elasticsearch service on travis. Adding records to the database requires indexing and the refused connection was to a nonexistent elasticsearch server.

mehulkar
  • 4,895
  • 5
  • 35
  • 55
  • Is there anywhere explained why you need an elasticsearch server? – Mario Uher Mar 22 '16 at 09:14
  • @MarioUher my test suite had tests that was expecting a server avaiable – mehulkar Mar 22 '16 at 19:51
  • Ah ok. Wasn't mentioned anywhere and the the official docs included a link to an elastic search package as well. So I was very confused. But thankful the support from Travis is really good, and helped me with my problem. – Mario Uher Mar 22 '16 at 19:54
0

Based on the doc, you should turn on the postgresql service first

services:
  - postgresql

And specify the DB type with (optional) :

    env:
      - DB=pgsql

NOTE: postgresql and postgres WILL NOT WORK. Please use pgsql !!!

Here is the full code that worked for me:

.travis.yml

language: ruby

rvm:
  - 2.2

env:
  - DB=pgsql

services:
  - postgresql

script:
  - RAILS_ENV=test bundle exec rake db:migrate --trace
  - bundle exec rake db:test:prepare
  - bundle exec rake

before_script:
  - cp config/database.yml.travis config/database.yml
  - psql -c 'create database courseselect_test;' -U postgres

config/database.yml.travis

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: courseselect_development

test:
  <<: *default
  database: courseselect_test

production:
  <<: *default
  database: courseselect_developement

BTW, I also have the file database.yml with the same content as config/database.yml.travis

zhaoqing
  • 775
  • 8
  • 8