diff --git a/db/migrate/20260211145813_create_reporting_events_rollup.rb b/db/migrate/20260211145813_create_reporting_events_rollup.rb index 4251ac58a..3f1680cd0 100644 --- a/db/migrate/20260211145813_create_reporting_events_rollup.rb +++ b/db/migrate/20260211145813_create_reporting_events_rollup.rb @@ -2,6 +2,13 @@ class CreateReportingEventsRollup < ActiveRecord::Migration[7.1] disable_ddl_transaction! def change + create_rollups_table + add_rollups_indexes + end + + private + + def create_rollups_table create_table :reporting_events_rollups do |t| t.integer :account_id, null: false t.date :date, null: false @@ -14,24 +21,19 @@ class CreateReportingEventsRollup < ActiveRecord::Migration[7.1] t.timestamps end + end - # Unique index for upsert conflict resolution + def add_rollups_indexes add_index :reporting_events_rollups, [:account_id, :date, :dimension_type, :dimension_id, :metric], - unique: true, - name: 'index_rollup_unique_key', - algorithm: :concurrently + unique: true, name: 'index_rollup_unique_key', algorithm: :concurrently - # Query index for timeseries (date range scans) add_index :reporting_events_rollups, [:account_id, :metric, :date], - name: 'index_rollup_timeseries', - algorithm: :concurrently + name: 'index_rollup_timeseries', algorithm: :concurrently - # Query index for summaries (dimension scans) add_index :reporting_events_rollups, [:account_id, :dimension_type, :date], - name: 'index_rollup_summary', - algorithm: :concurrently + name: 'index_rollup_summary', algorithm: :concurrently end end