From 1b03a15f2818661008a52f4eda82c3aa5228365e Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Thu, 7 Sep 2017 15:18:56 +0800 Subject: [PATCH] Correct db and model inconsistent This is detected by the mogan-dbsync autogenerate. Change-Id: I5e310016aa0e2eb1bb4f6108905927eb48c4473f --- .../91941bf1ebc9_initial_migration.py | 58 +++++++++---------- mogan/db/sqlalchemy/api.py | 7 +-- mogan/db/sqlalchemy/models.py | 14 ++--- mogan/tests/unit/db/utils.py | 3 +- 4 files changed, 41 insertions(+), 41 deletions(-) diff --git a/mogan/db/sqlalchemy/alembic/versions/91941bf1ebc9_initial_migration.py b/mogan/db/sqlalchemy/alembic/versions/91941bf1ebc9_initial_migration.py index 753035a2..8ff293dc 100644 --- a/mogan/db/sqlalchemy/alembic/versions/91941bf1ebc9_initial_migration.py +++ b/mogan/db/sqlalchemy/alembic/versions/91941bf1ebc9_initial_migration.py @@ -39,9 +39,11 @@ def upgrade(): sa.Column('resources', sa.Text(), nullable=True), sa.Column('resource_traits', sa.Text(), nullable=True), sa.Column('resource_aggregates', sa.Text(), nullable=True), - sa.Column('is_public', sa.Boolean(), nullable=False), - sa.Column('disabled', sa.Boolean(), nullable=False), + sa.Column('is_public', sa.Boolean(), nullable=True), + sa.Column('disabled', sa.Boolean(), nullable=True), sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('uuid', name='uniq_flavors0uuid'), + sa.UniqueConstraint('name', name='uniq_flavors0name'), mysql_ENGINE='InnoDB', mysql_DEFAULT_CHARSET='UTF8' ) @@ -55,6 +57,8 @@ def upgrade(): sa.ForeignKeyConstraint(['flavor_id'], ['flavors.id']), sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('flavor_id', 'project_id', + name='uniq_flavor_projects0flavor_id0project_id'), mysql_ENGINE='InnoDB', mysql_DEFAULT_CHARSET='UTF8' ) @@ -62,11 +66,11 @@ def upgrade(): 'servers', sa.Column('created_at', sa.DateTime(), nullable=True), sa.Column('updated_at', sa.DateTime(), nullable=True), - sa.Column('uuid', sa.String(length=36), nullable=True), + sa.Column('uuid', sa.String(length=36), nullable=False), sa.Column('id', sa.Integer(), nullable=False), sa.Column('user_id', sa.String(length=36), nullable=True), sa.Column('project_id', sa.String(length=36), nullable=True), - sa.Column('name', sa.String(length=255), nullable=True), + sa.Column('name', sa.String(length=255), nullable=False), sa.Column('description', sa.String(length=255), nullable=True), sa.Column('status', sa.String(length=255), nullable=True), sa.Column('power_state', sa.String(length=15), nullable=True), @@ -83,28 +87,26 @@ def upgrade(): sa.Column('key_name', sa.String(length=255), nullable=True), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('uuid', name='uniq_servers0uuid'), + sa.Index('servers_project_id_idx', 'project_id'), mysql_ENGINE='InnoDB', mysql_DEFAULT_CHARSET='UTF8' ) - op.create_index('servers_project_id_idx', 'servers', ['project_id'], - unique=False) op.create_table( 'server_nics', sa.Column('created_at', sa.DateTime(), nullable=True), sa.Column('updated_at', sa.DateTime(), nullable=True), - sa.Column('server_uuid', sa.String(length=36), nullable=False), + sa.Column('server_uuid', sa.String(length=36), + sa.ForeignKey('servers.uuid'), nullable=True), sa.Column('port_id', sa.String(length=36), nullable=False), - sa.Column('mac_address', sa.String(length=36), nullable=True), + sa.Column('mac_address', sa.String(length=36), nullable=False), sa.Column('network_id', sa.String(length=36), nullable=True), sa.Column('floating_ip', sa.String(length=64), nullable=True), sa.Column('fixed_ips', sa.Text(), nullable=True), - sa.ForeignKeyConstraint(['server_uuid'], ['servers.uuid'], ), sa.PrimaryKeyConstraint('port_id'), + sa.Index('server_nics_server_uuid_idx', 'server_uuid'), mysql_ENGINE='InnoDB', mysql_DEFAULT_CHARSET='UTF8' ) - op.create_index('server_nics_server_uuid_idx', 'server_nics', - ['server_uuid'], unique=False) op.create_table( 'server_faults', sa.Column('id', sa.Integer(), nullable=False), @@ -116,18 +118,17 @@ def upgrade(): sa.Column('detail', sa.Text(), nullable=True), sa.ForeignKeyConstraint(['server_uuid'], ['servers.uuid']), sa.PrimaryKeyConstraint('id'), + sa.Index('server_faults_server_uuid_idx', 'server_uuid'), mysql_ENGINE='InnoDB', mysql_DEFAULT_CHARSET='UTF8' ) - op.create_index('server_faults_server_uuid_idx', 'server_faults', - ['server_uuid'], unique=False) op.create_table( 'quotas', sa.Column('created_at', sa.DateTime(), nullable=True), sa.Column('updated_at', sa.DateTime(), nullable=True), sa.Column('id', sa.Integer(), nullable=False), - sa.Column('project_id', sa.String(length=36), nullable=True), - sa.Column('resource_name', sa.String(length=255), nullable=True), + sa.Column('project_id', sa.String(length=36), nullable=False), + sa.Column('resource_name', sa.String(length=255), nullable=False), sa.Column('hard_limit', sa.Integer(), nullable=True), sa.Column('allocated', sa.Integer(), nullable=True), sa.PrimaryKeyConstraint('id'), @@ -148,43 +149,41 @@ def upgrade(): sa.Column('until_refresh', sa.Integer(), nullable=True), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('resource_name', 'project_id', - name='uniq_quotas_usages0resource_name'), + name='uniq_quota_usages0resource_name'), + sa.Index('quota_usage_project_id_idx', 'project_id'), mysql_ENGINE='InnoDB', mysql_DEFAULT_CHARSET='UTF8' ) - op.create_index('quota_usage_project_id_idx', 'quota_usages', - ['project_id'], unique=False) op.create_table( 'reservations', sa.Column('created_at', sa.DateTime(), nullable=True), sa.Column('updated_at', sa.DateTime(), nullable=True), sa.Column('id', sa.Integer(), nullable=False), - sa.Column('uuid', sa.String(length=36), nullable=True), + sa.Column('uuid', sa.String(length=36), nullable=False), sa.Column('usage_id', sa.Integer(), nullable=True), sa.Column('allocated_id', sa.Integer(), nullable=True), sa.Column('project_id', sa.String(length=36), nullable=True), sa.Column('resource_name', sa.String(length=255), nullable=True), sa.Column('delta', sa.Integer(), nullable=True), - sa.Column('expire', sa.DateTime(), nullable=True), + sa.Column('expire', sa.DateTime(), nullable=False), sa.ForeignKeyConstraint(['usage_id'], ['quota_usages.id']), sa.ForeignKeyConstraint(['allocated_id'], ['quotas.id']), sa.PrimaryKeyConstraint('id'), + sa.Index('reservations_project_id_idx', 'project_id'), sa.UniqueConstraint('uuid', name='uniq_reservation0uuid'), mysql_ENGINE='InnoDB', mysql_DEFAULT_CHARSET='UTF8' ) - op.create_index('reservations_project_id_idx', 'reservations', - ['project_id'], unique=False) op.create_table( 'key_pairs', sa.Column('created_at', sa.DateTime(), nullable=True), sa.Column('updated_at', sa.DateTime(), nullable=True), sa.Column('id', sa.Integer(), primary_key=True, nullable=False), sa.Column('name', sa.String(length=255), nullable=False), - sa.Column('user_id', sa.String(length=255), nullable=True), - sa.Column('project_id', sa.String(length=36), nullable=True), + sa.Column('user_id', sa.String(length=255), nullable=False), + sa.Column('project_id', sa.String(length=36), nullable=False), sa.Column('fingerprint', sa.String(255)), sa.Column('public_key', sa.Text()), sa.Column('type', sa.Enum('ssh', 'x509'), nullable=False, @@ -216,18 +215,18 @@ def upgrade(): sa.PrimaryKeyConstraint('id'), sa.ForeignKeyConstraint(['aggregate_id'], ['aggregates.id']), + sa.UniqueConstraint('aggregate_id', 'key', + name='uniq_aggregate_metadata0aggregate_id0key'), + sa.Index('aggregate_metadata_key_idx', 'key'), mysql_ENGINE='InnoDB', mysql_DEFAULT_CHARSET='UTF8' ) - op.create_index('aggregate_metadata_key_idx', 'aggregate_metadata', - ['key'], unique=False) - op.create_table( 'server_groups', sa.Column('created_at', sa.DateTime(), nullable=True), sa.Column('updated_at', sa.DateTime(), nullable=True), sa.Column('id', sa.Integer(), primary_key=True, nullable=False), - sa.Column('uuid', sa.String(length=36), nullable=True), + sa.Column('uuid', sa.String(length=36), nullable=False), sa.Column('name', sa.String(length=255), nullable=False), sa.Column('user_id', sa.String(length=255), nullable=True), sa.Column('project_id', sa.String(length=255), nullable=True), @@ -242,7 +241,8 @@ def upgrade(): sa.Column('updated_at', sa.DateTime(), nullable=True), sa.Column('id', sa.Integer(), primary_key=True, nullable=False), sa.Column('policy', sa.String(length=36), nullable=True), - sa.Column('group_id', sa.String(length=255), nullable=False), + sa.Column('group_id', sa.Integer, sa.ForeignKey('server_groups.id'), + nullable=False), sa.Index('server_group_policy_policy_idx', 'policy'), sa.PrimaryKeyConstraint('id'), mysql_engine='InnoDB', diff --git a/mogan/db/sqlalchemy/api.py b/mogan/db/sqlalchemy/api.py index 47ae0195..5b02f93b 100644 --- a/mogan/db/sqlalchemy/api.py +++ b/mogan/db/sqlalchemy/api.py @@ -1042,14 +1042,13 @@ class Connection(api.Connection): group = query.first() if not group: raise exception.ServerGroupNotFound(group_uuid=group_uuid) - group_id = group.id - with _session_for_write(): - query.delete() # Delete policies and members instance_models = [models.ServerGroupPolicy, models.ServerGroupMember] for model in instance_models: - model_query(context, model).filter_by(group_id=group_id).delete() + model_query(context, model).filter_by(group_id=group.id).delete() + with _session_for_write(): + query.delete() def server_group_get_all(self, context, project_id=None): """Get all groups.""" diff --git a/mogan/db/sqlalchemy/models.py b/mogan/db/sqlalchemy/models.py index 11bf5ab8..296fbd0f 100644 --- a/mogan/db/sqlalchemy/models.py +++ b/mogan/db/sqlalchemy/models.py @@ -103,7 +103,8 @@ class ServerNic(Base): Index('server_nics_server_uuid_idx', 'server_uuid'), table_args() ) - server_uuid = Column(String(36), ForeignKey('servers.uuid')) + server_uuid = Column(String(36), ForeignKey('servers.uuid'), + nullable=True) port_id = Column(String(36), primary_key=True) mac_address = Column(String(32), nullable=False) network_id = Column(String(36), nullable=True) @@ -175,7 +176,7 @@ class FlavorProjects(Base): ) id = Column(Integer, primary_key=True) flavor_id = Column(Integer, ForeignKey('flavors.id'), - nullable=True) + nullable=False) project_id = Column(String(36), nullable=True) flavors = orm.relationship( Flavors, @@ -207,13 +208,13 @@ class QuotaUsage(Base): __tablename__ = 'quota_usages' __table_args__ = ( schema.UniqueConstraint('resource_name', 'project_id', - name='uniq_quotas0resource_name'), + name='uniq_quota_usages0resource_name'), Index('quota_usage_project_id_idx', 'project_id'), table_args() ) id = Column(Integer, primary_key=True) - project_id = Column(String(255), index=True) + project_id = Column(String(255)) resource_name = Column(String(255)) in_use = Column(Integer) reserved = Column(Integer) @@ -238,7 +239,7 @@ class Reservation(Base): uuid = Column(String(36), nullable=False) usage_id = Column(Integer, ForeignKey('quota_usages.id'), nullable=True) allocated_id = Column(Integer, ForeignKey('quotas.id'), nullable=True) - project_id = Column(String(255), index=True) + project_id = Column(String(255)) resource_name = Column(String(255)) delta = Column(Integer) expire = Column(DateTime, nullable=False) @@ -295,7 +296,6 @@ class Aggregate(Base): __tablename__ = 'aggregates' __table_args__ = ( - Index('aggregate_uuid_idx', 'uuid'), table_args() ) id = Column(Integer, primary_key=True) @@ -356,7 +356,7 @@ class ServerGroup(Base): user_id = Column(String(255)) project_id = Column(String(255)) uuid = Column(String(36), nullable=False) - name = Column(String(255)) + name = Column(String(255), nullable=False) _policies = orm.relationship( ServerGroupPolicy, primaryjoin='ServerGroup.id == ServerGroupPolicy.group_id') diff --git a/mogan/tests/unit/db/utils.py b/mogan/tests/unit/db/utils.py index 7d40d0a5..c677d9db 100644 --- a/mogan/tests/unit/db/utils.py +++ b/mogan/tests/unit/db/utils.py @@ -21,6 +21,7 @@ from mogan.db import api as db_api def get_test_server(**kw): + server_uuid = uuidutils.generate_uuid() fake_server_nics = [{ 'port_id': uuidutils.generate_uuid(), 'network_id': 'bf942f63-c284-4eb8-925b-c2fa1a89ed33', @@ -40,7 +41,7 @@ def get_test_server(**kw): return { 'id': kw.get('id', 123), - 'uuid': kw.get('uuid', uuidutils.generate_uuid()), + 'uuid': kw.get('uuid', server_uuid), 'name': kw.get('name', 'test'), 'description': kw.get('description', 'test'), 'project_id': kw.get('project_id',