tsapi-seriesdb-shared-sql/sql/03_tables.sql
peter 85679b5e78 Schema 1.5.0: holiday calendar registry, citext names; drop num_records
For the coverage-aware holiday-calendar API in TSAPI core (db_base
holiday_calendar__get / __set_coverage / __set_metadata / __upsert).

- meta.holiday_calendars (name, description, covered_from, covered_to,
  source, updated_at): a calendar exists when it has a row here, not
  when it has holidays (retires the 9999-12-31 sentinel row).
  covered_from/to: the range over which the closure list is complete;
  both set or both NULL, from <= to (holiday_calendars_coverage_check).
- Calendar names are citext in every table (registry, holidays.calendar_name,
  exchanges.default_holiday_calendar, instruments.holiday_calendar):
  stored as given, unique and matched case-insensitively -- also in the
  foreign keys -- as the native TSAPI backend does with COLLATE NOCASE.
  New extension citext (00_extensions.sql). Name check: non-empty,
  trimmed, <= 50 characters.
- FKs: holidays -> registry ON DELETE CASCADE; exchanges/instruments ->
  registry ON DELETE RESTRICT (a calendar in use cannot be dropped);
  ON UPDATE CASCADE everywhere.
- meta.series.num_records dropped. Only some write paths maintained it
  (never C++), it sometimes held an estimate, and it was NULL for a series
  never written to. Clients ask the table: count(*), or
  SELECT EXISTS (SELECT 1 FROM <table>) for "is it empty?".
- find_schema_problems() covers the new table, columns, constraints and
  extension, and runs with search_path = pg_catalog so format_type()
  reports 'public.citext' whatever the caller's search_path.

Verified on a fresh database (PG 17): run_setup.py installs it and
find_schema_problems() returns zero rows, also for a caller without
public on the search_path; constraint behaviour checked by hand
(case-insensitive duplicate refused, FK matches any spelling, RESTRICT
and CASCADE, coverage and name checks).

Re-provision databases (no migration).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 17:56:04 +01:00

175 lines
5.9 KiB
SQL

-- Meta schema tables
-- Generated: 2025-10-15T12:24:56.295509
CREATE UNLOGGED TABLE log.pending_changes (
id BIGSERIAL PRIMARY KEY,
"timestamp" TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
series_name TEXT NOT NULL,
db_schema TEXT NOT NULL,
op_type TEXT NOT NULL,
db_user TEXT DEFAULT CURRENT_USER,
details JSONB
);
CREATE TABLE log.event_log (
id BIGINT NOT NULL DEFAULT nextval('log.event_log_id_seq'),
"timestamp" TIMESTAMPTZ,
series_name TEXT NOT NULL,
db_schema TEXT NOT NULL,
event_type TEXT NOT NULL,
details JSONB,
source TEXT,
db_user TEXT,
PRIMARY KEY (id)
);
CREATE TABLE meta.currencies (
code character varying(3) NOT NULL,
name character varying(100) NOT NULL,
symbol character varying(10),
decimal_places integer DEFAULT 2,
notes text
);
CREATE TABLE meta.exchanges (
id character varying(12) NOT NULL,
name character varying(50),
timezone character varying(35),
currency character varying(3),
default_sec_type character varying(18),
default_session character varying(40),
default_holiday_calendar public.citext,
default_tick_size double precision,
default_tick_value double precision,
default_extended_hours boolean DEFAULT false,
country character varying(3)
);
-- Holiday calendar registry (1.5.0). A calendar exists when it has a row
-- here -- not when it has holidays. covered_from/covered_to: the range over
-- which its list of closures is complete (both set, or both NULL = unknown).
-- Names are citext: stored as given, matched case-insensitively ('NYSE' and
-- 'nyse' are one calendar), the same rule as the native TSAPI backend.
CREATE TABLE meta.holiday_calendars (
name public.citext NOT NULL,
description text,
covered_from date,
covered_to date,
source text,
updated_at timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE meta.holidays (
calendar_name public.citext NOT NULL,
date date NOT NULL,
description character varying(255),
early_close boolean NOT NULL DEFAULT false,
PRIMARY KEY (calendar_name, date)
);
CREATE TABLE meta.instruments (
symbol character varying(12) NOT NULL,
exchange character varying(12) NOT NULL,
name character varying(50) NOT NULL,
type character varying(10),
tick_size double precision,
tick_value double precision,
expiry character varying(20),
strike double precision,
currency character varying(3),
session character varying(40),
holiday_calendar public.citext,
sec_id character varying(50),
sec_id_type character varying(20),
description character varying(255),
country character varying(3),
extended_hours boolean,
sector character varying(50),
industry character varying(100),
category character varying(50),
subcategory character varying(50),
asset_class character varying(30),
geography character varying(30),
underlying character varying(30)
);
CREATE TABLE meta.security_types (
code character varying(10) NOT NULL,
name character varying(50) NOT NULL,
description text,
notes text
);
CREATE TABLE meta.schema_version (
version character varying(20) NOT NULL,
installed_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
description text
);
-- No record count is stored (removed in 1.5.0): "num_records" was maintained by
-- some write paths only, sometimes held an estimate, and was NULL for a series
-- never written to. Ask the table itself: SELECT count(*), or for "is it empty?"
-- SELECT EXISTS (SELECT 1 FROM <table>) -- fast, stops at the first row.
CREATE TABLE meta.series (
table_id integer DEFAULT nextval('meta._table_meta__table_id_seq'::regclass) NOT NULL,
db_schema character varying(70) NOT NULL,
table_name character varying(70) NOT NULL,
type character varying(30),
description character varying(255),
column_schema text DEFAULT ''::text,
symbol character varying(30),
root_symbol character varying(30),
exchange character varying(30),
frequency character varying(30),
session text,
sec_type character varying(30),
sec_id character varying(30),
sec_id_type character varying(30),
create_timestamp timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
first_timestamp timestamp without time zone,
last_timestamp timestamp without time zone,
bar_type character varying(30),
feed character varying(30),
currency character varying(30),
expiry character varying(30),
roll_type character varying(30),
feed_type character varying(30),
user_attr text,
user_attr_2 text,
script text,
notes text,
timezone character varying(50),
category character varying(30),
subcategory character varying(30),
json JSONB,
needs_reindex boolean NOT NULL DEFAULT false
);
-- A session's schedule is its definition string, nothing else. (Until 1.4.0
-- a parsed copy was also stored in a "json" column; nothing read it, the C++
-- client never wrote it, and it could silently disagree with the definition.
-- Clients derive the parsed form from the definition when they need it.)
CREATE TABLE meta.sessions (
name character varying(50) NOT NULL,
definition text NOT NULL,
exchange_id character varying(12) NOT NULL
);
-- Registry of series schemas. A series may only live in a registered schema
-- (FK from meta.series.db_schema, 04_indexes.sql). Every other schema in the
-- database belongs to the user and is never touched by the clients.
CREATE TABLE meta.series_schemas (
name text NOT NULL,
description text,
created_at timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
created_by text NOT NULL DEFAULT CURRENT_USER
);
CREATE TABLE meta.timezones (
iana_name character varying(50) NOT NULL,
display_name character varying(100) NOT NULL,
description text,
region character varying(50),
utc_offset_hours real,
notes text
);