I have defined a secondary external database in my Rails application for read-only purposes. Thus for testing, I setup a local database and plan to mock data within my test examples. Connecting to the database and running tests locally work great. However, when running the CI tests, the secondary database fails to setup due to the following error:
I believe this to be a configuration setup issue within the ci.yml
file, and am not sure how to configure this properly.
# ci.yml
name: Continuous Integration
on:
pull_request:
branches: [ main ]
jobs:
test:
name: Testing
runs-on: ubuntu-latest
services:
postgres:
image: postgres:12
ports:
- "5432:5432"
env:
POSTGRES_USER: rails
POSTGRES_PASSWORD: password
env:
RAILS_ENV: test
RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Chromedriver
uses: nanasess/setup-chromedriver@v1
# with:
# Optional: do not specify to match Chrome's version
# chromedriver-version: '88.0.4324.96'
# Add or replace dependency steps here
- name: Install Ruby and gems
uses: ruby/setup-ruby@1a68550f2e3309e13c8ccb91ac6b8786f59ee147
with:
bundler-cache: true
# Add or replace database setup steps here
- name: Set up primary database
env:
POSTGRES_DB: calendarize_test
DATABASE_URL: "postgres://rails:password@localhost:5432/calendarize_test"
run: bin/rails db:create:primary db:migrate:primary
- name: Set up warehouse database
env:
POSTGRES_DB: warehouse_test
DATABASE_URL: "postgres://rails:password@localhost:5432/warehouse_test"
run: bin/rails db:create:warehouse db:migrate:warehouse
# Add or replace test runners here
- name: Start Chromedriver
run: |
export DISPLAY=:99
chromedriver --url-base=/wd/hub --disable-dev-shm-usage &
sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional
- name: Run tests
run: bundle exec rspec --color
# database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
calendarize: &calendarize
<<: *default
host: localhost
username: <%= ENV["CALENDARIZE_DATABASE_USERNAME"] %>
password: <%= ENV["CALENDARIZE_DATABASE_PASSWORD"] %>
test:
primary:
<<: *calendarize
database: calendarize_test
warehouse:
<<: *calendarize
database: warehouse_test
migrations_paths: db/warehouse_migrate
development:
primary:
<<: *calendarize
database: calendarize_development
warehouse:
<<: *calendarize
database: warehouse_development
migrations_paths: db/warehouse_migrate
production:
primary:
<<: *calendarize
database: <%= ENV["CALENDARIZE_DATABASE_NAME"] %>
warehouse:
<<: *default
url: <%= ENV["WAREHOUSE_DATABASE_URL"] %>
database_tasks: false
2
Answers
The problem was solved by matching the username/password fields between both
ci.yml
anddatabase.yml
.I think i encountered the same issue when i run tests in circleci while supporting multiple database. Because circleci assumes you will only need 1 database. In my case i was using mysql but i think same approach will also help you too. First i installed the mysql client in order to make sql queries from the image that we are on in circleci. Then i gave permissions to the user that i have in my database.yml and rest is just rails commands. Hope the code below demosntrate it better.