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

@ -2,7 +2,7 @@ DROP TABLE IF EXISTS account_snapshot;
DROP TABLE IF EXISTS position_snapshot; DROP TABLE IF EXISTS position_snapshot;
DROP TABLE IF EXISTS transaction; DROP TABLE IF EXISTS transaction;
DROP TABLE IF EXISTS recurring_rule; DROP TABLE IF EXISTS recurring_rule;
DROP TABLE IF EXISTS envelope;
DROP TABLE IF EXISTS account; DROP TABLE IF EXISTS account;
DROP TABLE IF EXISTS instrument_ticker_cache;
DROP TABLE IF EXISTS price_history; DROP TABLE IF EXISTS price_history;
DROP TABLE IF EXISTS instrument; DROP TABLE IF EXISTS instrument;

View file

@ -9,32 +9,34 @@ CREATE TABLE instrument (
devise_cotation TEXT NOT NULL DEFAULT 'EUR' 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 ( CREATE TABLE price_history (
instrument_id INTEGER NOT NULL REFERENCES instrument(id), instrument_id INTEGER NOT NULL REFERENCES instrument(id),
date DATE NOT NULL, fetched_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
prix NUMERIC(24, 8) NOT NULL, 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 ( CREATE TABLE account (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
nom TEXT NOT NULL, nom TEXT NOT NULL,
type TEXT NOT NULL, -- courant, livret, pea, cto, crypto, ... type TEXT NOT NULL, -- courant, livret, pea, cto, crypto, ...
devise_reference TEXT NOT NULL DEFAULT 'EUR', devise_reference TEXT NOT NULL DEFAULT 'EUR',
plafond NUMERIC(24, 8), plafond NUMERIC(24, 8), -- indicatif (ex : plafond réglementaire Livret A)
taux NUMERIC(10, 6) master_account_id INTEGER REFERENCES account(id) ON DELETE CASCADE,
); objectif TEXT -- description libre pour les enveloppes
-- 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
); );
-- Règles de récurrence -- Règles de récurrence
@ -49,11 +51,10 @@ CREATE TABLE recurring_rule (
tiers TEXT, tiers TEXT,
label TEXT NOT NULL, label TEXT NOT NULL,
categorie TEXT, categorie TEXT,
envelope_id INTEGER REFERENCES envelope(id),
frequence TEXT NOT NULL, -- RRULE string (RFC 5545) frequence TEXT NOT NULL, -- RRULE string (RFC 5545)
date_debut DATE NOT NULL, date_debut DATE NOT NULL,
date_fin DATE, 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 -- Transactions : échange atomique source/dest généralisé aux instruments
@ -69,7 +70,6 @@ CREATE TABLE transaction (
tiers TEXT, tiers TEXT,
label TEXT NOT NULL, label TEXT NOT NULL,
categorie TEXT, categorie TEXT,
envelope_id INTEGER REFERENCES envelope(id),
validated BOOLEAN NOT NULL DEFAULT FALSE, validated BOOLEAN NOT NULL DEFAULT FALSE,
recurring_rule_id INTEGER REFERENCES recurring_rule(id), 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_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_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_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_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 ( CREATE TABLE position_snapshot (
date DATE NOT NULL, date DATE NOT NULL,
account_id INTEGER NOT NULL REFERENCES account(id), account_id INTEGER NOT NULL REFERENCES account(id),
@ -119,7 +119,7 @@ CREATE TABLE position_snapshot (
SELECT create_hypertable('position_snapshot', by_range('date')); SELECT create_hypertable('position_snapshot', by_range('date'));
CREATE INDEX idx_position_snapshot_lookup ON position_snapshot(account_id, instrument_id, date DESC); 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 ( CREATE TABLE account_snapshot (
date DATE NOT NULL, date DATE NOT NULL,
account_id INTEGER NOT NULL REFERENCES account(id), account_id INTEGER NOT NULL REFERENCES account(id),

View file

@ -1,10 +0,0 @@
DROP TABLE IF EXISTS instrument_ticker_cache;
DROP TABLE IF EXISTS price_history;
CREATE TABLE price_history (
instrument_id INTEGER NOT NULL REFERENCES instrument(id),
date DATE NOT NULL,
prix NUMERIC(24, 8) NOT NULL,
PRIMARY KEY (instrument_id, date)
);
SELECT create_hypertable('price_history', by_range('date'));

View file

@ -1,19 +0,0 @@
-- Recréation de price_history avec support intraday (plusieurs prix par jour)
-- Le job de nettoyage consolide à J-1 en gardant uniquement le dernier fetch
DROP TABLE IF EXISTS price_history;
CREATE TABLE price_history (
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, fetched_at)
);
SELECT create_hypertable('price_history', by_range('fetched_at'));
CREATE INDEX idx_price_history_instrument ON price_history(instrument_id, fetched_at DESC);
-- 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()
);

View file

@ -1 +0,0 @@
ALTER TABLE account ADD COLUMN taux NUMERIC(10, 6);

View file

@ -1,3 +0,0 @@
-- taux retiré de account : le taux varie dans le temps et nécessite
-- une table account_rate(account_id, taux, date_debut, date_fin) dédiée
ALTER TABLE account DROP COLUMN taux;

View file

@ -1,18 +0,0 @@
ALTER TABLE transaction DROP CONSTRAINT chk_transaction_sides;
ALTER TABLE transaction
DROP COLUMN envelope_source_id,
DROP COLUMN envelope_dest_id,
ADD COLUMN envelope_id INTEGER REFERENCES envelope(id);
ALTER TABLE transaction ADD CONSTRAINT chk_transaction_sides CHECK (
(account_source_id IS NOT NULL AND instrument_source_id IS NOT NULL AND quantite_source IS NOT NULL
OR account_source_id IS NULL AND instrument_source_id IS NULL AND quantite_source IS NULL)
AND
(account_dest_id IS NOT NULL AND instrument_dest_id IS NOT NULL AND quantite_dest IS NOT NULL
OR account_dest_id IS NULL AND instrument_dest_id IS NULL AND quantite_dest IS NULL)
AND
(account_source_id IS NOT NULL OR account_dest_id IS NOT NULL)
);
DROP TABLE IF EXISTS envelope_snapshot;

View file

@ -1,49 +0,0 @@
-- Snapshots dédiés aux enveloppes (strictement monétaires = valeur EUR uniquement)
CREATE TABLE envelope_snapshot (
date DATE NOT NULL,
envelope_id INTEGER NOT NULL REFERENCES envelope(id) ON DELETE CASCADE,
valeur NUMERIC(24, 8) NOT NULL,
PRIMARY KEY (date, envelope_id)
);
SELECT create_hypertable('envelope_snapshot', by_range('date'));
CREATE INDEX idx_envelope_snapshot_lookup ON envelope_snapshot(envelope_id, date DESC);
-- Mise à jour de la table transaction :
-- Les enveloppes deviennent de vrais participants source/dest (plus de envelope_id de catégorie)
ALTER TABLE transaction
DROP COLUMN envelope_id,
ADD COLUMN envelope_source_id INTEGER REFERENCES envelope(id),
ADD COLUMN envelope_dest_id INTEGER REFERENCES envelope(id);
-- Remplacement de la contrainte CHECK pour couvrir account et envelope comme participants
ALTER TABLE transaction DROP CONSTRAINT chk_transaction_sides;
ALTER TABLE transaction ADD CONSTRAINT chk_transaction_sides CHECK (
-- Côté source : au plus un parmi (account, envelope), les trois champs cohérents ou tous null
(
(account_source_id IS NOT NULL AND envelope_source_id IS NULL
OR account_source_id IS NULL AND envelope_source_id IS NOT NULL
OR account_source_id IS NULL AND envelope_source_id IS NULL)
AND
(((account_source_id IS NOT NULL OR envelope_source_id IS NOT NULL)
AND instrument_source_id IS NOT NULL AND quantite_source IS NOT NULL)
OR (account_source_id IS NULL AND envelope_source_id IS NULL
AND instrument_source_id IS NULL AND quantite_source IS NULL))
)
AND
-- Côté dest : même logique
(
(account_dest_id IS NOT NULL AND envelope_dest_id IS NULL
OR account_dest_id IS NULL AND envelope_dest_id IS NOT NULL
OR account_dest_id IS NULL AND envelope_dest_id IS NULL)
AND
(((account_dest_id IS NOT NULL OR envelope_dest_id IS NOT NULL)
AND instrument_dest_id IS NOT NULL AND quantite_dest IS NOT NULL)
OR (account_dest_id IS NULL AND envelope_dest_id IS NULL
AND instrument_dest_id IS NULL AND quantite_dest IS NULL))
)
AND
-- Au moins un côté doit exister
(account_source_id IS NOT NULL OR envelope_source_id IS NOT NULL
OR account_dest_id IS NOT NULL OR envelope_dest_id IS NOT NULL)
);

View file

@ -1,59 +0,0 @@
-- Recréation de la table envelope
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
);
-- Remigration des sous-comptes vers envelope
INSERT INTO envelope (account_id, nom, objectif, montant_alloue)
SELECT master_account_id, nom, objectif, COALESCE(montant_alloue, 0)
FROM account WHERE master_account_id IS NOT NULL;
-- Suppression des sous-comptes de account
DELETE FROM account WHERE master_account_id IS NOT NULL;
ALTER TABLE account
DROP COLUMN master_account_id,
DROP COLUMN objectif,
DROP COLUMN montant_alloue;
-- Restauration envelope_snapshot
CREATE TABLE envelope_snapshot (
date DATE NOT NULL,
envelope_id INTEGER NOT NULL REFERENCES envelope(id) ON DELETE CASCADE,
valeur NUMERIC(24, 8) NOT NULL,
PRIMARY KEY (date, envelope_id)
);
SELECT create_hypertable('envelope_snapshot', by_range('date'));
-- Restauration transaction avec envelope_source/dest
ALTER TABLE transaction
DROP CONSTRAINT chk_transaction_sides,
ADD COLUMN envelope_source_id INTEGER REFERENCES envelope(id),
ADD COLUMN envelope_dest_id INTEGER REFERENCES envelope(id);
ALTER TABLE transaction ADD CONSTRAINT chk_transaction_sides CHECK (
(account_source_id IS NOT NULL AND envelope_source_id IS NULL
OR account_source_id IS NULL AND envelope_source_id IS NOT NULL
OR account_source_id IS NULL AND envelope_source_id IS NULL)
AND
(((account_source_id IS NOT NULL OR envelope_source_id IS NOT NULL)
AND instrument_source_id IS NOT NULL AND quantite_source IS NOT NULL)
OR (account_source_id IS NULL AND envelope_source_id IS NULL
AND instrument_source_id IS NULL AND quantite_source IS NULL))
AND
(account_dest_id IS NOT NULL AND envelope_dest_id IS NULL
OR account_dest_id IS NULL AND envelope_dest_id IS NOT NULL
OR account_dest_id IS NULL AND envelope_dest_id IS NULL)
AND
(((account_dest_id IS NOT NULL OR envelope_dest_id IS NOT NULL)
AND instrument_dest_id IS NOT NULL AND quantite_dest IS NOT NULL)
OR (account_dest_id IS NULL AND envelope_dest_id IS NULL
AND instrument_dest_id IS NULL AND quantite_dest IS NULL))
AND
(account_source_id IS NOT NULL OR envelope_source_id IS NOT NULL
OR account_dest_id IS NOT NULL OR envelope_dest_id IS NOT NULL)
);

View file

@ -1,50 +0,0 @@
-- Les enveloppes deviennent des comptes avec master_account_id
-- Un sous-compte hérite du type et de la devise de son compte maître
ALTER TABLE account
ADD COLUMN master_account_id INTEGER REFERENCES account(id) ON DELETE CASCADE,
ADD COLUMN objectif TEXT,
ADD COLUMN montant_alloue NUMERIC(24, 8);
-- Migration des enveloppes existantes vers account
INSERT INTO account (nom, type, devise_reference, master_account_id, objectif, montant_alloue)
SELECT e.nom, a.type, a.devise_reference, e.account_id, e.objectif, e.montant_alloue
FROM envelope e
JOIN account a ON a.id = e.account_id;
-- Suppression des champs envelope_source/dest ajoutés en 000004
-- (les enveloppes étant désormais des comptes, account_source_id suffit)
ALTER TABLE transaction
DROP CONSTRAINT chk_transaction_sides,
DROP COLUMN envelope_source_id,
DROP COLUMN envelope_dest_id;
ALTER TABLE transaction ADD CONSTRAINT chk_transaction_sides CHECK (
(
account_source_id IS NOT NULL AND
instrument_source_id IS NOT NULL AND
quantite_source IS NOT NULL
OR
account_source_id IS NULL AND
instrument_source_id IS NULL AND
quantite_source IS NULL
)
AND
(
account_dest_id IS NOT NULL AND
instrument_dest_id IS NOT NULL AND
quantite_dest IS NOT NULL
OR
account_dest_id IS NULL AND
instrument_dest_id IS NULL AND
quantite_dest IS NULL
)
AND
(account_source_id IS NOT NULL OR account_dest_id IS NOT NULL)
);
-- Suppression de la FK envelope_id dans recurring_rule avant de dropper envelope
ALTER TABLE recurring_rule DROP COLUMN envelope_id;
-- Suppression des tables envelope devenues inutiles
DROP TABLE IF EXISTS envelope_snapshot;
DROP TABLE IF EXISTS envelope;

View file

@ -1 +0,0 @@
ALTER TABLE account ADD COLUMN montant_alloue NUMERIC(24, 8);

View file

@ -1,3 +0,0 @@
-- montant_alloue retiré : le solde réel d'une enveloppe est dérivé
-- des transactions et stocké dans account_snapshot
ALTER TABLE account DROP COLUMN montant_alloue;