Session Management allows for checks on table existence for db version
This commit is contained in:
parent
1d23b34ad6
commit
d382a2ce9e
1 changed files with 26 additions and 11 deletions
|
|
@ -1,6 +1,6 @@
|
|||
from core.errors.logger import logger
|
||||
from core.models.orm_models.Base import BaseModel
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy import create_engine, inspect
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
|
@ -18,9 +18,15 @@ def get_path():
|
|||
"""Returns XDG Base Directory path or ~/.local/share/hydra-veil"""
|
||||
xdg_data = os.getenv("XDG_DATA_HOME")
|
||||
if xdg_data:
|
||||
return Path(xdg_data) / "my-app"
|
||||
return Path(xdg_data) / "hydra-veil"
|
||||
return Path.home() / ".local" / "share" / "hydra-veil"
|
||||
|
||||
def does_it_exist(filepath):
|
||||
if filepath.exists():
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
# ============================================================================
|
||||
# GLOBAL STATE
|
||||
# ============================================================================
|
||||
|
|
@ -80,33 +86,42 @@ def create_ONLY_db_version_table():
|
|||
raise RuntimeError("Engine not initialized. Call _reinitialize_engine_and_session() first.")
|
||||
|
||||
from core.models.orm_models.DatabaseVersion import database_version
|
||||
database_version.create(engine, checkfirst=True)
|
||||
# DatabaseVersion.__table__.create(engine, checkfirst=True)
|
||||
try:
|
||||
database_version.create(engine)
|
||||
except:
|
||||
logger.info("[DB MANAGEMENT] database_version already exists, but was attempted to be made again by calling create_ONLY_db_version_table.")
|
||||
|
||||
# if using without checking or try except blocks:
|
||||
# database_version.create(engine, checkfirst=True)
|
||||
|
||||
|
||||
def does_db_version_table_exist():
|
||||
if engine is None:
|
||||
raise RuntimeError("Engine not initialized. Call _reinitialize_engine_and_session() first.")
|
||||
|
||||
inspector = inspect(engine)
|
||||
table_exists = inspector.has_table("database_version")
|
||||
return table_exists
|
||||
|
||||
|
||||
def create_ALL_tables():
|
||||
"""Create all tables from BaseModel.metadata using the global engine."""
|
||||
print("starting create_ALL_tables")
|
||||
if engine is None:
|
||||
raise RuntimeError("Engine not initialized. Call _reinitialize_engine_and_session() first.")
|
||||
print("passed engine test")
|
||||
|
||||
from core.models.orm_models.Location import Location
|
||||
from core.models.orm_models.Operator import Operator
|
||||
from core.models.orm_models.CachedSync import CachedSync
|
||||
from core.models.orm_models.EncryptedProxy import EncryptedProxy
|
||||
|
||||
from core.models.SubscriptionPlan import SubscriptionPlan
|
||||
from core.models.session.ApplicationVersion import ApplicationVersion
|
||||
|
||||
|
||||
try:
|
||||
print("trying this")
|
||||
BaseModel.metadata.create_all(engine, checkfirst=True)
|
||||
print("Returning True for Creating all Tables..")
|
||||
logger.info("[DB MANAGEMENT] All Tables have been successfully created.")
|
||||
return True
|
||||
except:
|
||||
print("couldn't make all tables")
|
||||
logger.error("[DB MANAGEMENT] Fatal Error with creating all tables in the create_ALL_tables function of session management.")
|
||||
return False
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue