diff --git a/mogan/db/sqlalchemy/alembic/versions/cf73c09d3ff2_add_server_tags.py b/mogan/db/sqlalchemy/alembic/versions/cf73c09d3ff2_add_server_tags.py new file mode 100644 index 00000000..c1cdce5a --- /dev/null +++ b/mogan/db/sqlalchemy/alembic/versions/cf73c09d3ff2_add_server_tags.py @@ -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' + ) diff --git a/mogan/db/sqlalchemy/models.py b/mogan/db/sqlalchemy/models.py index 62e310eb..3148e611 100644 --- a/mogan/db/sqlalchemy/models.py +++ b/mogan/db/sqlalchemy/models.py @@ -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) diff --git a/mogan/tests/unit/db/sqlalchemy/test_migrations.py b/mogan/tests/unit/db/sqlalchemy/test_migrations.py index 80f2af33..eecf4204 100644 --- a/mogan/tests/unit/db/sqlalchemy/test_migrations.py +++ b/mogan/tests/unit/db/sqlalchemy/test_migrations.py @@ -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')