diff --git a/lib/pact_broker/matrix/head_row.rb b/lib/pact_broker/matrix/head_row.rb index 61dceb7c7..0ace3ea4c 100644 --- a/lib/pact_broker/matrix/head_row.rb +++ b/lib/pact_broker/matrix/head_row.rb @@ -6,6 +6,26 @@ module Matrix # Rows with a nil consumer_tag_name are the overall latest class HeadRow < Row set_dataset(:materialized_head_matrix) + + dataset_module do + include PactBroker::Repositories::Helpers + include PactBroker::Logging + + def refresh ids + return super unless ids[:tag_name] + + logger.debug("Refreshing #{model.table_name} for #{ids}") + db = model.db + table_name = model.table_name + criteria = { consumer_id: ids[:pacticipant_id], consumer_version_tag_name: ids[:tag_name] } + db.transaction do + db[table_name].where(criteria).delete + # Not sure if the distinct is necessary. Think I put it in as an attempt to fix the duplicate rows in the index but it didn't work. + new_rows = db[source_view_name].where(criteria).distinct + db[table_name].insert(new_rows) + end + end + end end end end diff --git a/lib/pact_broker/matrix/repository.rb b/lib/pact_broker/matrix/repository.rb index 31f24142d..74dac79f8 100644 --- a/lib/pact_broker/matrix/repository.rb +++ b/lib/pact_broker/matrix/repository.rb @@ -60,6 +60,7 @@ def find_ids_for_pacticipant_names params criteria[:pacticipant_id] = pacticipant.id if pacticipant end + criteria[:tag_name] = params[:tag_name] if params[:tag_name].is_a?(String) # Could be a sym from resource parameters in api.rb criteria end diff --git a/lib/pact_broker/matrix/row.rb b/lib/pact_broker/matrix/row.rb index 2a60de1ce..2c7b21680 100644 --- a/lib/pact_broker/matrix/row.rb +++ b/lib/pact_broker/matrix/row.rb @@ -32,9 +32,11 @@ def refresh ids db[table_name].insert(new_rows) end elsif ids.any? + accepted_columns = [:consumer_id, :consumer_name, :provider_id, :provider_name] + criteria = ids.reject{ |k, v| !accepted_columns.include?(k) } db.transaction do - db[table_name].where(ids).delete - db[table_name].insert(db[source_view_name].where(ids).distinct) + db[table_name].where(criteria).delete + db[table_name].insert(db[source_view_name].where(criteria).distinct) end end end