From ca34b0301381bd334a9c79958a082b99204c57b8 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 22 Jul 2021 10:33:53 +1000 Subject: [PATCH] feat: automatically create database connection from YAML or environment variable config --- lib/pact_broker/app.rb | 2 ++ lib/pact_broker/initializers/database_connection.rb | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/lib/pact_broker/app.rb b/lib/pact_broker/app.rb index e308e7e2d..5c8675039 100644 --- a/lib/pact_broker/app.rb +++ b/lib/pact_broker/app.rb @@ -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" @@ -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 diff --git a/lib/pact_broker/initializers/database_connection.rb b/lib/pact_broker/initializers/database_connection.rb index 943cf00e9..581b8dd80 100644 --- a/lib/pact_broker/initializers/database_connection.rb +++ b/lib/pact_broker/initializers/database_connection.rb @@ -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 @@ -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