Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-06-13 14:45:00 +02:00
commit 33682eafb6
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
12 changed files with 31 additions and 244 deletions

View file

@ -9,32 +9,34 @@ CREATE TABLE instrument (
devise_cotation TEXT NOT NULL DEFAULT 'EUR'
);
-- Historique de prix (hypertable TimescaleDB)
-- Historique de prix intraday (hypertable TimescaleDB)
-- Le job de nettoyage consolide à J-1 en gardant uniquement le dernier fetch
CREATE TABLE price_history (
instrument_id INTEGER NOT NULL REFERENCES instrument(id),
date DATE NOT NULL,
instrument_id INTEGER NOT NULL REFERENCES instrument(id),
fetched_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
prix NUMERIC(24, 8) NOT NULL,
PRIMARY KEY (instrument_id, date)
PRIMARY KEY (instrument_id, fetched_at)
);
SELECT create_hypertable('price_history', by_range('date'));
SELECT create_hypertable('price_history', by_range('fetched_at'));
CREATE INDEX idx_price_history_instrument ON price_history(instrument_id, fetched_at DESC);
-- Comptes : conteneurs de positions, aucun solde stocké
-- Cache OpenFIGI (ISIN → ticker Yahoo Finance) et CoinGecko (ticker → coin ID)
CREATE TABLE instrument_ticker_cache (
instrument_id INTEGER PRIMARY KEY REFERENCES instrument(id) ON DELETE CASCADE,
ticker TEXT NOT NULL,
fetched_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Comptes et enveloppes : les enveloppes sont des sous-comptes (master_account_id non null)
-- Un sous-compte hérite du type et de la devise de son maître ; pas de chaînage
CREATE TABLE account (
id SERIAL PRIMARY KEY,
nom TEXT NOT NULL,
type TEXT NOT NULL, -- courant, livret, pea, cto, crypto, ...
devise_reference TEXT NOT NULL DEFAULT 'EUR',
plafond NUMERIC(24, 8),
taux NUMERIC(10, 6)
);
-- Enveloppes : ventilation logique EUR d'un compte (strictement monétaire)
CREATE TABLE envelope (
id SERIAL PRIMARY KEY,
account_id INTEGER NOT NULL REFERENCES account(id) ON DELETE CASCADE,
nom TEXT NOT NULL,
objectif TEXT,
montant_alloue NUMERIC(24, 8) NOT NULL DEFAULT 0
id SERIAL PRIMARY KEY,
nom TEXT NOT NULL,
type TEXT NOT NULL, -- courant, livret, pea, cto, crypto, ...
devise_reference TEXT NOT NULL DEFAULT 'EUR',
plafond NUMERIC(24, 8), -- indicatif (ex : plafond réglementaire Livret A)
master_account_id INTEGER REFERENCES account(id) ON DELETE CASCADE,
objectif TEXT -- description libre pour les enveloppes
);
-- Règles de récurrence
@ -49,11 +51,10 @@ CREATE TABLE recurring_rule (
tiers TEXT,
label TEXT NOT NULL,
categorie TEXT,
envelope_id INTEGER REFERENCES envelope(id),
frequence TEXT NOT NULL, -- RRULE string (RFC 5545)
date_debut DATE NOT NULL,
date_fin DATE,
generated_until DATE -- curseur d'idempotence
generated_until DATE -- curseur d'idempotence
);
-- Transactions : échange atomique source/dest généralisé aux instruments
@ -69,7 +70,6 @@ CREATE TABLE transaction (
tiers TEXT,
label TEXT NOT NULL,
categorie TEXT,
envelope_id INTEGER REFERENCES envelope(id),
validated BOOLEAN NOT NULL DEFAULT FALSE,
recurring_rule_id INTEGER REFERENCES recurring_rule(id),
@ -100,12 +100,12 @@ CREATE TABLE transaction (
)
);
CREATE INDEX idx_transaction_date ON transaction(date);
CREATE INDEX idx_transaction_account_source ON transaction(account_source_id) WHERE account_source_id IS NOT NULL;
CREATE INDEX idx_transaction_account_dest ON transaction(account_dest_id) WHERE account_dest_id IS NOT NULL;
CREATE INDEX idx_transaction_unvalidated ON transaction(date) WHERE validated = FALSE;
CREATE INDEX idx_transaction_date ON transaction(date);
CREATE INDEX idx_transaction_account_source ON transaction(account_source_id) WHERE account_source_id IS NOT NULL;
CREATE INDEX idx_transaction_account_dest ON transaction(account_dest_id) WHERE account_dest_id IS NOT NULL;
CREATE INDEX idx_transaction_unvalidated ON transaction(date) WHERE validated = FALSE;
-- Snapshots de position (hypertable TimescaleDB)
-- Snapshots de position par instrument et par compte (hypertable TimescaleDB)
CREATE TABLE position_snapshot (
date DATE NOT NULL,
account_id INTEGER NOT NULL REFERENCES account(id),
@ -119,7 +119,7 @@ CREATE TABLE position_snapshot (
SELECT create_hypertable('position_snapshot', by_range('date'));
CREATE INDEX idx_position_snapshot_lookup ON position_snapshot(account_id, instrument_id, date DESC);
-- Snapshots de compte agrégés (hypertable TimescaleDB)
-- Snapshots de valeur agrégée par compte (hypertable TimescaleDB)
CREATE TABLE account_snapshot (
date DATE NOT NULL,
account_id INTEGER NOT NULL REFERENCES account(id),