summaryrefslogtreecommitdiff
path: root/cinder/db/migrations/versions/daa98075b90d_add_resource_indexes.py
blob: b1135c6a7225fec165b9f7f881ea4f1e3ffae1a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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 resource indexes

Revision ID: daa98075b90d
Revises: 9c74c1c6971f
Create Date: 2021-11-26 10:26:41.883072
"""

from alembic import op
from oslo_db.sqlalchemy import enginefacade
from oslo_db.sqlalchemy import utils
from oslo_log import log as logging


LOG = logging.getLogger(__name__)


# revision identifiers, used by Alembic.
revision = 'daa98075b90d'
down_revision = 'c92a3e68beed'
branch_labels = None
depends_on = None

INDEXES = (
    ('groups', 'groups_deleted_project_id_idx', ('deleted', 'project_id')),

    ('group_snapshots', 'group_snapshots_deleted_project_id_idx',
     ('deleted', 'project_id')),

    ('volumes', 'volumes_deleted_project_id_idx', ('deleted', 'project_id')),
    ('volumes', 'volumes_deleted_host_idx', ('deleted', 'host')),

    ('backups', 'backups_deleted_project_id_idx', ('deleted', 'project_id')),

    ('snapshots', 'snapshots_deleted_project_id_idx', ('deleted',
                                                       'project_id')),
)


def upgrade():
    engine = enginefacade.reader.get_engine()
    is_mysql = engine.dialect.name == 'mysql'

    for table, idx_name, fields in INDEXES:
        # Skip creation in mysql if it already has the index
        if is_mysql and utils.index_exists(engine, table, idx_name):
            LOG.info('Skipping index %s, already exists', idx_name)
        else:
            op.create_index(idx_name, table, fields)