A platform for community discussion. Free, open, simple.
DEV: Remove deprecated `daily_usage` (#36570)
Keegan George6df7cbb70ca3
diff --git a/plugins/discourse-ai/app/models/llm_credit_allocation.rb b/plugins/discourse-ai/app/models/llm_credit_allocation.rbindex 884f8af3a37..cadb0cf4357 100644--- a/plugins/discourse-ai/app/models/llm_credit_allocation.rb+++ b/plugins/discourse-ai/app/models/llm_credit_allocation.rb@@ -12,10 +12,6 @@ class LlmCreditAllocation < ActiveRecord::Baseendend- # DEPRECATED: Only used for JSONB daily_usage cleanup during migration period- # TODO: Remove this constant once daily_usage column is dropped- DAILY_USAGE_RETENTION_DAYS = 90-belongs_to :llm_modelhas_many :daily_usages,class_name: "LlmCreditDailyUsage",@@ -23,10 +19,11 @@ class LlmCreditAllocation < ActiveRecord::Baseprimary_key: :llm_model_id,dependent: :destroy- # TODO: Remove once both column-dropping migrations have been promoted to pre-deploy:+ # TODO: Remove once column-dropping migrations have been promoted to pre-deploy:# - 20251105174003 (drops monthly_used, last_reset_at)# - 20251117000003 (drops monthly_credits, monthly_usage)- self.ignored_columns = %w[monthly_used last_reset_at monthly_credits monthly_usage]+ # - 20251209180201 (drops daily_usage)+ self.ignored_columns = %w[monthly_used last_reset_at monthly_credits monthly_usage daily_usage]validates :llm_model_id, presence: true, uniqueness: truevalidates :daily_credits, presence: true, numericality: { only_integer: true, greater_than: 0 }@@ -38,14 +35,8 @@ class LlmCreditAllocation < ActiveRecord::Baseless_than_or_equal_to: 100,}- # DEPRECATED: Used for JSONB daily_usage column which is being phased out- # TODO: Remove once daily_usage column is dropped- def current_day_key- Time.current.utc.strftime("%Y-%m-%d")- end-def daily_used- # Primary: Use llm_credit_daily_usages table+ # Use llm_credit_daily_usages table# Check if association is preloaded to avoid N+1 queriesif association(:daily_usages).loaded?usage_record = daily_usages.find { |u| u.usage_date == Date.current }@@ -56,10 +47,6 @@ class LlmCreditAllocation < ActiveRecord::Basereturn usage_record.credits_used if usage_recordend- # DEPRECATED: Fallback to JSONB daily_usage column for backwards compatibility- # TODO: Remove this fallback once daily_usage column is dropped and added to ignored_columns- return daily_usage[current_day_key].to_i if respond_to?(:daily_usage) && daily_usage.present?-0end@@ -67,13 +54,6 @@ class LlmCreditAllocation < ActiveRecord::Baseusage_record =LlmCreditDailyUsage.find_or_create_by!(llm_model_id: llm_model_id, usage_date: Date.current)usage_record.update!(credits_used: value.to_i)-- # DEPRECATED: Dual-write to JSONB daily_usage column during migration period- # TODO: Remove this dual-write once daily_usage column is dropped- if respond_to?(:daily_usage=)- day_key = current_day_key- self.daily_usage = (daily_usage || {}).merge(day_key => value.to_i)- endenddef credits_remaining@@ -112,22 +92,6 @@ class LlmCreditAllocation < ActiveRecord::Basedef deduct_credits!(credits)LlmCreditDailyUsage.increment_usage!(llm_model, credits)-- # DEPRECATED: Dual-write to JSONB daily_usage column during migration period- # TODO: Remove this entire block once daily_usage column is dropped- if respond_to?(:daily_usage=)- with_lock do- reload- day_key = current_day_key- self.daily_usage = daily_usage.merge(day_key => daily_used + credits)-- # Inline JSONB cleanup (original behavior for backwards compatibility)- cutoff = DAILY_USAGE_RETENTION_DAYS.days.ago.beginning_of_day.utc.strftime("%Y-%m-%d")- self.daily_usage = daily_usage.select { |k, _| k >= cutoff }-- save!- end- endenddef credits_available?@@ -187,7 +151,6 @@ end## id :bigint not null, primary key# daily_credits :bigint default(0), not null-# daily_usage :jsonb not null# soft_limit_percentage :integer default(80), not null# created_at :datetime not null# updated_at :datetime not nulldiff --git a/plugins/discourse-ai/db/post_migrate/20251209180201_drop_daily_usage_from_llm_credit_allocations.rb b/plugins/discourse-ai/db/post_migrate/20251209180201_drop_daily_usage_from_llm_credit_allocations.rbnew file mode 100644index 00000000000..d7b6d548018--- /dev/null+++ b/plugins/discourse-ai/db/post_migrate/20251209180201_drop_daily_usage_from_llm_credit_allocations.rb@@ -0,0 +1,13 @@+# frozen_string_literal: true++class DropDailyUsageFromLlmCreditAllocations < ActiveRecord::Migration[8.0]+ DROPPED_COLUMNS = { llm_credit_allocations: %i[daily_usage] }++ def up+ DROPPED_COLUMNS.each { |table, columns| Migration::ColumnDropper.execute_drop(table, columns) }+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rb b/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rbindex 20fbfe158e5..0264d8380ff 100644--- a/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rb+++ b/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rb@@ -3,21 +3,12 @@Fabricator(:llm_credit_allocation) dollm_modeldaily_credits 33_334- daily_usage { {} }soft_limit_percentage 80transient :daily_used- after_build do |allocation, transients|- if transients[:daily_used]- day_key = Time.current.utc.strftime("%Y-%m-%d")- allocation.daily_usage = { day_key => transients[:daily_used] }- end- end-after_create do |allocation, transients|if transients[:daily_used]- # Also create the daily usage record in the new tableusage =LlmCreditDailyUsage.find_or_create_by!(llm_model_id: allocation.llm_model_id,diff --git a/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb b/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rbindex c739214ff9d..d553455d025 100644--- a/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb+++ b/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb@@ -36,9 +36,8 @@ RSpec.describe LlmCreditAllocation doexpect(allocation).to be_validend- it "initializes with empty daily_usage on create" do+ it "initializes with zero daily_used on create" doallocation = Fabricate(:llm_credit_allocation, llm_model: llm_model)- expect(allocation.daily_usage).to eq({})expect(allocation.daily_used).to eq(0)endend@@ -248,7 +247,7 @@ RSpec.describe LlmCreditAllocation doexpect(allocation.daily_used).to eq(0)end- it "preserves previous day's data" do+ it "preserves previous day's data in daily_usages table" dofreeze_time(Time.zone.parse("2025-10-15 14:30:00 UTC"))allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 800)@@ -257,22 +256,12 @@ RSpec.describe LlmCreditAllocation doallocation.reloadexpect(allocation.daily_used).to eq(200)- expect(allocation.daily_usage["2025-10-15"]).to eq(800)- end-- it "cleans up old days beyond 90 days" do- allocation = Fabricate(:llm_credit_allocation)-- 100.times do |i|- day_key = (i + 1).days.ago.beginning_of_day.utc.strftime("%Y-%m-%d")- allocation.daily_usage[day_key] = 100 * (i + 1)- end- allocation.save!-- allocation.deduct_credits!(100)- allocation.reload-- expect(allocation.daily_usage.keys.size).to be <= 91+ previous_day_usage =+ LlmCreditDailyUsage.find_by(+ llm_model_id: allocation.llm_model_id,+ usage_date: Date.parse("2025-10-15"),+ )+ expect(previous_day_usage.credits_used).to eq(800)endend