Skip to content

Commit

Permalink
feat: optimise head matrix refresh when adding or deleting tags
Browse files Browse the repository at this point in the history
Only delete and update the lines with a matching consumer and consumer version tag name
  • Loading branch information
bethesque committed Mar 10, 2018
1 parent 52d587e commit 263c2a4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
20 changes: 20 additions & 0 deletions lib/pact_broker/matrix/head_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions lib/pact_broker/matrix/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions lib/pact_broker/matrix/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 263c2a4

Please sign in to comment.