# Spawn fewer services for Multiple database ruby gem tests

**URL:** <https://travis-ci.community/t/spawn-fewer-services-for-multiple-database-ruby-gem-tests/8717>\
**Category:** Travis CI Discussions & Feedback\
**Tags:** travis-build\
**Created:** [May 21, 2020, 3:55am UTC](https://travis-ci.community/t/spawn-fewer-services-for-multiple-database-ruby-gem-tests/8717 "2020-05-21T03:55:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kbrock](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/kbrock/32/4181_2.png) [@kbrock](https://travis-ci.community/u/kbrock)\
**Post date:** [May 21, 2020, 3:55am UTC](https://travis-ci.community/t/spawn-fewer-services-for-multiple-database-ruby-gem-tests/8717/1 "2020-05-21T03:55:29Z")

</div>

# Background

I have a few ruby gems that run tests for a few databases. They all follow the same pattern.

> <https://github.com/kbrock/activerecord-hash_options/blob/master/.travis.yml>

# Now

I start up `mysql` and `postgres` services for all test matrix entries.

# Request

I would like to find a way to only run the services that I need.

using `service` works for mysql and postgres. using `systemctl` works for mysql but not for postgres.

Anyone with an idea how I can only spawn the necessary service?

Thanks for the great service,  
Keenan

## Current

```auto
---
sudo: false
language: ruby
cache: bundler

rvm:
  - 2.5.5

services:
  - mysql
  - postgresql

env:
  - DB=mysql2
  - DB=pg
  - DB=sqlite3

gemfile:
  - gemfiles/gemfile_50.gemfile
  - gemfiles/gemfile_51.gemfile
  - gemfiles/gemfile_52.gemfile
  - gemfiles/gemfile_60.gemfile

before_script:
  - [$DB = "mysql2"] && mysql -e 'create database hash_options_test;' || true
  - [$DB = "pg"] && psql -c 'create database hash_options_test;' -U postgres || true

```

## Failure A

The current travis scripts call the command `travis_setup_postgresql && postgresql@9.6-main` but they are not availble to me from `before_script1`. I would like to use what ever is defined in the `services:` list.

```auto
---
sudo: false
language: ruby
cache: bundler

rvm:
  - 2.5.3

env:
  - DB=mysql2
  - DB=pg
  - DB=sqlite3

gemfile:
  - gemfiles/gemfile_50.gemfile
  - gemfiles/gemfile_51.gemfile
  - gemfiles/gemfile_52.gemfile
  - gemfiles/gemfile_60.gemfile

before_script:
  - sh -c "['$DB' = 'mysql2'] && sudo systemctl start mysql && mysql -e 'create database hash_options_test;' || true"
  - sh -c "['$DB' = 'pg'] && sudo systemctl start postgresql && psql -c 'create database hash_options_test;' -U postgres || true"

```

## Failure 2

Tried to get services linked into the test matrix. under the `env:` section seems to make the most sense to me.

```auto
---
sudo: false
language: ruby
cache: bundler

rvm:
  - 2.5.3

env:
  - DB=mysql2
    services: mysql
  - DB=pg
    services: postgres
  - DB=sqlite3

gemfile:
  - gemfiles/gemfile_50.gemfile
  - gemfiles/gemfile_51.gemfile
  - gemfiles/gemfile_52.gemfile
  - gemfiles/gemfile_60.gemfile

before_script:
  - sh -c "['$DB' = 'mysql2'] && mysql -e 'create database hash_options_test;' || true"
  - sh -c "['$DB' = 'pg'] && psql -c 'create database hash_options_test;' -U postgres || true"

```

---

<div class="post-metadata">

**Author:** ![Montana](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/montana/32/9566_2.png) [@Montana](https://travis-ci.community/u/Montana)\
**Post date:** [May 27, 2020, 7:55am UTC](https://travis-ci.community/t/spawn-fewer-services-for-multiple-database-ruby-gem-tests/8717/2 "2020-05-27T07:55:28Z")

</div>

Have you had a peek at this?

> **[Travis CI Documentation](https://docs.travis-ci.com/user/database-setup/#using-a-different-postgresql-version)**
>
> Setting up Databases and Services

---

<div class="post-metadata">

**Author:** ![kbrock](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/kbrock/32/4181_2.png) [@kbrock](https://travis-ci.community/u/kbrock)\
**Post date:** [May 28, 2020, 9:55pm UTC](https://travis-ci.community/t/spawn-fewer-services-for-multiple-database-ruby-gem-tests/8717/3 "2020-05-28T21:55:21Z")

</div>

@Montana Thanks for replying

I followed the instructions for using-environment variables. [https://docs.travis-ci.com/user/database-setup/#using-environment-variables-and-a-before\_script-step](https://docs.travis-ci.com/user/database-setup/#using-environment-variables-and-a-before_script-step)

But there isn’t a way to limit the services running for each variable.  
I’ll read through that again. it has provided nuggets of information in the past.
