add databases modules

Change-Id: Idb1a453fd2a6d165deeb2321c73b06aab951102a
This commit is contained in:
jiasirui 2022-12-12 22:35:52 +08:00
parent 8164828f7b
commit 3a6167124e
3 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1,70 @@
# Copyright 2020 Inspur
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
SQLAlchemy models for venus data.
"""
from oslo_config import cfg
from oslo_db.sqlalchemy import models
from oslo_utils import timeutils
from sqlalchemy import Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import DateTime, String
CONF = cfg.CONF
BASE = declarative_base()
class VenusBase(models.TimestampMixin,
models.ModelBase):
"""Base class for Venus Models."""
__table_args__ = {'mysql_engine': 'InnoDB'}
# TODO(rpodolyaka): reuse models.SoftDeleteMixin in the next stage
# of implementing of BP db-cleanup
created_at = Column(DateTime)
updated_at = Column(DateTime)
deleted_at = Column(DateTime)
# deleted = Column(Boolean, default=False)
deleted = Column(String(1), default=0)
metadata = None
def delete(self, session):
"""Delete this object."""
self.deleted = True
self.deleted_at = timeutils.utcnow()
self.save(session=session)
def register_models():
"""Rvenuster Models and create metadata.
Called from venus.db.sqlalchemy.__init__ as part of loading the driver,
it will never need to be called explicitly elsewhere unless the
connection is lost and needs to be reestablished.
"""
from sqlalchemy import create_engine
models = ()
engine = create_engine(CONF.database.connection, echo=False)
for model in models:
model.metadata.create_all(engine)
class CustomConfig(BASE):
__tablename__ = 't_mo_custom_config'
id = Column(String(64), primary_key=True)
value = Column(String(10240))
update_time = Column(DateTime())

View File

@ -0,0 +1,97 @@
# Copyright 2020 Inspur
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Implementation of SQLAlchemy backend."""
import sqlalchemy
import threading
import time
from oslo_config import cfg
from oslo_db import options
from oslo_db.sqlalchemy import session as db_session
from oslo_log import log as logging
import osprofiler.sqlalchemy
from venus.modules.custom_config.backends import models
CONF = cfg.CONF
CONF.import_group("profiler", "venus.service")
log = logging.getLogger(__name__)
options.set_defaults(CONF, connection='sqlite:///$state_path/venus.sqlite')
_LOCK = threading.Lock()
_FACADE = None
def _create_facade_lazily():
global _LOCK
with _LOCK:
global _FACADE
if _FACADE is None:
_FACADE = db_session.EngineFacade(
CONF.database.connection,
**dict(CONF.database)
)
if CONF.profiler.profiler_enabled:
if CONF.profiler.trace_sqlalchemy:
osprofiler.sqlalchemy.add_tracing(sqlalchemy,
_FACADE.get_engine(),
"db")
return _FACADE
def get_session(**kwargs):
facade = _create_facade_lazily()
return facade.get_session(**kwargs)
class CustomConfigSql(object):
def get_config(self, id):
session = get_session()
with session.begin():
config = session.query(models.CustomConfig).filter_by(
id=id).first()
if config is None:
return None
else:
return config.value
def set_config(self, id, value):
session = get_session()
with session.begin():
config = session.query(models.CustomConfig).filter_by(
id=id).first()
if config is None:
s_instance = models.CustomConfig(
id=id,
value=value,
update_time=time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(time.time())))
session.add(s_instance)
else:
config.value = value
config.update_time = time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(time.time()))