projects
/
osm
/
POL.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Replaces direct use of aiokafka with osm_common message bus in agent and
[osm/POL.git]
/
osm_policy_module
/
core
/
database.py
diff --git
a/osm_policy_module/core/database.py
b/osm_policy_module/core/database.py
index
a39f982
..
330d8c5
100644
(file)
--- a/
osm_policy_module/core/database.py
+++ b/
osm_policy_module/core/database.py
@@
-24,15
+24,14
@@
import datetime
import logging
import datetime
import logging
-from peewee import CharField, IntegerField, ForeignKeyField, Model, TextField, AutoField, DateTimeField
+from peewee import CharField, IntegerField, ForeignKeyField, Model, TextField, AutoField, DateTimeField
, Proxy
from playhouse.db_url import connect
from osm_policy_module.core.config import Config
log = logging.getLogger(__name__)
from playhouse.db_url import connect
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):
class BaseModel(Model):
@@
-70,13
+69,14
@@
class ScalingAlarm(BaseModel):
class DatabaseManager:
class DatabaseManager:
+ def init_db(self, config: Config):
+ db.initialize(connect(config.get('sql', 'database_uri')))
+ self.create_tables()
+
def create_tables(self):
def create_tables(self):
- try:
- db.connect()
+ with db.atomic():
db.create_tables([ScalingGroup, ScalingPolicy, ScalingCriteria, ScalingAlarm])
db.create_tables([ScalingGroup, ScalingPolicy, ScalingCriteria, ScalingAlarm])
- db.close()
- except Exception:
- log.exception("Error creating tables: ")
def get_alarm(self, alarm_uuid: str):
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()