-- Fix reporting_events_rollups table structure to match GORM model -- The init schema (000001) created columns: dimension, dimension_value, metric_name, -- value, value_in_business_hours, period — but the GORM model and all Go code use: -- date, dimension_type, dimension_id, metric, count, sum_value, sum_value_business_hours. -- This migration drops and recreates the table with the correct schema. -- The table is empty in practice (rollups are computed on-demand), so data loss is negligible. DROP TABLE IF EXISTS reporting_events_rollups; CREATE TABLE reporting_events_rollups ( id SERIAL PRIMARY KEY, account_id INTEGER NOT NULL, date DATE NOT NULL, dimension_type VARCHAR(20) NOT NULL, dimension_id INTEGER NOT NULL, metric VARCHAR(50) NOT NULL, count BIGINT NOT NULL DEFAULT 0, sum_value DOUBLE PRECISION NOT NULL DEFAULT 0, sum_value_business_hours DOUBLE PRECISION NOT NULL DEFAULT 0, created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), deleted_at TIMESTAMP WITH TIME ZONE, UNIQUE(account_id, date, dimension_type, dimension_id, metric) ); CREATE INDEX idx_reporting_events_rollups_deleted_at ON reporting_events_rollups(deleted_at); CREATE INDEX idx_reporting_events_rollups_account_id ON reporting_events_rollups(account_id) WHERE deleted_at IS NULL; CREATE INDEX idx_reporting_events_rollups_date ON reporting_events_rollups(date) WHERE deleted_at IS NULL; CREATE INDEX idx_reporting_events_rollups_dimension ON reporting_events_rollups(dimension_type, dimension_id) WHERE deleted_at IS NULL;