home · contact · privacy
Check for database file's DB version (via user_version PRAGMA).
[plomtask] / plomtask / db.py
index f45d2b6adac0e109938d9f8018653cf1caa73fcf..dd2ee2452a7480878c47b38f2669c08cee229ca6 100644 (file)
@@ -6,6 +6,7 @@ from typing import Any, Dict
 from plomtask.exceptions import HandledException
 
 PATH_DB_SCHEMA = 'scripts/init.sql'
+EXPECTED_DB_VERSION = 0
 
 
 class DatabaseFile:  # pylint: disable=too-few-public-methods
@@ -23,11 +24,22 @@ class DatabaseFile:  # pylint: disable=too-few-public-methods
         self._check()
 
     def _check(self) -> None:
-        """Check file exists, and is of proper schema."""
+        """Check file exists, and is of proper DB version and schema."""
         self.exists = isfile(self.path)
         if self.exists:
+            self._validate_user_version()
             self._validate_schema()
 
+    def _validate_user_version(self) -> None:
+        """Compare DB user_version with EXPECTED_DB_VERSION."""
+        sql_for_db_version = 'PRAGMA user_version'
+        with sql_connect(self.path) as conn:
+            db_version = list(conn.execute(sql_for_db_version))[0][0]
+            if db_version != EXPECTED_DB_VERSION:
+                msg = f'Wrong DB version, expected '\
+                        f'{EXPECTED_DB_VERSION}, got {db_version}.'
+                raise HandledException(msg)
+
     def _validate_schema(self) -> None:
         """Compare found schema with what's stored at PATH_DB_SCHEMA."""
         sql_for_schema = 'SELECT sql FROM sqlite_master ORDER BY sql'
@@ -70,17 +82,34 @@ class DatabaseConnection:
     def rewrite_relations(self, table_name: str, key: str, target: int,
                           rows: list[list[Any]]) -> None:
         """Rewrite relations in table_name to target, with rows values."""
-        self.exec(f'DELETE FROM {table_name} WHERE {key} = ?', (target,))
+        self.delete_where(table_name, key, target)
         for row in rows:
             values = tuple([target] + row)
             q_marks = self.__class__.q_marks_from_values(values)
             self.exec(f'INSERT INTO {table_name} VALUES {q_marks}', values)
 
-    def all_where(self, table_name: str, key: str, target: int) -> list[Row]:
+    def row_where(self, table_name: str, key: str,
+                  target: int | str) -> list[Row]:
         """Return list of Rows at table where key == target."""
         return list(self.exec(f'SELECT * FROM {table_name} WHERE {key} = ?',
                               (target,)))
 
+    def column_where(self, table_name: str, column: str, key: str,
+                     target: int | str) -> list[Any]:
+        """Return column of table where key == target."""
+        return [row[0] for row in
+                self.exec(f'SELECT {column} FROM {table_name} '
+                          f'WHERE {key} = ?', (target,))]
+
+    def column_all(self, table_name: str, column: str) -> list[Any]:
+        """Return complete column of table."""
+        return [row[0] for row in
+                self.exec(f'SELECT {column} FROM {table_name}')]
+
+    def delete_where(self, table_name: str, key: str, target: int) -> None:
+        """Delete from table where key == target."""
+        self.exec(f'DELETE FROM {table_name} WHERE {key} = ?', (target,))
+
     @staticmethod
     def q_marks_from_values(values: tuple[Any]) -> str:
         """Return placeholder to insert values into SQL code."""
@@ -116,8 +145,7 @@ class BaseModel:
             obj = cache[id_]
             from_cache = True
         else:
-            for row in db_conn.exec(f'SELECT * FROM {cls.table_name} '
-                                    'WHERE id = ?', (id_,)):
+            for row in db_conn.row_where(cls.table_name, 'id', id_):
                 obj = cls.from_table_row(db_conn, row)
                 cache[id_] = obj
                 break