From 06978e6862a443f526e02777cfc7ab93b1fc2cf2 Mon Sep 17 00:00:00 2001 From: Maveno Date: Sat, 16 Nov 2024 09:17:08 +0100 Subject: [PATCH] #380 Datenbank-Version --- bin/ffx.py | 3 +- bin/ffx/configuration_controller.py | 90 ++++++++++++++++++++++++++++- bin/ffx/constants.py | 3 + bin/ffx/database.py | 68 ++++++++++++++++++++-- bin/ffx/model/property.py | 16 +++++ 5 files changed, 173 insertions(+), 7 deletions(-) create mode 100644 bin/ffx/model/property.py diff --git a/bin/ffx.py b/bin/ffx.py index ad117f7..5508dbe 100755 --- a/bin/ffx.py +++ b/bin/ffx.py @@ -31,10 +31,9 @@ from ffx.filter.preset_filter import PresetFilter from ffx.filter.nlmeans_filter import NlmeansFilter +from ffx.constants import VERSION -VERSION='0.2.2' - # 0.1.1 # Bugfixes, TMBD identify shows # 0.1.2 diff --git a/bin/ffx/configuration_controller.py b/bin/ffx/configuration_controller.py index dc6d125..8fb5561 100644 --- a/bin/ffx/configuration_controller.py +++ b/bin/ffx/configuration_controller.py @@ -49,4 +49,92 @@ class ConfigurationController(): def getData(self): - return self.__configurationData \ No newline at end of file + return self.__configurationData + + +# +# +# +# def addPattern(self, patternDescriptor): +# +# try: +# +# s = self.Session() +# q = s.query(Pattern).filter(Pattern.show_id == int(patternDescriptor['show_id']), +# Pattern.pattern == str(patternDescriptor['pattern'])) +# +# if not q.count(): +# pattern = Pattern(show_id = int(patternDescriptor['show_id']), +# pattern = str(patternDescriptor['pattern'])) +# s.add(pattern) +# s.commit() +# return pattern.getId() +# else: +# return 0 +# +# except Exception as ex: +# raise click.ClickException(f"PatternController.addPattern(): {repr(ex)}") +# finally: +# s.close() +# +# +# def updatePattern(self, patternId, patternDescriptor): +# +# try: +# s = self.Session() +# q = s.query(Pattern).filter(Pattern.id == int(patternId)) +# +# if q.count(): +# +# pattern = q.first() +# +# pattern.show_id = int(patternDescriptor['show_id']) +# pattern.pattern = str(patternDescriptor['pattern']) +# +# s.commit() +# return True +# +# else: +# return False +# +# except Exception as ex: +# raise click.ClickException(f"PatternController.updatePattern(): {repr(ex)}") +# finally: +# s.close() +# +# +# +# def findPattern(self, patternDescriptor): +# +# try: +# s = self.Session() +# q = s.query(Pattern).filter(Pattern.show_id == int(patternDescriptor['show_id']), Pattern.pattern == str(patternDescriptor['pattern'])) +# +# if q.count(): +# pattern = q.first() +# return int(pattern.id) +# else: +# return None +# +# except Exception as ex: +# raise click.ClickException(f"PatternController.findPattern(): {repr(ex)}") +# finally: +# s.close() +# +# +# def getPattern(self, patternId : int): +# +# if type(patternId) is not int: +# raise ValueError(f"PatternController.getPattern(): Argument patternId is required to be of type int") +# +# try: +# s = self.Session() +# q = s.query(Pattern).filter(Pattern.id == int(patternId)) +# +# return q.first() if q.count() else None +# +# except Exception as ex: +# raise click.ClickException(f"PatternController.getPattern(): {repr(ex)}") +# finally: +# s.close() +# \ No newline at end of file diff --git a/bin/ffx/constants.py b/bin/ffx/constants.py index b4a755c..16b3924 100644 --- a/bin/ffx/constants.py +++ b/bin/ffx/constants.py @@ -1,3 +1,6 @@ +VERSION='0.2.2' +DATABASE_VERSION = 2 + DEFAULT_QUALITY = 32 DEFAULT_AV1_PRESET = 5 diff --git a/bin/ffx/database.py b/bin/ffx/database.py index 0835a38..e1c39db 100644 --- a/bin/ffx/database.py +++ b/bin/ffx/database.py @@ -1,4 +1,4 @@ -import os +import os, click from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker @@ -7,9 +7,16 @@ from ffx.model.show import Base, Show from ffx.model.pattern import Pattern from ffx.model.track import Track -from ffx.model.media_tag import MediaTag -from ffx.model.track_tag import TrackTag +from ffx.model.property import Property +from ffx.constants import DATABASE_VERSION + + +DATABASE_VERSION_KEY = 'database_version' + +class DatabaseVersionException(Exception): + def __init__(self, errorMessage): + super().__init__(errorMessage) def databaseContext(databasePath: str = ''): @@ -42,4 +49,57 @@ def databaseContext(databasePath: str = ''): # except sqlite3.OperationalError: # time.sleep(0.1) - return databaseContext \ No newline at end of file + ensureDatabaseVersion(databaseContext) + + return databaseContext + +def ensureDatabaseVersion(databaseContext): + + currentDatabaseVersion = getDatabaseVersion(databaseContext) + click.echo(f"current database version: {currentDatabaseVersion}") + if currentDatabaseVersion: + if currentDatabaseVersion != DATABASE_VERSION: + raise DatabaseVersionException(f"Current database version ({currentDatabaseVersion}) does not match required ({DATABASE_VERSION})") + else: + setDatabaseVersion(databaseContext, DATABASE_VERSION) + + +def getDatabaseVersion(databaseContext): + + try: + + Session = databaseContext['session'] + s = Session() + q = s.query(Property).filter(Property.key == DATABASE_VERSION_KEY) + + return int(q.first().value) if q.count() else 0 + + except Exception as ex: + raise click.ClickException(f"getDatabaseVersion(): {repr(ex)}") + finally: + s.close() + + +def setDatabaseVersion(databaseContext, databaseVersion: int): + + try: + Session = databaseContext['session'] + s = Session() + + q = s.query(Property).filter(Property.key == DATABASE_VERSION_KEY) + + dbVersion = int(databaseVersion) + + versionProperty = q.first() + if versionProperty: + versionProperty.value = str(dbVersion) + else: + versionProperty = Property(key = DATABASE_VERSION_KEY, + value = str(dbVersion)) + s.add(versionProperty) + s.commit() + + except Exception as ex: + raise click.ClickException(f"setDatabaseVersion(): {repr(ex)}") + finally: + s.close() \ No newline at end of file diff --git a/bin/ffx/model/property.py b/bin/ffx/model/property.py new file mode 100644 index 0000000..4dc9472 --- /dev/null +++ b/bin/ffx/model/property.py @@ -0,0 +1,16 @@ +# from typing import List +from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, Enum +from sqlalchemy.orm import relationship, declarative_base, sessionmaker + +from .show import Base + + +class Property(Base): + + __tablename__ = 'properties' + + # v1.x + id = Column(Integer, primary_key=True) + + key = Column(String) + value = Column(String)