diff --git a/nova/db/sqlalchemy/api_migrations/migrate_repo/versions/062_instance_mapping_add_user_id.py b/nova/db/sqlalchemy/api_migrations/migrate_repo/versions/062_instance_mapping_add_user_id.py new file mode 100644 index 000000000000..34ea9c603036 --- /dev/null +++ b/nova/db/sqlalchemy/api_migrations/migrate_repo/versions/062_instance_mapping_add_user_id.py @@ -0,0 +1,32 @@ +# 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 import Column +from sqlalchemy import Index +from sqlalchemy import MetaData +from sqlalchemy import String +from sqlalchemy import Table + + +def upgrade(migrate_engine): + meta = MetaData() + meta.bind = migrate_engine + + instance_mappings = Table('instance_mappings', meta, autoload=True) + + if not hasattr(instance_mappings.c, 'user_id'): + instance_mappings.create_column(Column('user_id', String(length=255), + nullable=True)) + index = Index('instance_mappings_user_id_project_id_idx', + instance_mappings.c.user_id, + instance_mappings.c.project_id) + index.create() diff --git a/nova/db/sqlalchemy/api_models.py b/nova/db/sqlalchemy/api_models.py index 442ad3cb3048..609a75e2f452 100644 --- a/nova/db/sqlalchemy/api_models.py +++ b/nova/db/sqlalchemy/api_models.py @@ -134,13 +134,18 @@ class InstanceMapping(API_BASE): __table_args__ = (Index('project_id_idx', 'project_id'), Index('instance_uuid_idx', 'instance_uuid'), schema.UniqueConstraint('instance_uuid', - name='uniq_instance_mappings0instance_uuid')) + name='uniq_instance_mappings0instance_uuid'), + Index('instance_mappings_user_id_project_id_idx', + 'user_id', 'project_id')) id = Column(Integer, primary_key=True) instance_uuid = Column(String(36), nullable=False) cell_id = Column(Integer, ForeignKey('cell_mappings.id'), nullable=True) project_id = Column(String(255), nullable=False) + # FIXME(melwitt): This should eventually be non-nullable, but we need a + # transition period first. + user_id = Column(String(255), nullable=True) queued_for_delete = Column(Boolean) cell_mapping = orm.relationship('CellMapping', backref=backref('instance_mapping', uselist=False), diff --git a/nova/tests/functional/db/api/test_migrations.py b/nova/tests/functional/db/api/test_migrations.py index f9c7f82835e9..108be5af4d71 100644 --- a/nova/tests/functional/db/api/test_migrations.py +++ b/nova/tests/functional/db/api/test_migrations.py @@ -726,6 +726,11 @@ class NovaAPIMigrationsWalk(test_migrations.WalkVersionsMixin): self.assertColumnExists(engine, 'instance_mappings', 'queued_for_delete') + def _check_062(self, engine, data): + self.assertColumnExists(engine, 'instance_mappings', 'user_id') + self.assertIndexExists(engine, 'instance_mappings', + 'instance_mappings_user_id_project_id_idx') + class TestNovaAPIMigrationsWalkSQLite(NovaAPIMigrationsWalk, test_fixtures.OpportunisticDBTestMixin,