From acd64e66ee7b614465d1d1d6788d6e367e0fbbb8 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 12 Feb 2018 10:36:15 +1100 Subject: [PATCH] feat(matrix): make index refresh happen synchronously to prevent data corruption --- .../matrix/refresh_head_matrix_job.rb | 16 ---------------- lib/pact_broker/matrix/repository.rb | 3 +-- lib/pact_broker/matrix/row.rb | 17 ++++++++++++----- spec/lib/pact_broker/matrix/row_spec.rb | 6 +++++- 4 files changed, 18 insertions(+), 24 deletions(-) delete mode 100644 lib/pact_broker/matrix/refresh_head_matrix_job.rb diff --git a/lib/pact_broker/matrix/refresh_head_matrix_job.rb b/lib/pact_broker/matrix/refresh_head_matrix_job.rb deleted file mode 100644 index a2fe536ac..000000000 --- a/lib/pact_broker/matrix/refresh_head_matrix_job.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'sucker_punch' -require 'pact_broker/matrix/head_row' - -module PactBroker - module Matrix - class RefreshHeadMatrixJob - - include SuckerPunch::Job - include PactBroker::Logging - - def perform params - PactBroker::Matrix::HeadRow.refresh(params[:params]) - end - end - end -end diff --git a/lib/pact_broker/matrix/repository.rb b/lib/pact_broker/matrix/repository.rb index 79b262317..21295cff0 100644 --- a/lib/pact_broker/matrix/repository.rb +++ b/lib/pact_broker/matrix/repository.rb @@ -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' @@ -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 diff --git a/lib/pact_broker/matrix/row.rb b/lib/pact_broker/matrix/row.rb index 7af35e81c..a6df45246 100644 --- a/lib/pact_broker/matrix/row.rb +++ b/lib/pact_broker/matrix/row.rb @@ -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 = {} @@ -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 diff --git a/spec/lib/pact_broker/matrix/row_spec.rb b/spec/lib/pact_broker/matrix/row_spec.rb index 3b258dd52..ef8948922 100644 --- a/spec/lib/pact_broker/matrix/row_spec.rb +++ b/spec/lib/pact_broker/matrix/row_spec.rb @@ -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