Skip to content

Commit

Permalink
Setup postgres container on GitHub CI Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
bhaak committed Feb 25, 2025
1 parent 10d78ab commit 84306e4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
26 changes: 25 additions & 1 deletion .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,39 @@ jobs:
test:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_HOST: localhost
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
- name: Setup system dependencies
run: sudo apt-get install -y libpq-dev postgresql-server-dev-16

- uses: actions/checkout@v4

- name: Set up Ruby
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# change this to (see https://github.com/ruby/setup-ruby#versioning):
# uses: ruby/setup-ruby@v1
uses: ruby/setup-ruby@v1
with:
bundler-cache: true # runs 'bundle install' and caches installed gems automatically

- name: Run tests
run: bundle exec rake spec

env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_HOST: localhost
POSTGRES_DATABASE: postgres
13 changes: 10 additions & 3 deletions lib/junethack/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,31 @@
# set all String properties to have a default length of 255
DataMapper::Property::String.length(255)

USER = ENV['POSTGRES_USER'] || 'junethack'
PASSWORD = ENV['POSTGRES_PASSWORD'] || 'test'
HOST = ENV['POSTGRES_HOST'] || 'localhost'

configure :production do
puts "Configuring production database"
DATABASE = ENV['POSTGRES_DATABASE'] || 'junethack_production'
# for debugging: print all generated SQL statemtens
#DataMapper::Logger.new("logs/db.log", :debug)
DataMapper.setup(:default, 'postgres://localhost/junethack')
DataMapper.setup(:default, "postgres://#{USER}:#{PASSWORD}@#{HOST}/#{DATABASE}")
end

configure :development do
puts "Configuring development database"
DATABASE = ENV['POSTGRES_DATABASE'] || 'junethack_developmnet'
# for debugging: print all generated SQL statemtens
DataMapper::Logger.new("logs/dev_db.log", :debug)
DataMapper.setup(:default, 'postgres://localhost/junethack')
DataMapper.setup(:default, "postgres://#{USER}:#{PASSWORD}@#{HOST}/#{DATABASE}")
end

configure :test do
puts "Configuring test database"
DATABASE = ENV['POSTGRES_DATABASE'] || 'junethack_text'
DataMapper::Logger.new("logs/test_db.log", :debug)
DataMapper.setup(:default, "postgres://#{user}:#{password}@localhost/junethack_test")
DataMapper.setup(:default, "postgres://#{USER}:#{PASSWORD}@#{HOST}/#{DATABASE}")

# suppress migration output.
# it would be written at every run as we use a in-memory db
Expand Down

0 comments on commit 84306e4

Please # to comment.