Files
substrata/scripts/db/016-market-data.sql
CatoandClaude Sonnet 5.5 ad9fb7932c feat(deploy): the deploy applies migrations, installs timers, runs the price backfill once (#276)
* feat(deploy): the deploy applies migrations, installs timers and runs the one-time price backfill

Before the release goes live, deploy.yml now applies pending scripts/db/*.sql
(one transaction each, recorded in public.schema_migrations, first run checks
existing objects before baselining) and installs deploy/systemd units. A failure
stops the deploy with the live release untouched. After the release, the price
backfill runs once and is recorded in public.schema_oneshots.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

* docs: name no migration file that does not exist

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-27 23:22:53 +01:00

59 lines
2.3 KiB
PL/PgSQL

BEGIN;
-- Market data (lib/market-data). Additive and idempotent, like every file here;
-- the deploy applies it before /api/cron/prices reaches production
-- (docs/operations/data-layer.md; docs/INFRASTRUCTURE.md, "Market data").
-- Daily closes, one row per listing, source and trading day. `close` is the
-- source's split-adjusted close (dividends not added back); the basis is
-- stated with every figure in lib/market-data/prices/series.ts. A split
-- re-adjusts history at the source, so the fetch overwrites and bumps
-- updated_at; first_seen keeps when the day first reached us.
CREATE TABLE IF NOT EXISTS research_price_points (
exchange text NOT NULL,
symbol text NOT NULL,
source text NOT NULL,
day date NOT NULL,
close double precision NOT NULL CHECK (close > 0),
currency text NOT NULL,
first_seen timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (exchange, symbol, source, day)
);
CREATE TABLE IF NOT EXISTS research_price_runs (
id bigserial PRIMARY KEY,
started_at timestamptz NOT NULL DEFAULT now(),
finished_at timestamptz,
tickers integer NOT NULL DEFAULT 0,
points_new integer NOT NULL DEFAULT 0,
points_revised integer NOT NULL DEFAULT 0,
failed integer NOT NULL DEFAULT 0
);
-- Fundamentals and holders read from SEC EDGAR, as computed snapshots (the
-- shapes in lib/market-data/fundamentals and lib/market-data/holders), keyed
-- by kind and CIK. Every figure inside carries its own filing URL and date;
-- the row only says when it was read.
CREATE TABLE IF NOT EXISTS research_market_snapshots (
kind text NOT NULL CHECK (kind IN ('fundamentals', 'holders')),
key text NOT NULL,
data jsonb NOT NULL,
fetched_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (kind, key)
);
CREATE TABLE IF NOT EXISTS research_market_fact_runs (
id bigserial PRIMARY KEY,
started_at timestamptz NOT NULL DEFAULT now(),
finished_at timestamptz,
looked integer NOT NULL DEFAULT 0,
stored integer NOT NULL DEFAULT 0,
failed integer NOT NULL DEFAULT 0
);
COMMENT ON TABLE research_price_points IS 'Delayed end-of-day closes for listed companies (lib/market-data/prices); not redistributed.';
COMMENT ON TABLE research_market_snapshots IS 'SEC XBRL fundamentals and filing-based holders per CIK (lib/market-data).';
COMMIT;