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