From 00ee00d570e55a9ba71e882a4b02106e25b990c3 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 28 Jan 2020 12:25:17 +1100 Subject: [PATCH] feat: expose the data migrations code as a rake task --- lib/pact_broker/db.rb | 2 +- lib/pact_broker/tasks.rb | 1 + lib/pact_broker/tasks/data_migration_task.rb | 38 ++++++++++++++++++++ tasks/test_db.rake | 6 ++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 lib/pact_broker/tasks/data_migration_task.rb diff --git a/lib/pact_broker/db.rb b/lib/pact_broker/db.rb index 2b36ccc63..e3861aed5 100644 --- a/lib/pact_broker/db.rb +++ b/lib/pact_broker/db.rb @@ -24,7 +24,7 @@ def self.run_migrations database_connection, options = {} end def self.run_data_migrations database_connection - PactBroker::DB::MigrateData.(connection) + PactBroker::DB::MigrateData.(database_connection) end def self.validate_connection_config diff --git a/lib/pact_broker/tasks.rb b/lib/pact_broker/tasks.rb index 42b6e608e..9adecda87 100644 --- a/lib/pact_broker/tasks.rb +++ b/lib/pact_broker/tasks.rb @@ -1,3 +1,4 @@ require 'pact_broker/tasks/migration_task' +require 'pact_broker/tasks/data_migration_task' require 'pact_broker/tasks/version_task' require 'pact_broker/tasks/clean_task' diff --git a/lib/pact_broker/tasks/data_migration_task.rb b/lib/pact_broker/tasks/data_migration_task.rb new file mode 100644 index 000000000..ac4a2a378 --- /dev/null +++ b/lib/pact_broker/tasks/data_migration_task.rb @@ -0,0 +1,38 @@ +require 'rake/tasklib' + +=begin + +require 'pact_broker/tasks' + +PactBroker::DB::DataMigrationTask.new do | task | + require 'my_app/db' + task.database_connection = MyApp::DB +end + +=end + +module PactBroker + module DB + class DataMigrationTask < ::Rake::TaskLib + + attr_accessor :database_connection + + def initialize &block + rake_task &block + end + + def rake_task &block + namespace :pact_broker do + namespace :db do + desc "Run data migrations for pact broker database" + task :migrate_data do | t, args | + require 'pact_broker/db' + instance_eval(&block) + PactBroker::DB.run_data_migrations database_connection + end + end + end + end + end + end +end diff --git a/tasks/test_db.rake b/tasks/test_db.rake index 665597b0d..ecacb14e2 100644 --- a/tasks/test_db.rake +++ b/tasks/test_db.rake @@ -6,6 +6,12 @@ PactBroker::DB::MigrationTask.new do | task | task.database_connection = DB::PACT_BROKER_DB end +PactBroker::DB::DataMigrationTask.new do | task | + ENV['RACK_ENV'] ||= 'test' + require 'db' + task.database_connection = DB::PACT_BROKER_DB +end + PactBroker::DB::VersionTask.new do | task | ENV['RACK_ENV'] ||= 'test' require 'db'