home · contact · privacy
To prepare for migrations, add database version check.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 24 Nov 2024 17:25:54 +0000 (18:25 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 24 Nov 2024 17:25:54 +0000 (18:25 +0100)
ytplom/misc.py

index fe5f429b4937fdf77721ff4c00a834a2b38ffaf1..0928fbecd1a0a7f67d5ea0a351269f194f323224 100644 (file)
@@ -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: