Skip to content

Commit

Permalink
fix(db clean): optimise calls to identify overwritten data to delete
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Sep 27, 2021
1 parent 3182203 commit aaef9e6
Showing 1 changed file with 41 additions and 23 deletions.
64 changes: 41 additions & 23 deletions lib/pact_broker/db/delete_overwritten_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,50 +51,68 @@ def dry_run?
end

def delete_webhook_data
ids_to_keep = db[:latest_triggered_webhooks].select(:id)
resolved_ids_to_delete = db[:triggered_webhooks]
.where(id: ids_to_keep)
.invert
.where(Sequel.lit("created_at < ?", cut_off_date))
.limit(limit)
.collect{ |row| row[:id] }
ltw_join = {
Sequel[:triggered_webhooks][:id] => Sequel[:ltw][:id]
}
resolved_ids_to_delete = db[:triggered_webhooks]
.select(Sequel[:triggered_webhooks][:id])
.left_join(:latest_triggered_webhooks, ltw_join, { table_alias: :ltw })
.where(Sequel[:ltw][:id] => nil)
.where(Sequel.lit("triggered_webhooks.created_at < ?", cut_off_date))
.order(Sequel[:triggered_webhooks][:id])
.limit(limit)
.collect{ |row| row[:id] }

PactBroker::Webhooks::TriggeredWebhook.where(id: resolved_ids_to_delete).delete unless dry_run?
{ triggered_webhooks: resolved_ids_to_delete.count }
end

def delete_orphan_pact_versions
referenced_pact_version_ids = db[:pact_publications].select(:pact_version_id).union(db[:verifications].select(:pact_version_id))
pact_version_ids_to_delete = db[:pact_versions].where(id: referenced_pact_version_ids).invert.order(:id).limit(limit).collect{ |row| row[:id] }
rpv_join = {
Sequel[:pact_versions][:id] => Sequel[:rpv][:pact_version_id]
}
pact_version_ids_to_delete = db[:pact_versions]
.select(Sequel[:pact_versions][:id])
.left_join(referenced_pact_version_ids, rpv_join, { table_alias: :rpv })
.where(Sequel[:rpv][:pact_version_id] => nil)
.order(Sequel[:pact_versions][:id])
.limit(limit)
.collect{ |row| row[:id] }
db[:pact_versions].where(id: pact_version_ids_to_delete).delete unless dry_run?
{ pact_versions: pact_version_ids_to_delete.count }
end

def delete_overwritten_pact_publications
ids_to_keep = db[:latest_pact_publication_ids_for_consumer_versions].select(:pact_publication_id)
lp_join = {
Sequel[:pact_publications][:id] => Sequel[:lp][:pact_publication_id]
}

resolved_ids_to_delete = db[:pact_publications]
.where(id: ids_to_keep)
.invert
.where(Sequel.lit("created_at < ?", cut_off_date))
.order(:id)
.limit(limit)
.collect{ |row| row[:id] }
.select(Sequel[:pact_publications][:id])
.left_join(:latest_pact_publication_ids_for_consumer_versions, lp_join, { table_alias: :lp })
.where(Sequel[:lp][:pact_publication_id] => nil)
.where(Sequel.lit("pact_publications.created_at < ?", cut_off_date))
.order(:id)
.limit(limit)
.collect{ |row| row[:id] }

PactBroker::Pacts::PactPublication.where(id: resolved_ids_to_delete).delete unless dry_run?

{ pact_publications: resolved_ids_to_delete.count }
end

def delete_overwritten_verifications
ids_to_keep = db[:latest_verification_id_for_pact_version_and_provider_version].select(:verification_id)
lv_join = {
Sequel[:verifications][:id] => Sequel[:lv][:verification_id]
}
resolved_ids_to_delete = db[:verifications]
.where(id: ids_to_keep)
.invert
.where(Sequel.lit("created_at < ?", cut_off_date))
.order(:id)
.limit(limit)
.collect{ |row| row[:id] }
.select(Sequel[:verifications][:id])
.left_join(:latest_verification_id_for_pact_version_and_provider_version, lv_join, { table_alias: :lv})
.where(Sequel[:lv][:verification_id] => nil)
.where(Sequel.lit("verifications.created_at < ?", cut_off_date))
.order(Sequel[:verifications][:id])
.limit(limit)
.collect{ |row| row[:id] }

PactBroker::Domain::Verification.where(id: resolved_ids_to_delete).delete unless dry_run?
{ verification_results: resolved_ids_to_delete.count }
Expand Down

0 comments on commit aaef9e6

Please sign in to comment.