add envelop
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
af0d98683c
commit
1f3ed998df
8 changed files with 363 additions and 19 deletions
18
migrations/000004_envelopes_and_transactions.down.sql
Normal file
18
migrations/000004_envelopes_and_transactions.down.sql
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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;
|
||||
49
migrations/000004_envelopes_and_transactions.up.sql
Normal file
49
migrations/000004_envelopes_and_transactions.up.sql
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
-- 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)
|
||||
);
|
||||
59
migrations/000005_envelope_as_account.down.sql
Normal file
59
migrations/000005_envelope_as_account.down.sql
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
-- 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)
|
||||
);
|
||||
50
migrations/000005_envelope_as_account.up.sql
Normal file
50
migrations/000005_envelope_as_account.up.sql
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
-- 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;
|
||||
1
migrations/000006_drop_montant_alloue.down.sql
Normal file
1
migrations/000006_drop_montant_alloue.down.sql
Normal file
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE account ADD COLUMN montant_alloue NUMERIC(24, 8);
|
||||
3
migrations/000006_drop_montant_alloue.up.sql
Normal file
3
migrations/000006_drop_montant_alloue.up.sql
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
-- 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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue