-- Adds api_uploaded_at tracking column + partial index for "not-yet-uploaded" queries.
-- Safe to re-run (IF NOT EXISTS).

ALTER TABLE test_records
    ADD COLUMN IF NOT EXISTS api_uploaded_at TIMESTAMPTZ DEFAULT NULL;

CREATE INDEX IF NOT EXISTS idx_unuploaded_pass
    ON test_records(overall_result, forweb_exported_at)
    WHERE overall_result = 'PASS'
      AND forweb_exported_at IS NOT NULL
      AND api_uploaded_at IS NULL;
