commit 76ccf284f9f4e98e55560c8105e0ea7e04d60954
parent e930f6a9a6664bcdc31a913ed160b18756ea5e9b
Author: Paco Esteban <paco@e1e0.net>
Date: Wed, 17 Jan 2024 17:32:39 +0100
add database version guard
I'm probably rewriting big portions of the tool.
This commit adds a version guard to the database, so I don't do silly
stuff with an older database. Not sure if is needed, but it won't hurt
I guess ...
Diffstat:
3 files changed, 20 insertions(+), 0 deletions(-)
diff --git a/partsdb/database.py b/partsdb/database.py
@@ -134,6 +134,8 @@ DELETE_HISTORY_QUERY = "DELETE FROM parts_history WHERE part_id = ?"
GET_IMAGE_QUERY = "SELECT image FROM parts WHERE id = ?"
GET_DATASHEET_QUERY = "SELECT datasheet FROM parts WHERE id = ?"
+GET_DB_MAJOR_VERSION_METADATA = "SELECT value FROM metadata WHERE name = 'major_version'"
+
class PartsDB:
def __init__(self, database):
@@ -284,5 +286,12 @@ class PartsDB:
c.execute(GET_DATASHEET_QUERY, (part_id,))
return c.fetchone()
+ def validate_major_version(self, version: int) -> bool:
+ c = self.conn.cursor()
+ c.execute(GET_DB_MAJOR_VERSION_METADATA)
+ db_major_version_record = c.fetchone()
+
+ return int(db_major_version_record["value"]) == version
+
def close(self):
self.conn.close()
diff --git a/partsdb/partsdb.py b/partsdb/partsdb.py
@@ -420,6 +420,10 @@ def main():
ap.print_help()
sys.exit(0)
+ if not db.validate_major_version(1):
+ print("Database does not match software version")
+ sys.exit(1)
+
if "part_id" in vars(args).keys():
args.part_id = int(args.part_id, 0)
diff --git a/schema.sql b/schema.sql
@@ -39,3 +39,10 @@ CREATE TABLE IF NOT EXISTS parts_history (
mcomment text,
FOREIGN KEY (part_id) REFERENCES parts (id)
);
+
+-- metadata table
+CREATE TABLE IF NOT EXISTS metadata (
+ id integer PRIMARY KEY,
+ name text NOT NULL,
+ value text NOT NULL
+);