Skip to content

Commit 84306e4

Browse files
committed
Setup postgres container on GitHub CI Actions
1 parent 10d78ab commit 84306e4

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

.github/workflows/ruby.yml

+25-1
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,39 @@ jobs:
1313
test:
1414
runs-on: ubuntu-latest
1515

16+
services:
17+
postgres:
18+
image: postgres:latest
19+
env:
20+
POSTGRES_USER: postgres
21+
POSTGRES_PASSWORD: postgres
22+
POSTGRES_HOST: localhost
23+
options: >-
24+
--health-cmd pg_isready
25+
--health-interval 10s
26+
--health-timeout 5s
27+
--health-retries 5
28+
ports:
29+
- 5432:5432
30+
1631
steps:
32+
- name: Setup system dependencies
33+
run: sudo apt-get install -y libpq-dev postgresql-server-dev-16
34+
1735
- uses: actions/checkout@v4
36+
1837
- name: Set up Ruby
1938
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
2039
# change this to (see https://github.com/ruby/setup-ruby#versioning):
2140
# uses: ruby/setup-ruby@v1
2241
uses: ruby/setup-ruby@v1
2342
with:
2443
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
44+
2545
- name: Run tests
2646
run: bundle exec rake spec
27-
47+
env:
48+
POSTGRES_USER: postgres
49+
POSTGRES_PASSWORD: postgres
50+
POSTGRES_HOST: localhost
51+
POSTGRES_DATABASE: postgres

lib/junethack/database.rb

+10-3
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,31 @@
1111
# set all String properties to have a default length of 255
1212
DataMapper::Property::String.length(255)
1313

14+
USER = ENV['POSTGRES_USER'] || 'junethack'
15+
PASSWORD = ENV['POSTGRES_PASSWORD'] || 'test'
16+
HOST = ENV['POSTGRES_HOST'] || 'localhost'
17+
1418
configure :production do
1519
puts "Configuring production database"
20+
DATABASE = ENV['POSTGRES_DATABASE'] || 'junethack_production'
1621
# for debugging: print all generated SQL statemtens
1722
#DataMapper::Logger.new("logs/db.log", :debug)
18-
DataMapper.setup(:default, 'postgres://localhost/junethack')
23+
DataMapper.setup(:default, "postgres://#{USER}:#{PASSWORD}@#{HOST}/#{DATABASE}")
1924
end
2025

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

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

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

0 commit comments

Comments
 (0)