Skip
Arish's avatar

10. Database Setup in CI


PostgreSQL Service

yaml
1services:
2  postgres:
3    image: postgres:15
4    env:
5      POSTGRES_USER: postgres
6      POSTGRES_PASSWORD: postgres
7      POSTGRES_DB: test
8    ports:
9      - 5432:5432
10    options: >-
11      --health-cmd pg_isready
12      --health-interval 10s
13      --health-timeout 5s
14      --health-retries 5

Database Configuration

yaml
1# config/database.yml
2test:
3  adapter: postgresql
4  encoding: unicode
5  host: <%= ENV.fetch('DB_HOST', 'localhost') %>
6  port: <%= ENV.fetch('DB_PORT', 5432) %>
7  username: <%= ENV.fetch('DB_USER', 'postgres') %>
8  password: <%= ENV.fetch('DB_PASSWORD', 'postgres') %>
9  database: <%= ENV.fetch('DB_NAME', 'myapp_test') %>

Setup Steps

yaml
1env:
2  DATABASE_URL: postgres://postgres:postgres@localhost:5432/test
3  RAILS_ENV: test
4
5steps:
6  - name: Setup database
7    run: |
8      bundle exec rails db:create
9      bundle exec rails db:schema:load

With Migrations

yaml
1- name: Setup database
2  run: |
3    bundle exec rails db:create
4    bundle exec rails db:migrate

MySQL Alternative

yaml
1services:
2  mysql:
3    image: mysql:8
4    env:
5      MYSQL_ROOT_PASSWORD: root
6      MYSQL_DATABASE: test
7    ports:
8      - 3306:3306
9    options: >-
10      --health-cmd="mysqladmin ping"
11      --health-interval=10s
12      --health-timeout=5s
13      --health-retries=3