Skip to content

Commit

Permalink
feat(matrix): make index refresh happen synchronously to prevent data…
Browse files Browse the repository at this point in the history
… corruption
  • Loading branch information
bethesque committed Feb 11, 2018
1 parent ca1c6bb commit acd64e6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
16 changes: 0 additions & 16 deletions lib/pact_broker/matrix/refresh_head_matrix_job.rb

This file was deleted.

3 changes: 1 addition & 2 deletions lib/pact_broker/matrix/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'pact_broker/matrix/row'
require 'pact_broker/matrix/latest_row'
require 'pact_broker/matrix/head_row'
require 'pact_broker/matrix/refresh_head_matrix_job'
require 'pact_broker/error'


Expand All @@ -25,7 +24,7 @@ class Repository

def refresh params
PactBroker::Matrix::Row.refresh(params)
RefreshHeadMatrixJob.perform_in(1, params: params)
PactBroker::Matrix::HeadRow.refresh(params)
end

# Return the latest matrix row (pact/verification) for each consumer_version_number/provider_version_number
Expand Down
17 changes: 12 additions & 5 deletions lib/pact_broker/matrix/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Row < Sequel::Model(:materialized_matrix)
def refresh params
logger.debug("Refreshing #{model.table_name} for #{params}")

db = model.db
table_name = model.table_name

source_view_name = model.table_name.to_s.gsub('materialized_', '').to_sym
if params[:consumer_name] || params[:provider_name]
criteria = {}
Expand All @@ -35,17 +38,21 @@ def refresh params
criteria[:provider_id] = pacticipant.id if pacticipant
end
if criteria.any?
db[model.table_name].where(criteria).delete
db[model.table_name].insert(db[source_view_name].where(criteria))
db.transaction do
db[table_name].where(criteria).delete
db[table_name].insert(db[source_view_name].where(criteria).distinct)
end
end
end

if params[:pacticipant_name]
pacticipant = PactBroker::Domain::Pacticipant.where(name_like(:name, params[:pacticipant_name])).single_record
if pacticipant
db[model.table_name].where(consumer_id: pacticipant.id).or(provider_id: pacticipant.id).delete
new_rows = db[source_view_name].where(consumer_id: pacticipant.id).or(provider_id: pacticipant.id)
db[model.table_name].insert(new_rows)
db.transaction do
db[table_name].where(consumer_id: pacticipant.id).or(provider_id: pacticipant.id).delete
new_rows = db[source_view_name].where(consumer_id: pacticipant.id).or(provider_id: pacticipant.id).distinct
db[table_name].insert(new_rows)
end
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion spec/lib/pact_broker/matrix/row_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
module PactBroker
module Matrix
describe Row do
describe "refresh" do
describe "refresh", migration: true do
before do
PactBroker::Database.migrate
end

let(:td) { TestDataBuilder.new(auto_refresh_matrix: false) }

before do
Expand Down

0 comments on commit acd64e6

Please sign in to comment.