Coverage for manila/db/migrations/alembic/versions/e8ea58723178_remove_host_from_driver_private_data.py: 93%
40 statements
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
1# Copyright (c) 2016 EMC Corporation.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
16"""Remove host from driver private data
18Revision ID: e8ea58723178
19Revises: fdfb668d19e1
20Create Date: 2016-07-11 12:59:34.579291
22"""
24# revision identifiers, used by Alembic.
25revision = 'e8ea58723178'
26down_revision = 'fdfb668d19e1'
28from alembic import op
29from oslo_log import log
30from oslo_utils import uuidutils
31import sqlalchemy as sql
33from manila.db.migrations import utils
35LOG = log.getLogger(__name__)
36TABLE_NAME = 'drivers_private_data'
37COLUMN_HOST = 'host'
38DEFAULT_HOST = 'unknown'
39COLUMN_ENTITY = 'entity_uuid'
40COLUMN_KEY = 'key'
41MYSQL_ENGINE = 'mysql'
44def upgrade():
45 bind = op.get_bind()
46 engine = bind.engine
47 try:
48 if (engine.name == MYSQL_ENGINE):
49 op.drop_constraint('PRIMARY', TABLE_NAME, type_='primary')
50 op.create_primary_key('DRIVERS_PRIVATE_PK', TABLE_NAME,
51 ['entity_uuid', 'key'])
52 op.drop_column(TABLE_NAME, COLUMN_HOST)
53 except Exception:
54 LOG.error("Column '%s' could not be dropped", COLUMN_HOST)
55 raise
58def downgrade():
59 connection = op.get_bind()
60 from_table = utils.load_table(TABLE_NAME, connection)
61 migration_table_name = "_migrating_%(table)s_%(session)s" % {
62 'table': TABLE_NAME,
63 'session': uuidutils.generate_uuid()[:8]
64 }
66 LOG.info("Creating the migration table %(table)s", {
67 'table': migration_table_name
68 })
69 migration_table = op.create_table(
70 migration_table_name,
71 sql.Column('created_at', sql.DateTime),
72 sql.Column('updated_at', sql.DateTime),
73 sql.Column('deleted_at', sql.DateTime),
74 sql.Column('deleted', sql.Integer, default=0),
75 sql.Column('host', sql.String(255),
76 nullable=False, primary_key=True),
77 sql.Column('entity_uuid', sql.String(36),
78 nullable=False, primary_key=True),
79 sql.Column('key', sql.String(255),
80 nullable=False, primary_key=True),
81 sql.Column('value', sql.String(1023), nullable=False),
82 mysql_engine='InnoDB',
83 mysql_charset='utf8',
84 )
86 LOG.info("Copying data from %(from_table)s to the migration "
87 "table %(migration_table)s", {
88 'from_table': TABLE_NAME,
89 'migration_table': migration_table_name
90 })
91 rows = []
92 for row in op.get_bind().execute(from_table.select()):
93 rows.append({
94 'created_at': row.created_at,
95 'updated_at': row.updated_at,
96 'deleted_at': row.deleted_at,
97 'deleted': row.deleted,
98 'host': DEFAULT_HOST,
99 'entity_uuid': row.entity_uuid,
100 'key': row.key,
101 'value': row.value
102 })
103 op.bulk_insert(migration_table, rows)
105 LOG.info("Dropping table %(from_table)s", {
106 'from_table': TABLE_NAME
107 })
108 op.drop_table(TABLE_NAME)
110 LOG.info("Rename the migration table %(migration_table)s to "
111 "the original table %(from_table)s", {
112 'migration_table': migration_table_name,
113 'from_table': TABLE_NAME
114 })
115 op.rename_table(migration_table_name, TABLE_NAME)