Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug(Events) - make sure to cleanup clickhouse events after billable metric removal #2863

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/jobs/billable_metrics/delete_events_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions spec/factories/clickhouse/events_enriched.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
26 changes: 25 additions & 1 deletion spec/jobs/billable_metrics/delete_events_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down Expand Up @@ -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