From: Christian Heller Date: Sun, 24 Nov 2024 17:25:54 +0000 (+0100) Subject: To prepare for migrations, add database version check. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/%7B%7Bdeck_id%7D%7D/printable?a=commitdiff_plain;h=89ea6c3f57ab0c01f22209f347186c143b4d96a5;p=ytplom To prepare for migrations, add database version check. --- diff --git a/ytplom/misc.py b/ytplom/misc.py index fe5f429..0928fbe 100644 --- a/ytplom/misc.py +++ b/ytplom/misc.py @@ -85,6 +85,8 @@ TIMESTAMP_FMT = '%Y-%m-%d %H:%M:%S.%f' LEGAL_EXTENSIONS = {'webm', 'mp4', 'mkv'} # tables to create database with +EXPECTED_DB_VERSION = 0 +SQL_DB_VERSION = SqlText('PRAGMA user_version') SCRIPT_INIT_DB = ''' CREATE TABLE yt_queries ( id TEXT PRIMARY KEY, @@ -125,11 +127,15 @@ VIDEO_FLAGS: dict[FlagName, FlagsInt] = { class NotFoundException(Exception): - """Call on expected data missing, e.g. DB fetches finding nothing.""" + """Raise on expected data missing, e.g. DB fetches finding nothing.""" class CantWriteException(Exception): - """Call when there's an obstacle to expected writing of data.""" + """Raise when there's an obstacle to expected writing of data.""" + + +class HandledException(Exception): + """Raise in any other case where we know what's happening.""" def _ensure_expected_dirs(expected_dirs: list[PathStr]) -> None: @@ -159,6 +165,12 @@ class DatabaseConnection: 'into, did you run install.py?') with sql_connect(self._path) as conn: conn.executescript(SCRIPT_INIT_DB) + conn.execute(f'{SQL_DB_VERSION} = {EXPECTED_DB_VERSION}') + with sql_connect(self._path) as conn: + db_version = list(conn.execute(SQL_DB_VERSION))[0][0] + if db_version != EXPECTED_DB_VERSION: + raise HandledException(f'wrong database version {db_version}, ' + f'expected: {EXPECTED_DB_VERSION}') self._conn = sql_connect(self._path) def exec(self, sql: SqlText, inputs: tuple[Any, ...] = tuple()) -> Cursor: