X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_policy_module%2Fcore%2Fdatabase.py;h=3ca2e33e21aa87ae7b6e1dd501f32ac3eacede0b;hb=10be7c984475707be716708dfdd2d91a655158dc;hp=a39f982c85ba9afac09a82a4a4af31d070659181;hpb=312c16596975a42d6294a1a2ca7af98b0ff2ffb5;p=osm%2FPOL.git diff --git a/osm_policy_module/core/database.py b/osm_policy_module/core/database.py index a39f982..3ca2e33 100644 --- a/osm_policy_module/core/database.py +++ b/osm_policy_module/core/database.py @@ -23,16 +23,18 @@ ## import datetime import logging +import os -from peewee import CharField, IntegerField, ForeignKeyField, Model, TextField, AutoField, DateTimeField +from peewee import CharField, IntegerField, ForeignKeyField, Model, TextField, AutoField, DateTimeField, Proxy +from peewee_migrate import Router from playhouse.db_url import connect +from osm_policy_module import migrations from osm_policy_module.core.config import Config log = logging.getLogger(__name__) -cfg = Config.instance() -db = connect(cfg.OSMPOL_SQL_DATABASE_URI) +db = Proxy() class BaseModel(Model): @@ -70,13 +72,14 @@ class ScalingAlarm(BaseModel): class DatabaseManager: - def create_tables(self): - try: - db.connect() - db.create_tables([ScalingGroup, ScalingPolicy, ScalingCriteria, ScalingAlarm]) - db.close() - except Exception: - log.exception("Error creating tables: ") + def __init__(self, config: Config): + db.initialize(connect(config.get('sql', 'database_uri'))) + + def create_tables(self) -> None: + with db.atomic(): + router = Router(db, os.path.dirname(migrations.__file__)) + router.run() def get_alarm(self, alarm_uuid: str): - return ScalingAlarm.select().where(ScalingAlarm.alarm_uuid == alarm_uuid).get() + with db.atomic(): + return ScalingAlarm.select().where(ScalingAlarm.alarm_uuid == alarm_uuid).get()