Skip to content

Commit

Permalink
make sure to cleanup clickhouse events after billable metric removal
Browse files Browse the repository at this point in the history
  • Loading branch information
nudded committed Nov 25, 2024
1 parent 8435425 commit b8c5218
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
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
24 changes: 24 additions & 0 deletions spec/jobs/billable_metrics/delete_events_job_spec.rb
Original file line number Diff line number Diff line change
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', clickhouse: true 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', clickhouse: true 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

0 comments on commit b8c5218

Please sign in to comment.