core/sqlite: add compat version for backup() for python3.6

This commit is contained in:
Dima Gerasimov 2021-04-05 08:11:46 +01:00 committed by karlicoss
parent f09ca17560
commit b94120deaf
2 changed files with 19 additions and 2 deletions

View file

@ -64,3 +64,19 @@ else:
import os import os
windows = os.name == 'nt' windows = os.name == 'nt'
import sqlite3
def sqlite_backup(*, source: sqlite3.Connection, dest: sqlite3.Connection, **kwargs) -> None:
if sys.version_info[:2] >= (3, 7):
source.backup(dest, **kwargs)
else:
# https://stackoverflow.com/a/10856450/706389
import io
tempfile = io.StringIO()
for line in source.iterdump():
tempfile.write('%s\n' % line)
tempfile.seek(0)
dest.cursor().executescript(tempfile.read())
dest.commit()

View file

@ -45,6 +45,7 @@ def sqlite_copy_and_open(db: PathIsh) -> sqlite3.Connection:
tocopy = [dp] + [p for p in dp.parent.glob(dp.name + '-*') if not p.name.endswith('-shm')] tocopy = [dp] + [p for p in dp.parent.glob(dp.name + '-*') if not p.name.endswith('-shm')]
for p in tocopy: for p in tocopy:
shutil.copy(p, tdir / p.name) shutil.copy(p, tdir / p.name)
with sqlite3.connect(tdir / dp.name) as conn: with sqlite3.connect(str(tdir / dp.name)) as conn:
conn.backup(dest) from .compat import sqlite_backup
sqlite_backup(source=conn, dest=dest)
return dest return dest