Don't have db.create_all() run when Alembic runs because then it fails to detect new tables.
39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
# Standard library imports
|
|
import sys
|
|
|
|
# PyQt imports
|
|
|
|
# Third party imports
|
|
from alchemical import Alchemical # type:ignore
|
|
|
|
# App imports
|
|
|
|
|
|
def is_alembic_command():
|
|
# Define keywords that indicate Alembic is being invoked.
|
|
alembic_keywords = {'alembic', 'revision', 'upgrade', 'downgrade', 'history', 'current'}
|
|
return any(arg in alembic_keywords for arg in sys.argv)
|
|
|
|
|
|
class DatabaseManager:
|
|
"""
|
|
Singleton class to ensure we only ever have one db object
|
|
"""
|
|
|
|
__instance = None
|
|
|
|
def __init__(self, database_url: str, **kwargs: dict) -> None:
|
|
if DatabaseManager.__instance is None:
|
|
self.db = Alchemical(database_url, **kwargs)
|
|
if not is_alembic_command():
|
|
self.db.create_all()
|
|
DatabaseManager.__instance = self
|
|
else:
|
|
raise Exception("Attempted to create a second DatabaseManager instance")
|
|
|
|
@staticmethod
|
|
def get_instance(database_url: str, **kwargs: dict) -> Alchemical:
|
|
if DatabaseManager.__instance is None:
|
|
DatabaseManager(database_url, **kwargs)
|
|
return DatabaseManager.__instance
|