diff --git a/db/migrations/20210712_add_interaction_count_to_pact_versions.rb b/db/migrations/20210712_add_interaction_count_to_pact_versions.rb new file mode 100644 index 000000000..9a0cd5de1 --- /dev/null +++ b/db/migrations/20210712_add_interaction_count_to_pact_versions.rb @@ -0,0 +1,8 @@ +Sequel.migration do + change do + alter_table(:pact_versions) do + add_column(:messages_count, Integer) + add_column(:interactions_count, Integer) + end + end +end diff --git a/lib/pact_broker/pacts/content.rb b/lib/pact_broker/pacts/content.rb index b526a7202..c7aecc503 100644 --- a/lib/pact_broker/pacts/content.rb +++ b/lib/pact_broker/pacts/content.rb @@ -95,11 +95,11 @@ def content_that_affects_verification_results end def messages - pact_hash.is_a?(Hash) ? pact_hash["messages"] : nil + pact_hash.is_a?(Hash) && pact_hash["messages"].is_a?(Array) ? pact_hash["messages"] : nil end def interactions - pact_hash.is_a?(Hash) ? pact_hash["interactions"] : nil + pact_hash.is_a?(Hash) && pact_hash["interactions"].is_a?(Array) ? pact_hash["interactions"] : nil end def messages_or_interactions diff --git a/lib/pact_broker/pacts/repository.rb b/lib/pact_broker/pacts/repository.rb index 4d6f43e13..8fdec9b18 100644 --- a/lib/pact_broker/pacts/repository.rb +++ b/lib/pact_broker/pacts/repository.rb @@ -20,6 +20,7 @@ require "pact_broker/pacts/selectors" require "pact_broker/feature_toggle" require "pact_broker/pacts/pacts_for_verification_repository" +require "pact_broker/pacts/content" module PactBroker module Pacts @@ -355,7 +356,9 @@ def create_pact_version consumer_id, provider_id, sha, json_content provider_id: provider_id, sha: sha, content: json_content, - created_at: Sequel.datetime_class.now + created_at: Sequel.datetime_class.now, + interactions_count: Content.from_json(json_content).interactions&.count || 0, + messages_count: Content.from_json(json_content).messages&.count || 0 ).upsert end diff --git a/spec/lib/pact_broker/pacts/content_spec.rb b/spec/lib/pact_broker/pacts/content_spec.rb index 4dfd89092..aa4da7638 100644 --- a/spec/lib/pact_broker/pacts/content_spec.rb +++ b/spec/lib/pact_broker/pacts/content_spec.rb @@ -123,7 +123,7 @@ module Pacts end end - context "with both messages and interactions, even though this should never happen" do + context "with both messages and interactions (v4)" do let(:pact_hash) do { "ignored" => "foo", diff --git a/spec/lib/pact_broker/pacts/repository_spec.rb b/spec/lib/pact_broker/pacts/repository_spec.rb index 50e1e303f..c7ad93fad 100644 --- a/spec/lib/pact_broker/pacts/repository_spec.rb +++ b/spec/lib/pact_broker/pacts/repository_spec.rb @@ -17,7 +17,7 @@ module Pacts let(:provider) { Pacticipants::Repository.new.create name: "Provider" } let(:version) { PactBroker::Versions::Repository.new.create number: "1.2.3", pacticipant_id: consumer.id } let(:pact_version_sha) { "123" } - let(:json_content) { {some: "json"}.to_json } + let(:json_content) { { interactions: ["json"] }.to_json } let(:params) do { @@ -63,6 +63,16 @@ module Pacts expect(LatestPactPublicationIdForConsumerVersion.first.created_at).to eq PactBroker::Domain::Version.find(number: subject.consumer_version_number).created_at end + it "sets the interactions count" do + subject + expect(PactVersion.order(:id).last.interactions_count).to eq 1 + end + + it "sets the messages count" do + subject + expect(PactVersion.order(:id).last.messages_count).to eq 0 + end + context "when a pact already exists with exactly the same content" do let(:another_version) { Versions::Repository.new.create number: "2.0.0", pacticipant_id: consumer.id }