From ea0a9ea029236c558a8136008fed99118f5a6bf3 Mon Sep 17 00:00:00 2001 From: Toon Willems Date: Mon, 25 Nov 2024 17:35:31 +0100 Subject: [PATCH] make sure to cleanup clickhouse events after billable metric removal --- .../billable_metrics/delete_events_job.rb | 17 ++++++++++++ spec/factories/clickhouse/events_enriched.rb | 1 + .../delete_events_job_spec.rb | 26 ++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/app/jobs/billable_metrics/delete_events_job.rb b/app/jobs/billable_metrics/delete_events_job.rb index 9352b0d0825..4d5b1170612 100644 --- a/app/jobs/billable_metrics/delete_events_job.rb +++ b/app/jobs/billable_metrics/delete_events_job.rb @@ -25,6 +25,23 @@ def perform(metric) .where(billable_metric_id: metric.id) .joins(plan: :subscriptions).pluck('subscriptions.external_id') ).update_all(deleted_at:) # rubocop:disable Rails/SkipsModelValidations + + # Delete events_raw & events_enriched on clickhouse using `external_subscription_id` + Clickhouse::EventsRaw.where( + organization_id: metric.organization.id, + code: metric.code, + external_subscription_id: Charge.with_discarded + .where(billable_metric_id: metric.id) + .joins(plan: :subscriptions).pluck('subscriptions.external_id') + ).delete_all + + Clickhouse::EventsEnriched.where( + organization_id: metric.organization.id, + code: metric.code, + external_subscription_id: Charge.with_discarded + .where(billable_metric_id: metric.id) + .joins(plan: :subscriptions).pluck('subscriptions.external_id') + ).delete_all end end end diff --git a/spec/factories/clickhouse/events_enriched.rb b/spec/factories/clickhouse/events_enriched.rb index 3f462fd53bc..35b0a7cc3d0 100644 --- a/spec/factories/clickhouse/events_enriched.rb +++ b/spec/factories/clickhouse/events_enriched.rb @@ -6,6 +6,7 @@ subscription { create(:subscription, customer:) } customer { create(:customer) } organization { customer.organization } + billable_metric { create(:billable_metric, organization: organization) } end organization_id { organization.id } diff --git a/spec/jobs/billable_metrics/delete_events_job_spec.rb b/spec/jobs/billable_metrics/delete_events_job_spec.rb index 96b23f2d47a..3b4ed4e4f80 100644 --- a/spec/jobs/billable_metrics/delete_events_job_spec.rb +++ b/spec/jobs/billable_metrics/delete_events_job_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -RSpec.describe BillableMetrics::DeleteEventsJob, type: :job, transaction: false do +RSpec.describe BillableMetrics::DeleteEventsJob, type: :job, transaction: false, clickhouse: true do let(:billable_metric) { create(:billable_metric, :deleted) } let(:subscription) { create(:subscription) } @@ -31,4 +31,28 @@ expect(not_impacted_event.reload.deleted_at).to be_nil end end + + it 'deletes new-style external_subscription based events stored in clickhouse' do + create(:standard_charge, plan: subscription.plan, billable_metric:) + not_impacted_event = create(:clickhouse_events_raw, external_subscription_id: SecureRandom.uuid, organization_id: billable_metric.organization_id) + event = create(:clickhouse_events_raw, code: billable_metric.code, external_subscription_id: subscription.external_id, organization_id: billable_metric.organization_id) + + freeze_time do + described_class.perform_now(billable_metric) + expect(Clickhouse::EventsRaw.find_by(transaction_id: event.transaction_id)).to eq(nil) + expect(Clickhouse::EventsRaw.find_by(transaction_id: not_impacted_event.transaction_id)).not_to eq(nil) + end + end + + it 'deletes new-style external_subscription based events_enriched stored in clickhouse' do + create(:standard_charge, plan: subscription.plan, billable_metric:) + not_impacted_event = create(:clickhouse_events_enriched, external_subscription_id: SecureRandom.uuid, organization_id: billable_metric.organization_id) + event = create(:clickhouse_events_enriched, code: billable_metric.code, external_subscription_id: subscription.external_id, organization_id: billable_metric.organization_id) + + freeze_time do + described_class.perform_now(billable_metric) + expect(Clickhouse::EventsEnriched.find_by(transaction_id: event.transaction_id)).to eq(nil) + expect(Clickhouse::EventsEnriched.find_by(transaction_id: not_impacted_event.transaction_id)).not_to eq(nil) + end + end end