From ec59ba5b762ca294d739be7d828301af171af365 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 31 Jul 2018 16:34:10 +1000 Subject: [PATCH] feat: run data migrations on startup to ensure data created by old nodes is migrated when performing a rolling update --- lib/pact_broker/app.rb | 8 ++++++++ lib/pact_broker/configuration.rb | 3 ++- lib/pact_broker/db.rb | 6 ++++++ lib/pact_broker/db/migrate_data.rb | 9 +++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 lib/pact_broker/db/migrate_data.rb diff --git a/lib/pact_broker/app.rb b/lib/pact_broker/app.rb index 2944f5108..8daec289b 100644 --- a/lib/pact_broker/app.rb +++ b/lib/pact_broker/app.rb @@ -75,6 +75,14 @@ def prepare_database else logger.info "Skipping database migrations" end + + if configuration.auto_migrate_db_data + logger.info "Migrating data" + PactBroker::DB.run_data_migrations configuration.database_connection + else + logger.info "Skipping data migrations" + end + require 'pact_broker/webhooks/service' PactBroker::Webhooks::Service.fail_retrying_triggered_webhooks end diff --git a/lib/pact_broker/configuration.rb b/lib/pact_broker/configuration.rb index a40de3137..377fdb054 100644 --- a/lib/pact_broker/configuration.rb +++ b/lib/pact_broker/configuration.rb @@ -31,7 +31,7 @@ class Configuration :base_equality_only_on_content_that_affects_verification_results ] - attr_accessor :log_dir, :database_connection, :auto_migrate_db, :use_hal_browser, :html_pact_renderer + attr_accessor :log_dir, :database_connection, :auto_migrate_db, :auto_migrate_db_data, :use_hal_browser, :html_pact_renderer attr_accessor :validate_database_connection_config, :enable_diagnostic_endpoints, :version_parser, :sha_generator attr_accessor :use_case_sensitive_resource_names, :order_versions_by_date attr_accessor :check_for_potential_duplicate_pacticipant_names @@ -63,6 +63,7 @@ def self.default_configuration config = Configuration.new config.log_dir = File.expand_path("./log") config.auto_migrate_db = true + config.auto_migrate_db_data = true config.use_hal_browser = true config.validate_database_connection_config = true config.enable_diagnostic_endpoints = true diff --git a/lib/pact_broker/db.rb b/lib/pact_broker/db.rb index eb7289bc0..f64b45ae2 100644 --- a/lib/pact_broker/db.rb +++ b/lib/pact_broker/db.rb @@ -1,5 +1,7 @@ require 'sequel' require 'pact_broker/db/validate_encoding' +require 'pact_broker/db/migrate' +require 'pact_broker/db/migrate_data' Sequel.datetime_class = DateTime @@ -21,6 +23,10 @@ def self.run_migrations database_connection Sequel::TimestampMigrator.new(database_connection, PactBroker::DB::MIGRATIONS_DIR).run end + def self.run_data_migrations database_connection + PactBroker::DB::MigrateData.(connection) + end + def self.validate_connection_config PactBroker::DB::ValidateEncoding.(connection) end diff --git a/lib/pact_broker/db/migrate_data.rb b/lib/pact_broker/db/migrate_data.rb new file mode 100644 index 000000000..a44143762 --- /dev/null +++ b/lib/pact_broker/db/migrate_data.rb @@ -0,0 +1,9 @@ +module PactBroker + module DB + class MigrateData + def self.call database_connection, options = {} + + end + end + end +end