Enable tasks data model and table for async worker
Add tasks data model and table for async worker to store the tasks data persistently. Partially implement blueprint async-glance-workers Change-Id: I2b2d70295da4905bf75cffc2b2eadec174d1abfc
This commit is contained in:
parent
503c07d057
commit
6abd845ca7
|
@ -0,0 +1,66 @@
|
|||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2013 OpenStack Foundation
|
||||
# All Rights Reserved.
|
||||
# Copyright 2013 IBM Corp.
|
||||
#
|
||||
# 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.
|
||||
|
||||
from sqlalchemy.schema import (Column, MetaData, Table, Index)
|
||||
|
||||
from glance.db.sqlalchemy.migrate_repo.schema import (
|
||||
Boolean, DateTime, String, Text, create_tables, drop_tables)
|
||||
|
||||
|
||||
def define_tasks_table(meta):
|
||||
tasks = Table('tasks',
|
||||
meta,
|
||||
Column('id', String(36), primary_key=True, nullable=False),
|
||||
Column('type', String(30), nullable=False),
|
||||
Column('status', String(30), nullable=False),
|
||||
Column('owner', String(255), nullable=False),
|
||||
Column('input', Text()), # json blob
|
||||
Column('result', Text()), # json blob
|
||||
Column('message', Text()),
|
||||
Column('expires_at', DateTime(), nullable=True),
|
||||
Column('created_at', DateTime(), nullable=False),
|
||||
Column('updated_at', DateTime()),
|
||||
Column('deleted_at', DateTime()),
|
||||
Column('deleted',
|
||||
Boolean(),
|
||||
nullable=False,
|
||||
default=False),
|
||||
mysql_engine='InnoDB',
|
||||
extend_existing=True)
|
||||
|
||||
Index('ix_tasks_type', tasks.c.type)
|
||||
Index('ix_tasks_status', tasks.c.status)
|
||||
Index('ix_tasks_owner', tasks.c.owner)
|
||||
Index('ix_tasks_deleted', tasks.c.deleted)
|
||||
Index('ix_tasks_updated_at', tasks.c.updated_at)
|
||||
|
||||
return tasks
|
||||
|
||||
|
||||
def upgrade(migrate_engine):
|
||||
meta = MetaData()
|
||||
meta.bind = migrate_engine
|
||||
tables = [define_tasks_table(meta)]
|
||||
create_tables(tables)
|
||||
|
||||
|
||||
def downgrade(migrate_engine):
|
||||
meta = MetaData()
|
||||
meta.bind = migrate_engine
|
||||
tables = [define_tasks_table(meta)]
|
||||
drop_tables(tables)
|
|
@ -214,6 +214,25 @@ class ImageMember(BASE, ModelBase):
|
|||
status = Column(String(20), nullable=False, default="pending")
|
||||
|
||||
|
||||
class Task(BASE, ModelBase):
|
||||
"""Represents an task in the datastore"""
|
||||
__tablename__ = 'tasks'
|
||||
__table_args__ = (Index('ix_tasks_type', 'type'),
|
||||
Index('ix_tasks_status', 'status'),
|
||||
Index('ix_tasks_owner', 'owner'),
|
||||
Index('ix_tasks_deleted', 'deleted'),
|
||||
Index('ix_tasks_updated_at', 'updated_at'))
|
||||
|
||||
id = Column(String(36), primary_key=True, default=uuidutils.generate_uuid)
|
||||
type = Column(String(30))
|
||||
status = Column(String(30))
|
||||
input = Column(JSONEncodedDict())
|
||||
result = Column(JSONEncodedDict())
|
||||
owner = Column(String(255))
|
||||
message = Column(Text)
|
||||
expires_at = Column(DateTime, nullable=True)
|
||||
|
||||
|
||||
def register_models(engine):
|
||||
"""
|
||||
Creates database tables for all models with the given engine
|
||||
|
|
|
@ -993,3 +993,48 @@ class TestMigrations(utils.BaseTestCase):
|
|||
md = r['meta_data']
|
||||
d = pickle.loads(md)
|
||||
self.assertEqual(type(d), dict)
|
||||
|
||||
def _check_030(self, engine, data):
|
||||
table = "tasks"
|
||||
index_type = ('ix_tasks_type', ['type'])
|
||||
index_status = ('ix_tasks_status', ['status'])
|
||||
index_owner = ('ix_tasks_owner', ['owner'])
|
||||
index_deleted = ('ix_tasks_deleted', ['deleted'])
|
||||
index_updated_at = ('ix_tasks_updated_at', ['updated_at'])
|
||||
|
||||
meta = sqlalchemy.MetaData()
|
||||
meta.bind = engine
|
||||
|
||||
tasks_table = sqlalchemy.Table(table, meta, autoload=True)
|
||||
|
||||
index_data = [(idx.name, idx.columns.keys())
|
||||
for idx in tasks_table.indexes]
|
||||
|
||||
self.assertIn(index_type, index_data)
|
||||
self.assertIn(index_status, index_data)
|
||||
self.assertIn(index_owner, index_data)
|
||||
self.assertIn(index_deleted, index_data)
|
||||
self.assertIn(index_updated_at, index_data)
|
||||
|
||||
expected = [u'id',
|
||||
u'type',
|
||||
u'status',
|
||||
u'owner',
|
||||
u'input',
|
||||
u'result',
|
||||
u'message',
|
||||
u'expires_at',
|
||||
u'created_at',
|
||||
u'updated_at',
|
||||
u'deleted_at',
|
||||
u'deleted']
|
||||
|
||||
# NOTE(flwang): Skip the column type checking for now since Jenkins is
|
||||
# using sqlalchemy.dialects.postgresql.base.TIMESTAMP instead of
|
||||
# DATETIME which is using by mysql and sqlite.
|
||||
col_data = [col.name for col in tasks_table.columns]
|
||||
self.assertEqual(expected, col_data)
|
||||
|
||||
def _post_downgrade_030(self, engine):
|
||||
self.assertRaises(sqlalchemy.exc.NoSuchTableError,
|
||||
get_table, engine, 'tasks')
|
||||
|
|
Loading…
Reference in New Issue