Skip to content

A platform for community discussion. Free, open, simple.

FIX: Move post_migration to migration instead to ensure monthly_usage is created (#35902)

Natalie Tay5333e85fa199
diff --git a/plugins/discourse-ai/app/models/llm_credit_allocation.rb b/plugins/discourse-ai/app/models/llm_credit_allocation.rb
index 077e33bf7cf..f7c87ccfc82 100644
--- a/plugins/discourse-ai/app/models/llm_credit_allocation.rb
+++ b/plugins/discourse-ai/app/models/llm_credit_allocation.rb
@@ -14,7 +14,7 @@ class LlmCreditAllocation < ActiveRecord::Base
belongs_to :llm_model
- # TODO: Remove once 20251105174002_refactor_llm_credit_allocation_to_monthly_usage has been promoted to pre-deploy
+ # TODO: Remove once 20251105174003_remove_old_credit_columns_from_llm_credit_allocations has been promoted to pre-deploy
self.ignored_columns = %w[monthly_used last_reset_at]
validates :llm_model_id, presence: true, uniqueness: true
diff --git a/plugins/discourse-ai/db/migrate/20251105174002_refactor_llm_credit_allocation_to_monthly_usage.rb b/plugins/discourse-ai/db/migrate/20251105174002_refactor_llm_credit_allocation_to_monthly_usage.rb
new file mode 100644
index 00000000000..a8b9c8cbb27
--- /dev/null
+++ b/plugins/discourse-ai/db/migrate/20251105174002_refactor_llm_credit_allocation_to_monthly_usage.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class RefactorLlmCreditAllocationToMonthlyUsage < ActiveRecord::Migration[8.0]
+ def up
+ return if column_exists?(:llm_credit_allocations, :monthly_usage)
+
+ add_column :llm_credit_allocations, :monthly_usage, :jsonb, default: {}, null: false
+
+ execute <<~SQL
+ UPDATE llm_credit_allocations
+ SET monthly_usage = jsonb_build_object(
+ to_char(COALESCE(last_reset_at, CURRENT_TIMESTAMP), 'YYYY-MM'),
+ monthly_used
+ )
+ WHERE monthly_used > 0
+ SQL
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/plugins/discourse-ai/db/post_migrate/20251105174002_refactor_llm_credit_allocation_to_monthly_usage.rb b/plugins/discourse-ai/db/post_migrate/20251105174002_refactor_llm_credit_allocation_to_monthly_usage.rb
deleted file mode 100644
index 90ee901e2d2..00000000000
--- a/plugins/discourse-ai/db/post_migrate/20251105174002_refactor_llm_credit_allocation_to_monthly_usage.rb
+++ /dev/null
@@ -1,38 +0,0 @@
-# frozen_string_literal: true
-class RefactorLlmCreditAllocationToMonthlyUsage < ActiveRecord::Migration[8.0]
- def up
- add_column :llm_credit_allocations, :monthly_usage, :jsonb, default: {}, null: false
-
- execute <<~SQL
- UPDATE llm_credit_allocations
- SET monthly_usage = jsonb_build_object(
- to_char(COALESCE(last_reset_at, CURRENT_TIMESTAMP), 'YYYY-MM'),
- monthly_used
- )
- WHERE monthly_used > 0
- SQL
-
- remove_column :llm_credit_allocations, :monthly_used
- remove_column :llm_credit_allocations, :last_reset_at
- end
-
- def down
- add_column :llm_credit_allocations, :monthly_used, :bigint, default: 0, null: false
- add_column :llm_credit_allocations,
- :last_reset_at,
- :datetime,
- null: false,
- default: -> { "date_trunc('month', CURRENT_TIMESTAMP)" }
-
- execute <<~SQL
- UPDATE llm_credit_allocations
- SET monthly_used = COALESCE(
- (monthly_usage->>to_char(CURRENT_TIMESTAMP, 'YYYY-MM'))::bigint,
- 0
- ),
- last_reset_at = date_trunc('month', CURRENT_TIMESTAMP)
- SQL
-
- remove_column :llm_credit_allocations, :monthly_usage
- end
-end
diff --git a/plugins/discourse-ai/db/post_migrate/20251105174003_remove_old_credit_columns_from_llm_credit_allocations.rb b/plugins/discourse-ai/db/post_migrate/20251105174003_remove_old_credit_columns_from_llm_credit_allocations.rb
new file mode 100644
index 00000000000..8af119bd547
--- /dev/null
+++ b/plugins/discourse-ai/db/post_migrate/20251105174003_remove_old_credit_columns_from_llm_credit_allocations.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class RemoveOldCreditColumnsFromLlmCreditAllocations < ActiveRecord::Migration[8.0]
+ def up
+ if column_exists?(:llm_credit_allocations, :monthly_used)
+ remove_column :llm_credit_allocations, :monthly_used
+ end
+
+ if column_exists?(:llm_credit_allocations, :last_reset_at)
+ remove_column :llm_credit_allocations, :last_reset_at
+ end
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end