Uwem Uke
Uwem Uke

Reputation: 61

Travis CI: How do you configure travis.yml to connect to a PostgreSQL database?

On Travis, test returns "cannot read property 'statusCode' of undefined", because it can't find a database i guess. I have configured travis.yml to create a database and tables so Jasmine can test my endpoints. The travis doc https://docs.travis-ci.com/user/database-setup/ says "All services use default settings", so i didn't think i had to configure a database url.

Am i wrong? Do i need to specify a database url? if yes, then what key=value naming convention might i add in travis.yml?

Travis message

language: node_js
    node_js: 
      - "stable"
    env:
      - NODE_ENV="test"
    services:
      - postgresql
    install:
      - npm ci
    cache:
      directories:
      - "$HOME/.npm"
    before_script:
      - psql -c 'create database travis_ci_test;' -U postgres
      - psql -f './testdb/teamwork.sql -d travis_ci_test;' -U postgres
    script:
      - npm test

Upvotes: 0

Views: 1031

Answers (2)

Sam Odum
Sam Odum

Reputation: 26

You're right. Travis expects to have your test database but you aren't creating one. You're merely supplying a database name and password, but not the schema or form the database tables take. I usually tell Travis exactly how to build my database tables by first creating a dump file of my production database and then telling it to build my test database from that dump file. Here's a link that goes over the nuts and bolts of creating a dump file off your production db. Here's how your travis.yml should look like after all is said and done.

/* ./travis.yml */

language: node_js
node_js:
  - 'node'
branches:
  only:
    - develop
    - /^greenkeeper.*$/
services:
  - postgresql
addons:
  postgresql: '12'
  apt:
    packages:
      - postgresql-12
      - postgresql-client-12
env:
  global:
    - PGPORT=5433
    - DB_NAME=testdbname
    - DB_USER=dbowner
    - DB_PASSWORD=dbpassword
before_script:
  - psql --command="CREATE USER ${DB_USER};"
  - psql --command="CREATE DATABASE ${DB_NAME} WITH OWNER = ${DB_USER};"
  - psql --dbname=$DB_NAME --file=sql/create-tables.sql
install:
  - npm install
cache:
  directories:
    - 'node_modules'

Upvotes: 1

dinakajoy
dinakajoy

Reputation: 124

Try it with db_user details this time around

language: node_js
node_js: 
  - "stable"
env:
  - NODE_ENV="test"
services:
  - postgresql
install:
  - npm ci
cache:
  directories:
  - "$HOME/.npm"
before_script: 
  - psql -c 'create database travis_ci_test;' -U postgres
  - psql -c "CREATE USER db_username WITH PASSWORD 'db_password';" -U postgres
script:
  - npm test

Upvotes: 0

Related Questions