From c4524e3a7385125d7c5adf779ab51ea1c5c6e974 Mon Sep 17 00:00:00 2001 From: AdrianoDev Date: Sat, 22 Aug 2026 10:49:12 +0200 Subject: [PATCH] schema: la tabella score rifiuta un valore sotto uno stato che lo nega MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CHECK ((stato='ok' AND valore NOT NULL) OR (stato='insufficiente' AND valore NULL)): il tipo Punteggio protegge il codice, non la tabella, e l'interfaccia leggerà 'score' direttamente. Idempotente sui database nuovi (CREATE TABLE IF NOT EXISTS); un longevity.db già esistente NON riceve il vincolo, SQLite non lo permette via ALTER TABLE. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01XhLbMQ1q7wHwJSykgXRwQF --- src/lib/longevity/db.ts | 10 +++++++++- tests/longevity/db.test.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/lib/longevity/db.ts b/src/lib/longevity/db.ts index 21a0166..bf66c34 100644 --- a/src/lib/longevity/db.ts +++ b/src/lib/longevity/db.ts @@ -84,7 +84,15 @@ CREATE TABLE IF NOT EXISTS score ( stato TEXT NOT NULL CHECK (stato IN ('ok','insufficiente')), quest_version TEXT, model_version TEXT NOT NULL, - calcolato_at TEXT NOT NULL DEFAULT (datetime('now')) + calcolato_at TEXT NOT NULL DEFAULT (datetime('now')), + -- Il tipo Punteggio protegge il codice ma non la tabella: l'interfaccia leggera' + -- 'score' direttamente, dove quel tipo non c'e' piu'. Questo vincolo lo riscrive + -- a livello di dato: o lo stato e' 'ok' e il valore c'e', o e' 'insufficiente' e + -- il valore e' NULL, mai un numero scritto sotto uno stato che lo nega. + -- SQLite non permette di aggiungere un CHECK con ALTER TABLE a una tabella gia' + -- creata: un database longevity.db esistente, creato prima di questa modifica, + -- NON avra' questo vincolo finche' non viene ricreato. + CHECK ((stato = 'ok' AND valore IS NOT NULL) OR (stato = 'insufficiente' AND valore IS NULL)) ); CREATE INDEX IF NOT EXISTS idx_score_cliente ON score (client_code, calcolato_at DESC); `; diff --git a/tests/longevity/db.test.ts b/tests/longevity/db.test.ts index 959adb2..dcd5d33 100644 --- a/tests/longevity/db.test.ts +++ b/tests/longevity/db.test.ts @@ -43,6 +43,33 @@ describe('schema longevity', () => { expect(row.stato).toBe('insufficiente'); }); + it('la tabella rifiuta uno score insufficiente CON un valore scritto', () => { + // Il tipo Punteggio protegge il codice, ma l'interfaccia legge dalla tabella, + // dove il tipo non c'e' piu': senza questo vincolo si potrebbe scrivere un + // numero dichiarato "insufficiente" e chi legge crud dalla tabella lo prenderebbe. + const db = createLongevityDb(':memory:'); + db.prepare(`INSERT INTO soggetti (client_code, sesso) VALUES ('ISL-0001', 'F')`).run(); + db.prepare(`INSERT INTO sessioni (client_code, data, tipo) VALUES ('ISL-0001', '2026-08-21', 'checkup')`).run(); + expect(() => + db.prepare( + `INSERT INTO score (client_code, sessione_id, tipo, elemento, valore, copertura, stato, model_version) + VALUES ('ISL-0001', 1, 'asse', 'Forza & Struttura', 42, 0.2, 'insufficiente', 'v1.0')` + ).run() + ).toThrow(); + }); + + it('la tabella rifiuta uno score ok SENZA valore', () => { + const db = createLongevityDb(':memory:'); + db.prepare(`INSERT INTO soggetti (client_code, sesso) VALUES ('ISL-0001', 'F')`).run(); + db.prepare(`INSERT INTO sessioni (client_code, data, tipo) VALUES ('ISL-0001', '2026-08-21', 'checkup')`).run(); + expect(() => + db.prepare( + `INSERT INTO score (client_code, sessione_id, tipo, elemento, valore, copertura, stato, model_version) + VALUES ('ISL-0001', 1, 'asse', 'Forza & Struttura', NULL, 1, 'ok', 'v1.0')` + ).run() + ).toThrow(); + }); + it('identity tiene la persona, longevity non la conosce', () => { const id = createIdentityDb(':memory:'); const cols = (id.prepare(`PRAGMA table_info(clienti)`).all() as { name: string }[]).map((c) => c.name);