From cbe64859a5730d82922ff9d863710fa3a7c2eec8 Mon Sep 17 00:00:00 2001 From: aakashb95 Date: Mon, 12 Jan 2026 12:33:37 +0530 Subject: [PATCH] fix: make code more readable, remove unnecessary comments --- .../enterprise/account/plan_usage_and_limits.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/enterprise/app/models/enterprise/account/plan_usage_and_limits.rb b/enterprise/app/models/enterprise/account/plan_usage_and_limits.rb index 2a5bec2e7..53dddd67c 100644 --- a/enterprise/app/models/enterprise/account/plan_usage_and_limits.rb +++ b/enterprise/app/models/enterprise/account/plan_usage_and_limits.rb @@ -15,16 +15,19 @@ module Enterprise::Account::PlanUsageAndLimits } end - # Use atomic SQL update to prevent race condition when multiple jobs increment concurrently. - # Skipping validations is safe: no validations on custom_attributes, no dependent callbacks. def increment_response_usage - self.class.where(id: id).update_all( # rubocop:disable Rails/SkipsModelValidations - "custom_attributes = jsonb_set( + increment_sql = <<~SQL.squish + + custom_attributes = jsonb_set( custom_attributes, '{#{CAPTAIN_RESPONSES_USAGE}}', - (COALESCE((custom_attributes->>'#{CAPTAIN_RESPONSES_USAGE}')::int, 0) + 1)::text::jsonb - )" - ) + to_jsonb(COALESCE((custom_attributes->>'#{CAPTAIN_RESPONSES_USAGE}')::int, 0) + 1), + true + ) + SQL + + updated = self.class.where(id: id).update_all(increment_sql) + reload if updated.positive? end def reset_response_usage