--- /dev/null
+# All Rights Reserved.
+#
+# 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 Index, MetaData, Table
+from sqlalchemy.exc import IntegrityError
+
+
+def upgrade(migrate_engine):
+ meta = MetaData()
+ meta.bind = migrate_engine
+
+ reservations = Table('reservations', meta, autoload=True)
+
+ # Based on expire_reservations query
+ # from: cinder/db/sqlalchemy/api.py
+ index = Index('reservations_deleted_expire_idx',
+ reservations.c.deleted, reservations.c.expire)
+ try:
+ index.create(migrate_engine)
+ except IntegrityError:
+ pass
+
+
+def downgrade(migrate_engine):
+ meta = MetaData()
+ meta.bind = migrate_engine
+
+ reservations = Table('reservations', meta, autoload=True)
+
+ index = Index('reservations_deleted_expire_idx',
+ reservations.c.deleted, reservations.c.expire)
+ index.drop(migrate_engine)
metadata,
autoload=True)
self.assertNotIn('disabled_reason', services.c)
+
+ def test_migration_023(self):
+ """Test that adding reservations index works correctly."""
+ for (key, engine) in self.engines.items():
+ migration_api.version_control(engine,
+ TestMigrations.REPOSITORY,
+ migration.db_initial_version())
+ migration_api.upgrade(engine, TestMigrations.REPOSITORY, 22)
+ metadata = sqlalchemy.schema.MetaData()
+ metadata.bind = engine
+
+ migration_api.upgrade(engine, TestMigrations.REPOSITORY, 23)
+ reservations = sqlalchemy.Table('reservations',
+ metadata,
+ autoload=True)
+ index_colums = []
+ for idx in reservations.indexes:
+ if idx.name == 'reservations_deleted_expire_idx':
+ index_columns = idx.columns.keys()
+ break
+
+ self.assertEqual(sorted(['deleted', 'expire']),
+ sorted(index_columns))
+
+ migration_api.downgrade(engine, TestMigrations.REPOSITORY, 22)
+ metadata = sqlalchemy.schema.MetaData()
+ metadata.bind = engine
+
+ reservations = sqlalchemy.Table('reservations',
+ metadata,
+ autoload=True)
+ index_names = [idx.name for idx in reservations.indexes]
+ self.assertNotIn('reservations_deleted_expire_idx', index_names)