|
|
@ -1,4 +1,4 @@
|
|
|
|
import os
|
|
|
|
import os, click
|
|
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import create_engine
|
|
|
|
from sqlalchemy import create_engine
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
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.pattern import Pattern
|
|
|
|
from ffx.model.track import Track
|
|
|
|
from ffx.model.track import Track
|
|
|
|
|
|
|
|
|
|
|
|
from ffx.model.media_tag import MediaTag
|
|
|
|
from ffx.model.property import Property
|
|
|
|
from ffx.model.track_tag import TrackTag
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = ''):
|
|
|
|
def databaseContext(databasePath: str = ''):
|
|
|
|
|
|
|
|
|
|
|
@ -42,4 +49,57 @@ def databaseContext(databasePath: str = ''):
|
|
|
|
# except sqlite3.OperationalError:
|
|
|
|
# except sqlite3.OperationalError:
|
|
|
|
# time.sleep(0.1)
|
|
|
|
# time.sleep(0.1)
|
|
|
|
|
|
|
|
|
|
|
|
return databaseContext
|
|
|
|
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()
|