Skip to content

Commit

Permalink
feat(database): log schema versions and migration info on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 1, 2020
1 parent 143613e commit b385e53
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/pact_broker/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,18 @@ def post_configure
end

def prepare_database
logger.info "Database schema version is #{PactBroker::DB.version(configuration.database_connection)}"
if configuration.auto_migrate_db
logger.info "Migrating database"
PactBroker::DB.run_migrations configuration.database_connection, allow_missing_migration_files: configuration.allow_missing_migration_files
migration_options = { allow_missing_migration_files: configuration.allow_missing_migration_files }
if PactBroker::DB.is_current?(configuration.database_connection, migration_options)
logger.info "Skipping database migrations as the latest migration has already been applied"
else
logger.info "Migrating database schema"
PactBroker::DB.run_migrations configuration.database_connection, migration_options
logger.info "Database schema version is now #{PactBroker::DB.version(configuration.database_connection)}"
end
else
logger.info "Skipping database migrations"
logger.info "Skipping database schema migrations as database auto migrate is disabled"
end

if configuration.auto_migrate_db_data
Expand Down
10 changes: 9 additions & 1 deletion lib/pact_broker/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
require 'pact_broker/db/validate_encoding'
require 'pact_broker/db/migrate'
require 'pact_broker/db/migrate_data'
require 'pact_broker/db/version'

Sequel.datetime_class = DateTime

module PactBroker
module DB

MIGRATIONS_DIR = File.expand_path("../../../db/migrations", __FILE__)

def self.connection= connection
Expand All @@ -27,6 +27,14 @@ def self.run_data_migrations database_connection
PactBroker::DB::MigrateData.(database_connection)
end

def self.is_current? database_connection, options = {}
Sequel::TimestampMigrator.is_current?(database_connection, PactBroker::DB::MIGRATIONS_DIR, options)
end

def self.version database_connection
PactBroker::DB::Version.call(database_connection)
end

def self.validate_connection_config
PactBroker::DB::ValidateEncoding.(connection)
end
Expand Down

0 comments on commit b385e53

Please sign in to comment.