Skip to content

Commit

Permalink
feat: automatically create database connection from YAML or environme…
Browse files Browse the repository at this point in the history
…nt variable config
  • Loading branch information
bethesque committed Jul 22, 2021
1 parent 3f80598 commit ca34b03
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/pact_broker/app.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "pact_broker/configuration"
require "pact_broker/db"
require "pact_broker/initializers/database_connection"
require "pact_broker/project_root"
require "pact_broker/logging/default_formatter"
require "pact_broker/policies"
Expand Down Expand Up @@ -122,6 +123,7 @@ def load_configuration_from_database

def configure_database_connection
# Keep this configuration in sync with lib/db.rb
configuration.database_connection ||= PactBroker.create_database_connection(configuration.logger, configuration.database_configuration, configuration.database_connect_max_retries)
PactBroker::DB.connection = configuration.database_connection
PactBroker::DB.connection.timezone = :utc
PactBroker::DB.connection.extend_datasets do
Expand Down
6 changes: 6 additions & 0 deletions lib/pact_broker/initializers/database_connection.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "sequel"
require "pact_broker/db/log_quietener"
require "fileutils"

##
# Sequel by default does not test connections in its connection pool before
Expand Down Expand Up @@ -33,6 +34,11 @@ def self.create_database_connection(logger, config, max_retries = 0)
connection = nil
wait = 3

if config[:adapter] == "sqlite" && config[:database] && !File.exist?(File.dirname(config[:database]))
logger&.info "Creating directory #{File.expand_path(File.dirname(config[:database]))} for Sqlite database"
FileUtils.mkdir_p(File.dirname(config[:database]))
end

begin
connection = Sequel.connect(config_with_logger)
rescue StandardError => e
Expand Down

0 comments on commit ca34b03

Please sign in to comment.