How to test your rails application with Travis CI on different databases engines

Travis CI is an awesome continuous integration service that’s free for open-source projects.

If you’d like to test your app against multiple database engines, it’s fairly simple.
For these examples, I’m testing my app against SQLite, MySQL, and PostgreSQL.

Edit your #{Rails.root}/.travis.yml (and replace myapp with your app name):

And your config/database.yml:

How does this work?

The only tricky line is this:

The database.yml is pulled through the ERB interpreter first, and then parsed as YAML. The DB default is “postgresql” (which you could change, of course), and is overridden by the DB environment variable. The only glitch to this approach is if you use a database engine that doesn’t have a section defined (like “oracle”), you get a fairly cryptic error message from ActiveRecord that the “database driver must be specified”.

Do you have an example I can look at?

Certainly! Chromotype is an example Rails application that is using this setup successfully (along with Minitest!).

Posted in Technical HOWTOs
  • Thomas von Deyen

    This is so smart. Thank you for sharing this.

  • pfioravanti

    Thanks so much for sharing this.  This configuration worked well for me for a while, then strange things started happening with my rake tasks.  I outlined the issue as best as I could on StackOverflow here:  Have you had this happen to you or have you seen this kind of behaviour before with your configuration? 

  • Simone

    This helped me to find the right configuration for my app. Thank you.

  • Gregori

    Thanks. This post really helped.

  • LAT

    Thanks for the great article!