backup: Rewrite erroneously expanded SQL stmts on-the-fly

Seems to have little to no performance impact.
This commit is contained in:
Christian Decker
2020-12-01 15:04:54 +01:00
parent e6d26b479c
commit 092b295d06

View File

@@ -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):