This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
barkshark-web/barkshark_web/database/__init__.py
2022-10-05 13:30:39 -04:00

56 lines
989 B
Python

from izzylib.misc import class_name
from izzylib_sql import Database
from . import migrate
from .rows import row_classes
from .base import (
CustomSession,
default_config,
default_permissions,
default_searches,
subtypes,
tables
)
from .. import var
def get_database(app):
db = Database(
type = app.cfg.dbtype,
host = app.cfg.dbhost,
port = app.cfg.dbport,
database = app.cfg.dbname,
username = app.cfg.dbuser,
password = app.cfg.dbpass,
session_class = CustomSession,
maxconnections = 10,
row_classes = row_classes,
tables = tables
)
if db.cfg.type == 'sqlite':
db.vacuum()
with db.session as s:
## Check if database is up to date and migrate if necessary
try:
if not s.tables.get('config'):
version = 0
else:
version = s.get_config('version', 0)
except Exception as e:
if class_name(e).lower() == 'operationalerror':
version = 0
else:
raise e
if var.dbversion > version:
migrate.now(db, version)
return db