Yorkshireman
Yorkshireman

Reputation: 2343

Escaping hyphen in yaml key value for Travis CI

Trying to create the test database in the Travis CI build using the before_script command as specified here: https://docs.travis-ci.com/user/database-setup/#using-postgresql-in-your-builds

# .travis.yml

before_script:
  - bundle exec rake lint
  - "psql -c 'create database authentication-server_test;' -U postgres"

language: ruby

services:
  - postgresql

Travis throws with:

$ psql -c 'create database authentication-server_test;' -U postgres
ERROR:  syntax error at or near "-"
LINE 1: create database authentication-server_test;

I've done a fair bit of googling and having trouble working out how to escape that hyphen. The sticking point seems to be that I have to wrap the create database authentication-server_test; in quotes. Note that the example in the Travis docs has no hyphens.

Any ideas?

Upvotes: 1

Views: 1196

Answers (1)

mu is too short
mu is too short

Reputation: 434685

Your problem is that this PostgreSQL identifier:

authentication-server_test

needs to be quoted so that PostgreSQL won't try to interpret the - as an operator. Identifiers are quoted with double quotes so you need to this:

"authentication-server_test"

into the database. You could escape the double quotes in the YAML:

before_script:
  - bundle exec rake lint
  - "psql -c 'create database \"authentication-server_test\";' -U postgres"

or drop the outer double quotes (the YAML quotes for the string) while adding the inner double quotes (for the PostgreSQL identifier):

before_script:
  - bundle exec rake lint
  - psql -c 'create database "authentication-server_test";' -U postgres

or switch to createdb about avoid the problem altogether:

before_script:
  - bundle exec rake lint
  - createdb authentication-server_test -U postgres

by only having to worry about the shell's and YAML's quoting needs (neither of which apply here).

Upvotes: 1

Related Questions