From d520e228ec09de7fa138d912dbe2a77ff0e4395e Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 31 Jul 2018 21:19:35 +1000 Subject: [PATCH] feat: add consumer_id to pact_publications table --- ...15_add_consumer_id_to_pact_publications.rb | 13 ++++++ ...rate_consumer_ids_for_pact_publications.rb | 7 ++++ .../set_consumer_ids_for_pact_publications.rb | 31 ++++++++++++++ lib/pact_broker/db/migrate_data.rb | 5 ++- lib/pact_broker/pacts/repository.rb | 2 + ...consumer_ids_for_pact_publications_spec.rb | 40 +++++++++++++++++++ spec/lib/pact_broker/pacts/repository_spec.rb | 5 +++ ...d_provider_version_to_verification_spec.rb | 1 - 8 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 db/migrations/20180615_add_consumer_id_to_pact_publications.rb create mode 100644 db/migrations/20180616_migrate_consumer_ids_for_pact_publications.rb create mode 100644 lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications.rb create mode 100644 spec/lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications_spec.rb diff --git a/db/migrations/20180615_add_consumer_id_to_pact_publications.rb b/db/migrations/20180615_add_consumer_id_to_pact_publications.rb new file mode 100644 index 000000000..4b0ce87b5 --- /dev/null +++ b/db/migrations/20180615_add_consumer_id_to_pact_publications.rb @@ -0,0 +1,13 @@ +Sequel.migration do + change do + alter_table(:pact_publications) do + add_foreign_key(:consumer_id, :pacticipants) + add_index(:consumer_id, name: "pact_publications_consumer_id_index") + end + + # TODO + # alter_table(:pact_publications) do + # set_column_not_null(:consumer_id) + # end + end +end diff --git a/db/migrations/20180616_migrate_consumer_ids_for_pact_publications.rb b/db/migrations/20180616_migrate_consumer_ids_for_pact_publications.rb new file mode 100644 index 000000000..f2b9ab228 --- /dev/null +++ b/db/migrations/20180616_migrate_consumer_ids_for_pact_publications.rb @@ -0,0 +1,7 @@ +require 'pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications' + +Sequel.migration do + up do + PactBroker::DB::DataMigrations::SetConsumerIdsForPactPublications.call(self) + end +end diff --git a/lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications.rb b/lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications.rb new file mode 100644 index 000000000..a15c493d6 --- /dev/null +++ b/lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications.rb @@ -0,0 +1,31 @@ +module PactBroker + module DB + module DataMigrations + class SetConsumerIdsForPactPublications + def self.call connection + if columns_exist?(connection) + ids = connection.from(:pact_publications) + .select(Sequel[:pact_publications][:id], Sequel[:versions][:pacticipant_id].as(:consumer_id)) + .join(:versions, {id: :consumer_version_id}) + .where(Sequel[:pact_publications][:consumer_id] => nil) + + ids.each do | id | + connection.from(:pact_publications).where(id: id[:id]).update(consumer_id: id[:consumer_id]) + end + end + end + + def self.columns_exist?(connection) + column_exists?(connection, :pact_publications, :consumer_id) && + column_exists?(connection, :pact_publications, :id) && + column_exists?(connection, :versions, :id) && + column_exists?(connection, :versions, :pacticipant_id) + end + + def self.column_exists?(connection, table, column) + connection.table_exists?(table) && connection.schema(table).find{|col| col.first == column } + end + end + end + end +end diff --git a/lib/pact_broker/db/migrate_data.rb b/lib/pact_broker/db/migrate_data.rb index 4b22db1a6..31e6fe1e0 100644 --- a/lib/pact_broker/db/migrate_data.rb +++ b/lib/pact_broker/db/migrate_data.rb @@ -1,10 +1,13 @@ -require 'pact_broker/db/data_migrations/set_pacticipant_ids_for_verifications' +Dir.glob(File.expand_path(File.join(__FILE__, "..", "data_migrations", "*.rb"))).sort.each do | path | + require path +end module PactBroker module DB class MigrateData def self.call database_connection, options = {} DataMigrations::SetPacticipantIdsForVerifications.call(database_connection) + DataMigrations::SetConsumerIdsForPactPublications.call(database_connection) end end end diff --git a/lib/pact_broker/pacts/repository.rb b/lib/pact_broker/pacts/repository.rb index 656d99213..cc52f6bbe 100644 --- a/lib/pact_broker/pacts/repository.rb +++ b/lib/pact_broker/pacts/repository.rb @@ -23,6 +23,7 @@ def create params PactPublication.new( consumer_version_id: params[:version_id], provider_id: params[:provider_id], + consumer_id: params[:consumer_id], pact_version: find_or_create_pact_version(params.fetch(:consumer_id), params.fetch(:provider_id), params[:json_content]), ).save.to_domain end @@ -33,6 +34,7 @@ def update id, params if existing_model.pact_version_id != pact_version.id PactPublication.new( consumer_version_id: existing_model.consumer_version_id, + consumer_id: existing_model.consumer_id, provider_id: existing_model.provider_id, revision_number: (existing_model.revision_number + 1), pact_version: pact_version, diff --git a/spec/lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications_spec.rb b/spec/lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications_spec.rb new file mode 100644 index 000000000..b402c8ab3 --- /dev/null +++ b/spec/lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications_spec.rb @@ -0,0 +1,40 @@ +require 'pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications' + +module PactBroker + module DB + module DataMigrations + describe SetConsumerIdsForPactPublications, migration: true do + describe ".call" do + before do + PactBroker::Database.migrate(20180615) + end + + let(:now) { DateTime.new(2018, 2, 2) } + let!(:consumer_other) { create(:pacticipants, {name: 'Other consumer', created_at: now, updated_at: now}) } + let!(:consumer) { create(:pacticipants, {name: 'Consumer', created_at: now, updated_at: now}) } + let!(:provider) { create(:pacticipants, {name: 'Provider', created_at: now, updated_at: now}) } + let!(:consumer_version) { create(:versions, {number: '1.2.3', order: 1, pacticipant_id: consumer[:id], created_at: now, updated_at: now}) } + let!(:pact_version) { create(:pact_versions, {content: {some: 'json'}.to_json, sha: '1234', consumer_id: consumer[:id], provider_id: provider[:id], created_at: now}) } + let!(:pact_publication) do + create(:pact_publications, { + consumer_version_id: consumer_version[:id], + provider_id: provider[:id], + revision_number: 1, + pact_version_id: pact_version[:id], + created_at: (now - 1) + }) + end + + subject { SetConsumerIdsForPactPublications.call(database) } + + it "sets the consumer_id" do + expect(database[:pact_publications].first[:consumer_id]).to be nil + subject + expect(database[:pact_publications].first[:consumer_id]).to_not be nil + expect(database[:pact_publications].first[:consumer_id]).to eq consumer[:id] + end + end + end + end + end +end diff --git a/spec/lib/pact_broker/pacts/repository_spec.rb b/spec/lib/pact_broker/pacts/repository_spec.rb index f78737d4b..0f418a23d 100644 --- a/spec/lib/pact_broker/pacts/repository_spec.rb +++ b/spec/lib/pact_broker/pacts/repository_spec.rb @@ -24,6 +24,11 @@ module Pacts expect{subject}.to change{ PactPublication.count }.by(1) end + it "sets the consumer_id" do + subject + expect(PactPublication.first.consumer_id).to eq consumer.id + end + it "returns a Pact::Model" do expect(subject).to be_instance_of(PactBroker::Domain::Pact) end diff --git a/spec/migrations/44_add_provider_version_to_verification_spec.rb b/spec/migrations/44_add_provider_version_to_verification_spec.rb index 3113638ba..b635e00c1 100644 --- a/spec/migrations/44_add_provider_version_to_verification_spec.rb +++ b/spec/migrations/44_add_provider_version_to_verification_spec.rb @@ -6,7 +6,6 @@ let(:now) { DateTime.new(2018, 2, 2) } let!(:consumer) { create(:pacticipants, {name: 'Consumer', created_at: now, updated_at: now}) } let!(:provider) { create(:pacticipants, {name: 'Provider', created_at: now, updated_at: now}) } - let!(:provider) { create(:pacticipants, {name: 'Provider', created_at: now, updated_at: now}) } let!(:consumer_version) { create(:versions, {number: '1.2.3', order: 1, pacticipant_id: consumer[:id], created_at: now, updated_at: now}) } let!(:pact_version) { create(:pact_versions, {content: {some: 'json'}.to_json, sha: '1234', consumer_id: consumer[:id], provider_id: provider[:id], created_at: now}) } let!(:pact_publication) do