support json field in db

Change-Id: I99ea44d967b509214f975da8a8b7a588100f4785
This commit is contained in:
Muhamad Najjar 2017-12-11 13:10:06 +00:00
parent 359c38cf68
commit 01fa84178b
4 changed files with 31 additions and 9 deletions

View File

@ -18,7 +18,6 @@ import dateutil.parser
import oslo_messaging as oslo_m
from oslo_log import log
from oslo_serialization import jsonutils
from oslo_service import service as os_service
from vitrage.common.constants import DatasourceProperties as DSProps
from vitrage.common.constants import GraphAction
@ -73,6 +72,6 @@ class VitragePersistorEndpoint(object):
return
collector_timestamp = \
dateutil.parser.parse(data.get(DSProps.SAMPLE_DATE))
event_row = models.Event(payload=jsonutils.dumps(data),
event_row = models.Event(payload=data,
collector_timestamp=collector_timestamp)
self.db_connection.events.create(event_row)

View File

@ -61,6 +61,7 @@ class Connection(base.Connection):
def upgrade(self, nocreate=False):
engine = self._engine_facade.get_engine()
engine.connect()
models.Base.metadata.drop_all(engine, tables=[models.Event.__table__])
models.Base.metadata.create_all(engine)
# TODO(idan_hefetz) upgrade logic is missing

View File

@ -14,8 +14,11 @@
from oslo_db.sqlalchemy import models
from sqlalchemy import Column, DateTime, INTEGER, String, \
SmallInteger, BigInteger, Index, Text
SmallInteger, BigInteger, Index
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy.types as types
import json
class VitrageBase(models.ModelBase):
@ -39,6 +42,28 @@ class VitrageBase(models.ModelBase):
Base = declarative_base(cls=VitrageBase)
class StringyJSON(types.TypeDecorator):
"""Stores and retrieves JSON as TEXT.
Adjust JSON type to sqlite for unit test
"""
impl = types.TEXT
def process_bind_param(self, value, dialect):
if value is not None:
value = json.dumps(value)
return value
def process_result_value(self, value, dialect):
if value is not None:
value = json.loads(value)
return value
MagicJSON = types.JSON().with_variant(StringyJSON, 'sqlite')
class Event(Base):
__tablename__ = 'events'
@ -46,14 +71,14 @@ class Event(Base):
event_id = Column("id", INTEGER, primary_key=True, nullable=False,
autoincrement=True)
collector_timestamp = Column(DateTime, index=True, nullable=False)
payload = Column(Text, nullable=False)
payload = Column(MagicJSON, nullable=False)
def __repr__(self):
return \
"<Event(" \
"id='%s', " \
"collector_timestamp='%s', " \
"payload='%s')>" %\
"payload='%s')>" % \
(
self.event_id,
self.collector_timestamp,
@ -88,7 +113,7 @@ class ActiveAction(Base, models.TimestampMixin):
"target_vertex_id='%s', " \
"action_id='%s', " \
"score='%s', " \
"trigger='%s')>" %\
"trigger='%s')>" % \
(
self.created_at,
self.action_type,

View File

@ -16,7 +16,6 @@ import datetime
import six
from oslo_log import log as logging
from oslo_serialization import jsonutils
from vitrage.common.constants import DatasourceProperties as DSProps
from vitrage.datasources import NEUTRON_PORT_DATASOURCE
from vitrage.datasources import NOVA_INSTANCE_DATASOURCE
@ -129,6 +128,4 @@ class TestEvents(BaseVitrageTempest):
writen_events = self.db_connection.events.query(
gt_collector_timestamp=time_before_action)
for event in writen_events:
event.payload = jsonutils.loads(event.payload)
return writen_events