diff --git a/tests/plugins/dblog.py b/tests/plugins/dblog.py index 97d1b8ed0..ba2f40a9a 100755 --- a/tests/plugins/dblog.py +++ b/tests/plugins/dblog.py @@ -16,9 +16,18 @@ def init(configuration, options, plugin): plugin.conn = sqlite3.connect(plugin.get_option('dblog-file'), isolation_level=None) plugin.log("replaying pre-init data:") + plugin.conn.execute("PRAGMA foreign_keys = ON;") + + print(plugin.sqlite_pre_init_cmds) + + plugin.conn.execute("BEGIN TRANSACTION;") + for c in plugin.sqlite_pre_init_cmds: plugin.conn.execute(c) plugin.log("{}".format(c)) + + plugin.conn.execute("COMMIT;") + plugin.initted = True plugin.log("initialized {}".format(configuration)) @@ -29,9 +38,15 @@ def db_write(plugin, writes, **kwargs): plugin.log("deferring {} commands".format(len(writes))) plugin.sqlite_pre_init_cmds += writes else: + print(writes) + plugin.conn.execute("BEGIN TRANSACTION;") + for c in writes: plugin.conn.execute(c) plugin.log("{}".format(c)) + + plugin.conn.execute("COMMIT;") + return True diff --git a/wallet/db.c b/wallet/db.c index 3a58e09a2..33a62d27e 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -789,30 +789,29 @@ size_t db_changes(struct db *db) void db_begin_transaction_(struct db *db, const char *location) { + bool ok; if (db->in_transaction) db_fatal("Already in transaction from %s", db->in_transaction); db_prepare_for_changes(db); - db_do_exec(location, db, "BEGIN TRANSACTION;"); + ok = db->config->begin_tx_fn(db); + if (!ok) + db_fatal("Failed to start DB transaction: %s", db->error); + db->in_transaction = location; } void db_commit_transaction(struct db *db) { - int err; - char *errmsg; - const char *cmd = "COMMIT;"; - + bool ok; assert(db->in_transaction); db_assert_no_outstanding_statements(); + ok = db->config->commit_tx_fn(db); - /* We expect at least the BEGIN TRANSACTION */ - db_report_changes(db, cmd, 1); - - err = sqlite3_exec(db->sql, cmd, NULL, NULL, &errmsg); - if (err != SQLITE_OK) - db_fatal("%s:%s:%s:%s", __func__, sqlite3_errstr(err), cmd, errmsg); + if (!ok) + db_fatal("Failed to commit DB transaction: %s", db->error); + db_report_changes(db, NULL, 0); db->in_transaction = NULL; } diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index a1a9b2a39..33c581a4a 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -1138,6 +1138,9 @@ static bool test_htlc_crud(struct lightningd *ld, const tal_t *ctx) /* Make sure we have our references correct */ CHECK(transaction_wrap(w->db, db_exec(__func__, w->db, "INSERT INTO channels (id) VALUES (1);"))); + db_begin_transaction(w->db); + db_commit_transaction(w->db); + chan->dbid = 1; chan->peer = peer;