core: add helper for opening read-only database

This commit is contained in:
Dima Gerasimov 2020-06-19 23:50:21 +01:00 committed by karlicoss
parent 0bcc5952c7
commit 4fc33a9ed2
2 changed files with 12 additions and 3 deletions

9
my/core/dataset.py Normal file
View file

@ -0,0 +1,9 @@
from pathlib import Path
# TODO wonder if also need to open without WAL.. test this on read-only directory/db file
def connect_readonly(db: Path):
import dataset # type: ignore
# see https://github.com/pudo/dataset/issues/136#issuecomment-128693122
import sqlite3
creator = lambda: sqlite3.connect(f'file:{db}?mode=ro', uri=True)
return dataset.connect('sqlite:///', engine_kwargs={'creator': creator})

View file

@ -5,9 +5,9 @@ from datetime import datetime
from typing import Any, Dict, Iterator, NamedTuple
import pytz
import dataset # type: ignore
from .common import get_files
from .core.common import get_files
from .core.dataset import connect_readonly
from my.config import materialistic as config
@ -44,7 +44,7 @@ def _last_export():
def raw() -> Iterator[Row]:
db = dataset.connect('sqlite:///' + str(_last_export()))
db = connect_readonly(_last_export())
st = db['saved']
# TODO wonder if it's 'save time'?
yield from st.all(order_by='time')