-- 010_session_half_bars.sql
--
-- RTH session half bars (morning 9:30–12:45 ET, afternoon 12:45–16:00 ET).
-- Drops legacy 4Hour history and adds sessionHalf timeframe + index.

DELETE FROM market_data_snapshots
WHERE metric = 'bar' AND timeframe = '4Hour';

DELETE FROM market_data_archive
WHERE metric = 'bar' AND timeframe = '4Hour';

-- Idempotent: constraint may already include sessionHalf (008) or be missing after a failed run.
ALTER TABLE market_data_snapshots
  DROP CONSTRAINT IF EXISTS market_data_snapshots_timeframe_check;

ALTER TABLE market_data_snapshots
  ADD CONSTRAINT market_data_snapshots_timeframe_check
  CHECK (timeframe IN ('tick', '1Min', '1Hour', '4Hour', '1Day', 'sessionHalf'));

DROP INDEX IF EXISTS market_data_snapshots_bar_4h_idx;

CREATE INDEX IF NOT EXISTS market_data_snapshots_bar_session_half_idx
  ON market_data_snapshots (symbol, as_of DESC)
  WHERE metric = 'bar' AND timeframe = 'sessionHalf';
