Rails database setup on Travis-CI

守給你的承諾、 提交于 2019-12-02 16:19:25

My solution for this problem is fully based on a blog post with a few differences:

  1. Travis CI specific settings in config/database.travis.yml;
  2. cp config/database.travis.yml config/database.yml in before script section of .travis.yml;
  3. I don't have config/database.yml in source tree.

Here is full listing for both files:

# .travis.yml
language: ruby
rvm:
  - 1.9.3
env:
  - DB=sqlite
  - DB=mysql
  - DB=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.travis.yml config/database.yml
  - mysql -e 'create database strano_test'
  - psql -c 'create database strano_test' -U postgres


# config/database.travis.yml
sqlite: &sqlite
  adapter: sqlite3
  database: db/<%= Rails.env %>.sqlite3

mysql: &mysql
  adapter: mysql2
  username: root
  password:
  database: strano_<%= Rails.env %>

postgresql: &postgresql
  adapter: postgresql
  username: postgres
  password:
  database: strano_<%= Rails.env %>
  min_messages: ERROR

defaults: &defaults
  pool: 5
  timeout: 5000
  host: localhost
  <<: *<%= ENV['DB'] || "postgresql" %>

development:
  <<: *defaults

test:
  <<: *defaults

production:
  <<: *defaults

@mrm's blog post doesn't say anything about answering your question. I faced the same problem where my postgreql credentials are different on my local machine than travis default. This is the simplest solution I came up with:

# config/database.yml
test:
  adapter: postgresql
  database: medscraper_test
  username: <%= ENV['TRAVIS'] ? 'postgres' : 'MY_TEST_USERNAME' %>
  password: <%= ENV['TRAVIS'] ? '' : 'MY_TEST_PASSWORD' %>

Note that Travis CI automatically sets TRAVIS environment variable. Your solution would be:

# config/database.yml
test:
  adapter: sqlite3
  database: <%= ENV['TRAVIS'] ? '":memory:"' : 'db/test.sqlite3' %>
  timeout: 500

I just wrote a blog post describing how to do this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!