-- 006_market_data_archive.sql — Phase 2 archive-before-delete (TODO §4.2).

CREATE TABLE IF NOT EXISTS market_data_archive (
  id            BIGSERIAL PRIMARY KEY,
  symbol        TEXT NOT NULL,
  asset_class   TEXT NOT NULL DEFAULT 'us_equity',
  feed          TEXT NOT NULL DEFAULT 'iex',
  metric        TEXT NOT NULL,
  timeframe     TEXT NOT NULL DEFAULT 'tick',
  price         NUMERIC,
  volume        NUMERIC,
  as_of         TIMESTAMPTZ NOT NULL,
  raw           JSONB,
  archived_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS market_data_archive_asof_idx
  ON market_data_archive (as_of DESC);

CREATE INDEX IF NOT EXISTS market_data_archive_symbol_asof_idx
  ON market_data_archive (symbol, as_of DESC);
