Merge "Change access column type for instance"

This commit is contained in:
Zuul 2020-09-16 15:13:17 +00:00 committed by Gerrit Code Review
commit 38977368be
3 changed files with 15 additions and 8 deletions

View File

@ -18,7 +18,6 @@
from oslo_log import log as logging from oslo_log import log as logging
import sqlalchemy.types import sqlalchemy.types
logger = logging.getLogger('trove.db.sqlalchemy.migrate_repo.schema') logger = logging.getLogger('trove.db.sqlalchemy.migrate_repo.schema')
@ -63,11 +62,6 @@ class Float(sqlalchemy.types.Float):
super(Float, self).__init__(*args, **kwargs) super(Float, self).__init__(*args, **kwargs)
class Json(sqlalchemy.types.JSON):
def __init__(self, *args, **kwargs):
super(Json, self).__init__(*args, **kwargs)
def create_tables(tables): def create_tables(tables):
for table in tables: for table in tables:
logger.info("creating table %(table)s", {'table': table}) logger.info("creating table %(table)s", {'table': table})

View File

@ -16,8 +16,8 @@
from sqlalchemy.schema import Column from sqlalchemy.schema import Column
from sqlalchemy.schema import MetaData from sqlalchemy.schema import MetaData
from trove.db.sqlalchemy.migrate_repo.schema import Json
from trove.db.sqlalchemy.migrate_repo.schema import Table from trove.db.sqlalchemy.migrate_repo.schema import Table
from trove.db.sqlalchemy.migrate_repo.schema import Text
def upgrade(migrate_engine): def upgrade(migrate_engine):
@ -25,4 +25,4 @@ def upgrade(migrate_engine):
meta.bind = migrate_engine meta.bind = migrate_engine
instances = Table('instances', meta, autoload=True) instances = Table('instances', meta, autoload=True)
instances.create_column(Column('access', Json(), nullable=True)) instances.create_column(Column('access', Text(), nullable=True))

View File

@ -17,6 +17,7 @@
"""Model classes that form the core of instances functionality.""" """Model classes that form the core of instances functionality."""
from datetime import datetime from datetime import datetime
from datetime import timedelta from datetime import timedelta
import json
import os.path import os.path
import re import re
@ -492,6 +493,8 @@ class SimpleInstance(object):
@property @property
def access(self): def access(self):
if hasattr(self.db_info, 'access'): if hasattr(self.db_info, 'access'):
if type(self.db_info.access) == str:
return json.loads(self.db_info.access)
return self.db_info.access return self.db_info.access
else: else:
return None return None
@ -885,6 +888,10 @@ class BaseInstance(SimpleInstance):
def update_db(self, **values): def update_db(self, **values):
self.db_info = DBInstance.find_by(id=self.id, deleted=False) self.db_info = DBInstance.find_by(id=self.id, deleted=False)
if 'access' in values and type(values['access'] != str):
values['access'] = json.dumps(values['access'])
for key in values: for key in values:
setattr(self.db_info, key, values[key]) setattr(self.db_info, key, values[key])
self.db_info.save() self.db_info.save()
@ -1884,6 +1891,12 @@ class DBInstance(dbmodels.DatabaseModelBase):
return cu.decrypt_data(cu.decode_data(self.encrypted_key), return cu.decrypt_data(cu.decode_data(self.encrypted_key),
CONF.inst_rpc_key_encr_key) CONF.inst_rpc_key_encr_key)
@classmethod
def create(cls, **values):
if 'access' in values and type(values['access'] != str):
values['access'] = json.dumps(values['access'])
return super(DBInstance, cls).create(**values)
def _validate(self, errors): def _validate(self, errors):
if InstanceTask.from_code(self.task_id) is None: if InstanceTask.from_code(self.task_id) is None:
errors['task_id'] = "Not valid." errors['task_id'] = "Not valid."