-- Migration: Add duration_ms column to listening_history table
-- This column is referenced by analytics queries but was missing from the schema

-- Add duration_ms column if it doesn't exist
ALTER TABLE listening_history
ADD COLUMN IF NOT EXISTS duration_ms INTEGER DEFAULT 0;

-- Backfill duration_ms from duration_played_ms if it exists and duration_ms is 0
UPDATE listening_history
SET duration_ms = COALESCE(duration_played_ms, 0)
WHERE duration_ms = 0 AND duration_played_ms IS NOT NULL;

-- Create index for analytics queries that use duration_ms
CREATE INDEX IF NOT EXISTS idx_listening_history_duration_ms
ON listening_history(duration_ms);
