From 416d6bf86064da70934069b9bb77b00e3eea1b7b Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 18 Jul 2017 22:13:54 +0200 Subject: [PATCH] db: Enable foreign keys and cascade on shachain delete Should make maintenance a bit easier. --- wallet/db.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/wallet/db.c b/wallet/db.c index ed8307e5b..e6473a4c9 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -30,11 +30,11 @@ char *dbmigrations[] = { min_index INTEGER, \ num_valid INTEGER, \ PRIMARY KEY (id));", - "CREATE TABLE shachain_known ( \ - shachain_id INTEGER, \ - pos INTEGER, \ - idx INTEGER, \ - hash BLOB, \ + "CREATE TABLE shachain_known ( \ + shachain_id INTEGER REFERENCES shachains(id) ON DELETE CASCADE, \ + pos INTEGER, \ + idx INTEGER, \ + hash BLOB, \ PRIMARY KEY (shachain_id, pos));", NULL, }; @@ -155,6 +155,10 @@ static struct db *db_open(const tal_t *ctx, char *filename) tal_add_destructor(db, close_db); db->in_transaction = false; db->err = NULL; + if (!db_exec(__func__, db, "PRAGMA foreign_keys = ON;")) { + fatal("Could not enable foreignkeys on database: %s", db->err); + } + return db; }