diff --git a/lib/pact_broker/pacts/pact_publication_dataset_module.rb b/lib/pact_broker/pacts/pact_publication_dataset_module.rb index e4260f955..bdfb5c431 100644 --- a/lib/pact_broker/pacts/pact_publication_dataset_module.rb +++ b/lib/pact_broker/pacts/pact_publication_dataset_module.rb @@ -57,6 +57,7 @@ def for_consumer_name_and_maybe_version_number(consumer_name, consumer_version_n end end + # TODO use the branch heads here def latest_by_consumer_branch branch_versions_join = { Sequel[:pact_publications][:consumer_version_id] => Sequel[:branch_versions][:version_id] diff --git a/lib/pact_broker/pacts/pact_publication_wip_dataset_module.rb b/lib/pact_broker/pacts/pact_publication_wip_dataset_module.rb index eb6ade199..9613089d4 100644 --- a/lib/pact_broker/pacts/pact_publication_wip_dataset_module.rb +++ b/lib/pact_broker/pacts/pact_publication_wip_dataset_module.rb @@ -1,35 +1,76 @@ module PactBroker module Pacts module PactPublicationWipDatasetModule + + # Use a cut down model of the verifications table just for the WIP calculations. + # Don't need all the associations and normal domain methods. + class VerificationForWipCalculations < Sequel::Model(:verifications) + dataset_module do + def successful_non_wip_by_provider(provider_id) + distinct.where(success: true, wip: false, provider_id: provider_id) + end + + def verified_before_creation_date_of(record) + if record + verified_before_date(record.created_at) + else + self + end + end + + def join_branch_versions_excluding_branch(provider_id, branch_name) + branch_versions_join = { + Sequel[:verifications][:provider_version_id] => Sequel[:branch_versions][:version_id], + Sequel[:branch_versions][:pacticipant_id] => provider_id + } + join(:branch_versions, branch_versions_join) do + Sequel.lit("branch_versions.branch_name != ?", branch_name) + end + end + + def join_provider_versions_for_provider_id_and_branch(provider_id, provider_version_branch) + branch_versions_join = { + Sequel[:verifications][:provider_version_id] => Sequel[:branch_versions][:version_id], + Sequel[:branch_versions][:pacticipant_id] => provider_id, + Sequel[:branch_versions][:branch_name] => provider_version_branch + } + + join(:branch_versions, branch_versions_join) + end + + def verified_before_date(date) + where { Sequel[:verifications][:execution_date] < date } + end + end + end + def successfully_verified_by_provider_branch_when_not_wip(provider_id, provider_version_branch) + successful_verifications = VerificationForWipCalculations + .select(:pact_version_id) + .distinct + .successful_non_wip_by_provider(provider_id) + .join_provider_versions_for_provider_id_and_branch(provider_id, provider_version_branch) + + from_self(alias: :pp) .select(Sequel[:pp].*) - .where(Sequel[:pp][:provider_id] => provider_id) - .join_successful_non_wip_verifications_for_provider_id(provider_id) - .join_provider_versions_for_provider_id_and_branch(provider_id, provider_version_branch) .distinct + .join(successful_verifications, { Sequel[:pp][:pact_version_id] => Sequel[:v][:pact_version_id] }, { table_alias: :v }) end def successfully_verified_by_provider_another_branch_before_this_branch_first_created(provider_id, provider_version_branch) first_version_for_branch = PactBroker::Domain::Version.first_for_pacticipant_id_and_branch(provider_id, provider_version_branch) + successful_verifications = VerificationForWipCalculations + .select(:pact_version_id) + .distinct + .successful_non_wip_by_provider(provider_id) + .join_branch_versions_excluding_branch(provider_id, provider_version_branch) + .verified_before_creation_date_of(first_version_for_branch) + from_self(alias: :pp) .select(Sequel[:pp].*) - .join_successful_non_wip_verifications_for_provider_id(provider_id) - .join_provider_versions_for_provider_id(provider_id) - .join_branch_versions_excluding_branch(provider_version_branch) - .where(Sequel[:pp][:provider_id] => provider_id) - .verified_before_creation_date_of(first_version_for_branch) - .distinct - end - - def join_branch_versions_excluding_branch(branch_name) - branch_versions_join = { - Sequel[:provider_versions][:id] => Sequel[:branch_versions][:version_id] - } - join(:branch_versions, branch_versions_join) do - Sequel.lit("branch_versions.branch_name != ?", branch_name) - end + .join(successful_verifications, { Sequel[:pp][:pact_version_id] => Sequel[:v][:pact_version_id] }, { table_alias: :v }) end def successfully_verified_by_provider_tag_when_not_wip(provider_tag) @@ -43,7 +84,6 @@ def successfully_verified_by_provider_tag_when_not_wip(provider_tag) .select(Sequel[:pp].*) .join(:pact_version_provider_tag_successful_verifications, pact_version_provider_tag_verifications_join, { table_alias: :sv }) .distinct - end def successfully_verified_by_provider_another_tag_before_this_tag_first_created(provider_id, provider_tag) @@ -71,66 +111,6 @@ def successfully_verified_by_provider_another_tag_before_this_tag_first_created( end .distinct end - - protected - - def verified_before_date(date) - where { Sequel[:verifications][:execution_date] < date } - end - - def join_successful_non_wip_verifications_for_provider_id(provider_id, &block) - verifications_join = { - pact_version_id: :pact_version_id, - Sequel[:verifications][:success] => true, - Sequel[:verifications][:wip] => false, - Sequel[:verifications][:provider_id] => provider_id - } - join(:verifications, verifications_join, {}, &block) - end - - def join_provider_version_tags &block - tags_join = { - Sequel[:verifications][:provider_version_id] => Sequel[:provider_tags][:version_id], - } - join(:tags, tags_join, { table_alias: :provider_tags }, &block) - end - - def join_provider_version_tags_for_tag(tag) - tags_join = { - Sequel[:verifications][:provider_version_id] => Sequel[:provider_tags][:version_id], - Sequel[:provider_tags][:name] => tag - } - join(:tags, tags_join, { table_alias: :provider_tags } ) - end - - def join_provider_versions_for_provider_id_and_branch(provider_id, provider_version_branch) - versions_join = { - Sequel[:verifications][:provider_version_id] => Sequel[:provider_versions][:id], - Sequel[:provider_versions][:pacticipant_id] => provider_id - } - branch_versions_join = { - Sequel[:provider_versions][:id] => Sequel[:branch_versions][:version_id], - Sequel[:branch_versions][:branch_name] => provider_version_branch - } - join(:versions, versions_join, { table_alias: :provider_versions } ) - .join(:branch_versions, branch_versions_join) - end - - def join_provider_versions_for_provider_id(provider_id, &block) - versions_join = { - Sequel[:verifications][:provider_version_id] => Sequel[:provider_versions][:id], - Sequel[:provider_versions][:pacticipant_id] => provider_id - } - join(:versions, versions_join, { table_alias: :provider_versions }, &block) - end - - def verified_before_creation_date_of(record) - if record - verified_before_date(record.created_at) - else - self - end - end end end end