Skip to content

Commit

Permalink
Add a setup for using docker compose to run a local database
Browse files Browse the repository at this point in the history
This will prefer a `DATABASE_URL` if it's set, otherwise, fall back
to checking for a running docker database via docker compose, and
only if that fails, fall back to a system postgres server

This adds `dotenv` to the requirements, to avoid duplicating the
authentication credentials for docker's postgres
  • Loading branch information
cflipse committed Nov 14, 2024
1 parent d707579 commit 277209c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .postgres.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POSTGRES_USER="rom"
POSTGRES_PASSWORD="password"
POSTGRES_DATABASE="rom_factory"
POSTGRES_HOST_AUTH_METHOD="trust"
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ gem "faker", "~> 3.0"

gem "rspec", "~> 3.0"

gem "dotenv"

git "https://github.com/rom-rb/rom.git", branch: "release-5.3" do
gem "rom-core"
gem "rom"
gem "rom-changeset"
gem "rom-core"
gem "rom-repository"
gem "rom"
end

group :test do
Expand Down
7 changes: 7 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
services:
db:
image: postgres
ports:
- 5432
env_file: .postgres.env
12 changes: 10 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require_relative "support/coverage"
require "dotenv"
Dotenv.load(".postgres.env", ".env")

require "pathname"
SPEC_ROOT = root = Pathname(__FILE__).dirname
Expand All @@ -18,10 +20,16 @@
end

DB_URI = ENV.fetch("DATABASE_URL") do
auth = ENV.values_at("POSTGRES_USER", "POSTGRES_PASSWORD").join(":")
address = `docker compose port db 5432 2> /dev/null`.strip
address = [auth, address].join("@") if address

address ||= "localhost"

if defined? JRUBY_VERSION
"jdbc:postgresql://localhost/rom_factory"
"jdbc:postgresql://#{address}"
else
"postgres://localhost/rom_factory"
"postgres://#{address}"
end
end

Expand Down

0 comments on commit 277209c

Please sign in to comment.