1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
# not use this file except in compliance with the License. You may obtain
5
# a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
# License for the specific language governing permissions and limitations
17
from cinder.openstack.common import log as logging
18
from migrate import ForeignKeyConstraint
19
from sqlalchemy import Integer, MetaData, String, Table
21
LOG = logging.getLogger(__name__)
24
def upgrade(migrate_engine):
25
"""Convert volume_type_id to UUID."""
27
meta.bind = migrate_engine
29
volumes = Table('volumes', meta, autoload=True)
30
volume_types = Table('volume_types', meta, autoload=True)
31
extra_specs = Table('volume_type_extra_specs', meta, autoload=True)
33
fkey_remove_list = [volumes.c.volume_type_id,
35
extra_specs.c.volume_type_id]
37
for column in fkey_remove_list:
38
fkeys = list(column.foreign_keys)
40
fkey_name = fkeys[0].constraint.name
41
fkey = ForeignKeyConstraint(columns=[column],
42
refcolumns=[volume_types.c.id],
48
if migrate_engine.url.get_dialect().name.startswith('sqlite'):
53
volumes.c.volume_type_id.alter(String(36))
54
volume_types.c.id.alter(String(36))
55
extra_specs.c.volume_type_id.alter(String(36))
57
vtype_list = list(volume_types.select().execute())
59
new_id = str(uuid.uuid4())
62
where(volumes.c.volume_type_id == t['id']).\
63
values(volume_type_id=new_id).execute()
65
extra_specs.update().\
66
where(extra_specs.c.volume_type_id == t['id']).\
67
values(volume_type_id=new_id).execute()
69
volume_types.update().\
70
where(volume_types.c.id == t['id']).\
71
values(id=new_id).execute()
73
for column in fkey_remove_list:
74
fkeys = list(column.foreign_keys)
76
fkey_name = fkeys[0].constraint.name
77
fkey = ForeignKeyConstraint(columns=[column],
78
refcolumns=[volume_types.c.id],
82
LOG.info('Created foreign key %s' % fkey_name)
84
if migrate_engine.url.get_dialect().name.startswith('sqlite'):
90
def downgrade(migrate_engine):
91
"""Convert volume_type from UUID back to int."""
93
meta.bind = migrate_engine
95
volumes = Table('volumes', meta, autoload=True)
96
volume_types = Table('volume_types', meta, autoload=True)
97
extra_specs = Table('volume_type_extra_specs', meta, autoload=True)
99
fkey_remove_list = [volumes.c.volume_type_id,
101
extra_specs.c.volume_type_id]
103
for column in fkey_remove_list:
104
fkeys = list(column.foreign_keys)
106
fkey_name = fkeys[0].constraint.name
107
fkey = ForeignKeyConstraint(columns=[column],
108
refcolumns=[volume_types.c.id],
114
if migrate_engine.url.get_dialect().name.startswith('sqlite'):
119
volumes.c.volume_type_id.alter(Integer)
120
volume_types.c.id.alter(Integer)
121
extra_specs.c.volume_type_id.alter(Integer)
123
vtype_list = list(volume_types.select().execute())
128
where(volumes.c.volume_type_id == t['id']).\
129
values(volume_type_id=new_id).execute()
131
extra_specs.update().\
132
where(extra_specs.c.volume_type_id == t['id']).\
133
values(volume_type_id=new_id).execute()
135
volume_types.update().\
136
where(volume_types.c.id == t['id']).\
137
values(id=new_id).execute()
141
for column in fkey_remove_list:
142
fkeys = list(column.foreign_keys)
144
fkey_name = fkeys[0].constraint.name
145
fkey = ForeignKeyConstraint(columns=[column],
146
refcolumns=[volume_types.c.id],
150
LOG.info('Created foreign key %s' % fkey_name)
152
if migrate_engine.url.get_dialect().name.startswith('sqlite'):