Add tag table in db and migration file

Add a new table named 'server_tags' in mogan database. and
provide a migration script to upgrade the old datbase.

Change-Id: I509d4db82d2aaa4c75ba93812ff0055eb3bd56f7
Implements: blueprint server-tags-support
This commit is contained in:
Tao Li 2017-09-26 09:37:17 +08:00
parent 7744129c83
commit bbbe964ff4
3 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,43 @@
#
# 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.
"""add server tags
Revision ID: cf73c09d3ff2
Revises: 0de89f877016
Create Date: 2017-09-21 04:11:09.636891
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'cf73c09d3ff2'
down_revision = '91941bf1ebc9'
def upgrade():
op.create_table(
'server_tags',
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.Column('server_id', sa.Integer(), nullable=False),
sa.Column('tag', sa.String(length=255), nullable=False),
sa.PrimaryKeyConstraint('server_id', 'tag'),
sa.ForeignKeyConstraint(['server_id'],
['servers.id']),
sa.Index('server_tags_tag_idx', 'tag'),
mysql_ENGINE='InnoDB',
mysql_DEFAULT_CHARSET='UTF8'
)

View File

@ -373,3 +373,15 @@ class ServerGroup(Base):
@property
def members(self):
return [m.server_uuid for m in self._members]
class ServerTag(Base):
"""Represents a tag of a bare metal server."""
__tablename__ = 'server_tags'
__table_args__ = (
Index('server_tags_tag_idx', 'tag'),
table_args())
server_id = Column(Integer, ForeignKey('servers.id'),
primary_key=True, nullable=False)
tag = Column(String(255), primary_key=True, nullable=False)

View File

@ -245,6 +245,21 @@ class MigrationCheckersMixin(object):
self.assertIsInstance(nodes.c.resource_name.type,
sqlalchemy.types.String)
def _check_cf73c09d3ff2(self, engine, data):
server_tags = db_utils.get_table(engine, 'server_tags')
col_names = [column.name for column in server_tags.c]
self.assertIn('tag', col_names)
self.assertIsInstance(server_tags.c.tag.type,
sqlalchemy.types.String)
servers = db_utils.get_table(engine, 'servers')
data = {'id': '123', 'name': 'server1'}
servers.insert().execute(data)
data = {'server_id': '123', 'tag': 'tag1'}
server_tags.insert().execute(data)
tag = server_tags.select(server_tags.c.server_id == '123').execute().\
first()
self.assertEqual('tag1', tag['tag'])
def test_upgrade_and_version(self):
with patch_with_engine(self.engine):
self.migration_api.upgrade('head')