diff --git a/backup/backup.py b/backup/backup.py index d6e6563..bb457f3 100755 --- a/backup/backup.py +++ b/backup/backup.py @@ -7,6 +7,7 @@ from urllib.parse import urlparse import json import logging import os +import re import struct import sys import sqlite3 @@ -94,11 +95,24 @@ class Backend(object): f.write(snapshot) self.db = self._db_open(dest) + def _rewrite_stmt(self, stmt: bytes) -> bytes: + """We had a stmt expansion bug in c-lightning, this replicates the fix. + + We were expanding statements incorrectly, missing some + whitespace between a param and the `WHERE` keyword. This + re-inserts the space. + + """ + stmt = re.sub(r'reserved_til=([0-9]+)WHERE', r'reserved_til=\1 WHERE', stmt) + stmt = re.sub(r'peer_id=([0-9]+)WHERE channels.id=', r'peer_id=\1 WHERE channels.id=', stmt) + return stmt + def _restore_transaction(self, tx: Iterator[bytes]): assert(self.db) cur = self.db.cursor() for q in tx: - cur.execute(q.decode('UTF-8')) + q = self._rewrite_stmt(q.decode('UTF-8')) + cur.execute(q) self.db.commit() def restore(self, dest: str, remove_existing: bool = False):