|
| 1 | +=begin |
| 2 | +The Matrix::Row is based on the matrix view which does a join of every pact/verification |
| 3 | +and then selects the relevant ones. |
| 4 | +
|
| 5 | +The Matrix::QuickRow starts with the relevant rows, and builds the matrix query from that, |
| 6 | +making it much quicker. |
| 7 | +
|
| 8 | +=end |
| 9 | +require 'sequel' |
| 10 | +require 'pact_broker/repositories/helpers' |
| 11 | +require 'pact_broker/logging' |
| 12 | +require 'pact_broker/pacts/pact_version' |
| 13 | +require 'pact_broker/domain/pacticipant' |
| 14 | +require 'pact_broker/domain/version' |
| 15 | +require 'pact_broker/domain/verification' |
| 16 | +require 'pact_broker/pacts/pact_publication' |
| 17 | +require 'pact_broker/tags/tag_with_latest_flag' |
| 18 | + |
| 19 | +module PactBroker |
| 20 | + module Matrix |
| 21 | + LV = :latest_verification_id_for_pact_version_and_provider_version |
| 22 | + LP = :latest_pact_publication_ids_for_consumer_versions |
| 23 | + |
| 24 | + CONSUMER_COLUMNS = [Sequel[:lp][:consumer_id], Sequel[:consumers][:name].as(:consumer_name), Sequel[:lp][:pact_publication_id], Sequel[:lp][:pact_version_id]] |
| 25 | + PROVIDER_COLUMNS = [Sequel[:lp][:provider_id], Sequel[:providers][:name].as(:provider_name), Sequel[:lv][:verification_id]] |
| 26 | + CONSUMER_VERSION_COLUMNS = [Sequel[:lp][:consumer_version_id], Sequel[:cv][:number].as(:consumer_version_number), Sequel[:cv][:order].as(:consumer_version_order)] |
| 27 | + PROVIDER_VERSION_COLUMNS = [Sequel[:lv][:provider_version_id], Sequel[:pv][:number].as(:provider_version_number), Sequel[:pv][:order].as(:provider_version_order)] |
| 28 | + ALL_COLUMNS = CONSUMER_COLUMNS + CONSUMER_VERSION_COLUMNS + PROVIDER_COLUMNS + PROVIDER_VERSION_COLUMNS |
| 29 | + |
| 30 | + LP_LV_JOIN = { Sequel[:lp][:pact_version_id] => Sequel[:lv][:pact_version_id] } |
| 31 | + CONSUMER_JOIN = { Sequel[:lp][:consumer_id] => Sequel[:consumers][:id] } |
| 32 | + PROVIDER_JOIN = { Sequel[:lp][:provider_id] => Sequel[:providers][:id] } |
| 33 | + CONSUMER_VERSION_JOIN = { Sequel[:lp][:consumer_version_id] => Sequel[:cv][:id] } |
| 34 | + PROVIDER_VERSION_JOIN = { Sequel[:lv][:provider_version_id] => Sequel[:pv][:id] } |
| 35 | + |
| 36 | + RAW_QUERY = Sequel::Model.db[Sequel.as(LP, :lp)] |
| 37 | + .select(*ALL_COLUMNS) |
| 38 | + .left_outer_join(LV, LP_LV_JOIN, { table_alias: :lv } ) |
| 39 | + .join(:pacticipants, CONSUMER_JOIN, { table_alias: :consumers }) |
| 40 | + .join(:pacticipants, PROVIDER_JOIN, { table_alias: :providers }) |
| 41 | + .join(:versions, CONSUMER_VERSION_JOIN, { table_alias: :cv }) |
| 42 | + .left_outer_join(:versions, PROVIDER_VERSION_JOIN, { table_alias: :pv } ) |
| 43 | + |
| 44 | + ALIASED_QUERY = Sequel.as(RAW_QUERY, :smart_rows) |
| 45 | + |
| 46 | + class QuickRow < Sequel::Model(ALIASED_QUERY) |
| 47 | + CONSUMER_ID = Sequel[:smart_rows][:consumer_id] |
| 48 | + PROVIDER_ID = Sequel[:smart_rows][:provider_id] |
| 49 | + CONSUMER_VERSION_ID = Sequel[:smart_rows][:consumer_version_id] |
| 50 | + PROVIDER_VERSION_ID = Sequel[:smart_rows][:provider_version_id] |
| 51 | + PACT_PUBLICATION_ID = Sequel[:smart_rows][:pact_publication_id] |
| 52 | + VERIFICATION_ID = Sequel[:smart_rows][:verification_id] |
| 53 | + |
| 54 | + associate(:many_to_one, :pact_publication, :class => "PactBroker::Pacts::PactPublication", :key => :pact_publication_id, :primary_key => :id) |
| 55 | + associate(:many_to_one, :provider, :class => "PactBroker::Domain::Pacticipant", :key => :provider_id, :primary_key => :id) |
| 56 | + associate(:many_to_one, :consumer, :class => "PactBroker::Domain::Pacticipant", :key => :consumer_id, :primary_key => :id) |
| 57 | + associate(:many_to_one, :consumer_version, :class => "PactBroker::Domain::Version", :key => :consumer_version_id, :primary_key => :id) |
| 58 | + associate(:many_to_one, :provider_version, :class => "PactBroker::Domain::Version", :key => :provider_version_id, :primary_key => :id) |
| 59 | + associate(:many_to_one, :pact_version, class: "PactBroker::Pacts::PactVersion", :key => :pact_version_id, :primary_key => :id) |
| 60 | + associate(:many_to_one, :verification, class: "PactBroker::Domain::Verification", :key => :verification_id, :primary_key => :id) |
| 61 | + associate(:one_to_many, :consumer_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :consumer_version_id, key: :version_id) |
| 62 | + associate(:one_to_many, :provider_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :provider_version_id, key: :version_id) |
| 63 | + |
| 64 | + dataset_module do |
| 65 | + include PactBroker::Repositories::Helpers |
| 66 | + include PactBroker::Logging |
| 67 | + |
| 68 | + def consumer_id consumer_id |
| 69 | + where(CONSUMER_ID => consumer_id) |
| 70 | + end |
| 71 | + |
| 72 | + def matching_selectors selectors |
| 73 | + if selectors.size == 1 |
| 74 | + where_consumer_or_provider_is(selectors.first) |
| 75 | + else |
| 76 | + where_consumer_and_provider_in(selectors) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + # find rows where (the consumer (and optional version) matches any of the selectors) |
| 81 | + # AND |
| 82 | + # the (provider (and optional version) matches any of the selectors OR the provider matches |
| 83 | + # and the verification is missing (and hence the provider version is null)) |
| 84 | + def where_consumer_and_provider_in selectors |
| 85 | + where{ |
| 86 | + Sequel.&( |
| 87 | + Sequel.|( |
| 88 | + *QueryHelper.consumer_and_maybe_consumer_version_match_any_selector(selectors) |
| 89 | + ), |
| 90 | + Sequel.|( |
| 91 | + *QueryHelper.provider_and_maybe_provider_version_match_any_selector_or_verification_is_missing(selectors) |
| 92 | + ), |
| 93 | + QueryHelper.either_consumer_or_provider_was_specified_in_query(selectors) |
| 94 | + ) |
| 95 | + } |
| 96 | + end |
| 97 | + |
| 98 | + # Can't access other dataset_module methods from inside the Sequel `where{ ... }` block, so make a private class |
| 99 | + # with some helper methods |
| 100 | + class QueryHelper |
| 101 | + def self.consumer_and_maybe_consumer_version_match_any_selector(selectors) |
| 102 | + selectors.collect { |s| consumer_and_maybe_consumer_version_match_selector(s) } |
| 103 | + end |
| 104 | + |
| 105 | + def self.consumer_and_maybe_consumer_version_match_selector(s) |
| 106 | + if s[:pact_publication_ids] |
| 107 | + Sequel.&(PACT_PUBLICATION_ID => s[:pact_publication_ids]) |
| 108 | + elsif s[:pacticipant_version_id] |
| 109 | + Sequel.&(CONSUMER_ID => s[:pacticipant_id], CONSUMER_VERSION_ID => s[:pacticipant_version_id]) |
| 110 | + else |
| 111 | + Sequel.&(CONSUMER_ID => s[:pacticipant_id]) |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + def self.provider_and_maybe_provider_version_match_selector(s) |
| 116 | + if s[:verification_ids] |
| 117 | + Sequel.&(VERIFICATION_ID => s[:verification_ids]) |
| 118 | + elsif s[:pacticipant_version_id] |
| 119 | + Sequel.&(PROVIDER_ID => s[:pacticipant_id], PROVIDER_VERSION_ID => s[:pacticipant_version_id]) |
| 120 | + else |
| 121 | + Sequel.&(PROVIDER_ID => s[:pacticipant_id]) |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + # if the pact for a consumer version has never been verified, it exists in the matrix as a row |
| 126 | + # with a blank provider version id |
| 127 | + def self.provider_verification_is_missing_for_matching_selector(s) |
| 128 | + Sequel.&(PROVIDER_ID => s[:pacticipant_id], PROVIDER_VERSION_ID => nil) |
| 129 | + end |
| 130 | + |
| 131 | + def self.provider_and_maybe_provider_version_match_any_selector_or_verification_is_missing(selectors) |
| 132 | + selectors.collect { |s| |
| 133 | + provider_and_maybe_provider_version_match_selector(s) |
| 134 | + } + selectors.collect { |s| |
| 135 | + provider_verification_is_missing_for_matching_selector(s) |
| 136 | + } |
| 137 | + end |
| 138 | + |
| 139 | + # Some selecters are specified in the query, others are implied (when only one pacticipant is specified, |
| 140 | + # the integrations are automatically worked out, and the selectors for these are of type :implied ) |
| 141 | + # When there are 3 pacticipants that each have dependencies on each other (A->B, A->C, B->C), the query |
| 142 | + # to deploy C (implied A, implied B, specified C) was returning the A->B row because it matched the |
| 143 | + # implied selectors as well. |
| 144 | + # This extra filter makes sure that every row that is returned actually matches one of the specified |
| 145 | + # selectors. |
| 146 | + def self.either_consumer_or_provider_was_specified_in_query(selectors) |
| 147 | + specified_pacticipant_ids = selectors.select{ |s| s[:type] == :specified }.collect{ |s| s[:pacticipant_id] } |
| 148 | + Sequel.|({ CONSUMER_ID => specified_pacticipant_ids } , { PROVIDER_ID => specified_pacticipant_ids }) |
| 149 | + end |
| 150 | + end |
| 151 | + |
| 152 | + def where_consumer_or_provider_is s |
| 153 | + where{ |
| 154 | + Sequel.|( |
| 155 | + s[:pacticipant_version_id] ? Sequel.&(CONSUMER_ID => s[:pacticipant_id], CONSUMER_VERSION_ID => s[:pacticipant_version_id]) : Sequel.&(CONSUMER_ID => s[:pacticipant_id]), |
| 156 | + s[:pacticipant_version_id] ? Sequel.&(PROVIDER_ID => s[:pacticipant_id], PROVIDER_VERSION_ID => s[:pacticipant_version_id]) : Sequel.&(PROVIDER_ID => s[:pacticipant_id]) |
| 157 | + ) |
| 158 | + } |
| 159 | + end |
| 160 | + |
| 161 | + def order_by_names_ascending_most_recent_first |
| 162 | + order( |
| 163 | + Sequel.asc(:consumer_name), |
| 164 | + Sequel.desc(:consumer_version_order), |
| 165 | + Sequel.asc(:provider_name), |
| 166 | + Sequel.desc(:provider_version_order), |
| 167 | + Sequel.desc(:verification_id)) |
| 168 | + end |
| 169 | + |
| 170 | + def eager_all_the_things |
| 171 | + eager(:consumer) |
| 172 | + .eager(:provider) |
| 173 | + .eager(:consumer_version) |
| 174 | + .eager(:provider_version) |
| 175 | + .eager(:verification) |
| 176 | + .eager(:pact_publication) |
| 177 | + .eager(:pact_version) |
| 178 | + end |
| 179 | + |
| 180 | + end |
| 181 | + |
| 182 | + def success |
| 183 | + verification&.success |
| 184 | + end |
| 185 | + |
| 186 | + def pact_version_sha |
| 187 | + pact_version.sha |
| 188 | + end |
| 189 | + |
| 190 | + def pact_revision_number |
| 191 | + pact_publication.revision_number |
| 192 | + end |
| 193 | + |
| 194 | + def verification_number |
| 195 | + verification&.number |
| 196 | + end |
| 197 | + |
| 198 | + def success |
| 199 | + verification&.success |
| 200 | + end |
| 201 | + |
| 202 | + def pact_created_at |
| 203 | + pact_publication.created_at |
| 204 | + end |
| 205 | + |
| 206 | + def verification_executed_at |
| 207 | + verification.execution_date |
| 208 | + end |
| 209 | + |
| 210 | + # Add logic for ignoring case |
| 211 | + def <=> other |
| 212 | + comparisons = [ |
| 213 | + compare_name_asc(consumer_name, other.consumer_name), |
| 214 | + compare_number_desc(consumer_version_order, other.consumer_version_order), |
| 215 | + compare_number_desc(pact_revision_number, other.pact_revision_number), |
| 216 | + compare_name_asc(provider_name, other.provider_name), |
| 217 | + compare_number_desc(provider_version_order, other.provider_version_order), |
| 218 | + compare_number_desc(verification_id, other.verification_id) |
| 219 | + ] |
| 220 | + |
| 221 | + comparisons.find{|c| c != 0 } || 0 |
| 222 | + end |
| 223 | + |
| 224 | + def compare_name_asc name1, name2 |
| 225 | + name1 <=> name2 |
| 226 | + end |
| 227 | + |
| 228 | + def to_s |
| 229 | + "#{consumer_name} v#{consumer_version_number} #{provider_name} #{provider_version_number} #{success}" |
| 230 | + end |
| 231 | + |
| 232 | + def compare_number_desc number1, number2 |
| 233 | + if number1 && number2 |
| 234 | + number2 <=> number1 |
| 235 | + elsif number1 |
| 236 | + 1 |
| 237 | + else |
| 238 | + -1 |
| 239 | + end |
| 240 | + end |
| 241 | + |
| 242 | + def eql?(obj) |
| 243 | + (obj.class == model) && (obj.values == values) |
| 244 | + end |
| 245 | + |
| 246 | + def pacticipant_names |
| 247 | + [consumer_name, provider_name] |
| 248 | + end |
| 249 | + |
| 250 | + def involves_pacticipant_with_name?(pacticipant_name) |
| 251 | + pacticipant_name.include?(pacticipant_name) |
| 252 | + end |
| 253 | + end |
| 254 | + end |
| 255 | +end |
0 commit comments