Skip to content

Commit

Permalink
Add ROM boilerplate for database connections
Browse files Browse the repository at this point in the history
We will need to reconcile the common notion of separate dev/test
databases with our earlier setup scripts, but this sets up the new
Hanami app to connect to "the database" via ROM and mysql2.

We can refer to the relations that are still under the top level lib
directory as we begin building out the API and need these tables mapped.
  • Loading branch information
botimer committed Nov 6, 2023
1 parent 6c07589 commit 43d219f
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
.DS_Store
.*.swp
*~
.env
.env*.local

.bundle/
.cache/
.idea/
coverage/
log/
tmp/
**lauth-secret.yml
**/.env*


# Meson-related build files
Expand Down
1 change: 1 addition & 0 deletions lauth/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL=mysql2://lauth:lauth@db.lauth.local:3306/lauth
1 change: 1 addition & 0 deletions lauth/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL=mysql2://lauth:lauth@db.lauth.local:3306/lauth_test
5 changes: 5 additions & 0 deletions lauth/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ gem "dry-types", "~> 1.0", ">= 1.6.1"
gem "puma"
gem "rake"

gem "rom", "~> 5.3"
gem "rom-sql", "~> 3.6"
gem "mysql2"

group :development do
gem "hanami-webconsole", "~> 2.1.0.beta"
gem "guard-puma"
Expand All @@ -32,4 +36,5 @@ end

group :test do
gem "rack-test"
gem "database_cleaner-sequel"
end
45 changes: 45 additions & 0 deletions lauth/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ GEM
coderay (>= 1.0.0)
erubi (>= 1.0.0)
rack (>= 0.9.0)
bigdecimal (3.1.4)
binding_of_caller (1.0.0)
debug_inspector (>= 0.0.1)
coderay (1.1.3)
concurrent-ruby (1.2.2)
database_cleaner-core (2.0.1)
database_cleaner-sequel (2.0.2)
database_cleaner-core (~> 2.0.0)
sequel
debug_inspector (1.1.0)
diff-lcs (1.5.0)
dotenv (2.8.1)
Expand Down Expand Up @@ -45,6 +50,11 @@ GEM
dry-logic (>= 1.4, < 2)
dry-types (>= 1.7, < 2)
zeitwerk (~> 2.6)
dry-struct (1.6.0)
dry-core (~> 1.0, < 2)
dry-types (>= 1.7, < 2)
ice_nine (~> 0.11)
zeitwerk (~> 2.6)
dry-system (1.0.1)
dry-auto_inject (~> 1.0, < 2)
dry-configurable (~> 1.0, < 2)
Expand Down Expand Up @@ -138,6 +148,7 @@ GEM
better_errors (~> 2.9, < 2.10.0)
binding_of_caller (~> 1.0)
hansi (0.2.1)
ice_nine (0.11.2)
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
Expand All @@ -148,6 +159,7 @@ GEM
mustermann-contrib (3.0.0)
hansi (~> 0.2.0)
mustermann (= 3.0.0)
mysql2 (0.5.5)
nenv (0.3.0)
nio4r (2.5.9)
notiffany (0.1.3)
Expand All @@ -165,6 +177,32 @@ GEM
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
rom (5.3.0)
rom-changeset (~> 5.3, >= 5.3.0)
rom-core (~> 5.3, >= 5.3.0)
rom-repository (~> 5.3, >= 5.3.0)
rom-changeset (5.3.0)
dry-core (~> 1.0)
rom-core (~> 5.3)
transproc (~> 1.0, >= 1.1.0)
rom-core (5.3.0)
concurrent-ruby (~> 1.1)
dry-configurable (~> 1.0)
dry-core (~> 1.0)
dry-inflector (~> 1.0)
dry-initializer (~> 3.0, >= 3.0.1)
dry-struct (~> 1.0)
dry-types (~> 1.6)
transproc (~> 1.0, >= 1.1.0)
rom-repository (5.3.0)
dry-core (~> 1.0)
dry-initializer (~> 3.0, >= 3.0.1)
rom-core (~> 5.3, >= 5.3.0)
rom-sql (3.6.1)
dry-core (~> 1.0)
dry-types (~> 1.0)
rom (~> 5.2, >= 5.2.1)
sequel (>= 4.49)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
Expand All @@ -179,17 +217,21 @@ GEM
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
ruby2_keywords (0.0.5)
sequel (5.74.0)
bigdecimal
shellany (0.0.1)
temple (0.10.3)
thor (1.3.0)
tilt (2.3.0)
transproc (1.1.1)
zeitwerk (2.6.12)

PLATFORMS
aarch64-linux
arm64-darwin-22

DEPENDENCIES
database_cleaner-sequel
dotenv
dry-types (~> 1.0, >= 1.6.1)
guard-puma
Expand All @@ -202,9 +244,12 @@ DEPENDENCIES
hanami-validations (~> 2.1.0.beta)
hanami-view (~> 2.1.0.beta)
hanami-webconsole (~> 2.1.0.beta)
mysql2
puma
rack-test
rake
rom (~> 5.3)
rom-sql (~> 3.6)

BUNDLED WITH
2.4.21
13 changes: 13 additions & 0 deletions lauth/Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# frozen_string_literal: true

require "hanami/rake_tasks"
require "rom/sql/rake_task"

task :environment do
require_relative "config/app"
require "hanami/prepare"
end

namespace :db do
task setup: :environment do
Hanami.app.prepare(:persistence)
ROM::SQL::RakeSupport.env = Hanami.app["persistence.config"]
end
end
21 changes: 21 additions & 0 deletions lauth/config/providers/persistence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Hanami.app.register_provider :persistence, namespace: true do
prepare do
require "rom"

config = ROM::Configuration.new(:sql, target["settings"].database_url)

register "config", config
register "db", config.gateways[:default].connection
end

start do
config = target["persistence.config"]

config.auto_registration(
target.root.join("lib/lauth/persistence"),
namespace: "Lauth::Persistence"
)

register "rom", ROM.container(config)
end
end
4 changes: 1 addition & 3 deletions lauth/config/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

module Lauth
class Settings < Hanami::Settings
# Define your app settings here, for example:
#
# setting :my_flag, default: false, constructor: Types::Params::Bool
setting :database_url, constructor: Types::String
end
end
1 change: 1 addition & 0 deletions lauth/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@

require_relative "support/rspec"
require_relative "support/requests"
require_relative "support/database_cleaner"
15 changes: 15 additions & 0 deletions lauth/spec/support/database_cleaner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Hanami.app.prepare(:persistence)
DatabaseCleaner[:sequel, db: Hanami.app["persistence.db"]]

RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end

config.around(:each, type: :database) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
end

0 comments on commit 43d219f

Please sign in to comment.