1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012 OpenStack LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
18
from migrate import ForeignKeyConstraint
19
from sqlalchemy import Integer
20
from sqlalchemy import MetaData, String, Table
22
from migrate import ForeignKeyConstraint
23
from nova.openstack.common import log as logging
25
LOG = logging.getLogger(__name__)
28
def upgrade(migrate_engine):
29
"""Convert volume and snapshot id columns from int to varchar."""
31
meta.bind = migrate_engine
33
volumes = Table('volumes', meta, autoload=True)
34
snapshots = Table('snapshots', meta, autoload=True)
35
iscsi_targets = Table('iscsi_targets', meta, autoload=True)
36
volume_metadata = Table('volume_metadata', meta, autoload=True)
37
sm_volume = Table('sm_volume', meta, autoload=True)
38
block_device_mapping = Table('block_device_mapping', meta, autoload=True)
41
fkeys = list(snapshots.c.volume_id.foreign_keys)
43
fkey_name = fkeys[0].constraint.name
44
ForeignKeyConstraint(columns=[snapshots.c.volume_id],
45
refcolumns=[volumes.c.id],
46
name=fkey_name).drop()
48
fkeys = list(iscsi_targets.c.volume_id.foreign_keys)
50
fkey_name = fkeys[0].constraint.name
51
ForeignKeyConstraint(columns=[iscsi_targets.c.volume_id],
52
refcolumns=[volumes.c.id],
53
name=fkey_name).drop()
55
fkeys = list(volume_metadata.c.volume_id.foreign_keys)
57
fkey_name = fkeys[0].constraint.name
58
ForeignKeyConstraint(columns=[volume_metadata.c.volume_id],
59
refcolumns=[volumes.c.id],
60
name=fkey_name).drop()
62
fkeys = list(sm_volume.c.id.foreign_keys)
64
fkey_name = fkeys[0].constraint.name
65
ForeignKeyConstraint(columns=[sm_volume.c.id],
66
refcolumns=[volumes.c.id],
67
name=fkey_name).drop()
69
fkeys = list(block_device_mapping.c.volume_id.foreign_keys)
71
fkey_name = fkeys[0].constraint.name
72
ForeignKeyConstraint(columns=[block_device_mapping.c.volume_id],
73
refcolumns=[volumes.c.id],
74
name=fkey_name).drop()
76
fkeys = list(block_device_mapping.c.snapshot_id.foreign_keys)
78
fkey_name = fkeys[0].constraint.name
79
ForeignKeyConstraint(columns=[block_device_mapping.c.snapshot_id],
80
refcolumns=[snapshots.c.id],
81
name=fkey_name).drop()
84
LOG.error(_("Foreign Key constraint couldn't be removed"))
87
volumes.c.id.alter(String(36), primary_key=True)
88
volumes.c.snapshot_id.alter(String(36))
89
volume_metadata.c.volume_id.alter(String(36), nullable=False)
90
snapshots.c.id.alter(String(36), primary_key=True)
91
snapshots.c.volume_id.alter(String(36))
92
sm_volume.c.id.alter(String(36))
93
block_device_mapping.c.volume_id.alter(String(36))
94
block_device_mapping.c.snapshot_id.alter(String(36))
95
iscsi_targets.c.volume_id.alter(String(36), nullable=True)
98
fkeys = list(snapshots.c.volume_id.foreign_keys)
100
fkey_name = fkeys[0].constraint.name
101
ForeignKeyConstraint(columns=[snapshots.c.volume_id],
102
refcolumns=[volumes.c.id],
103
name=fkey_name).create()
105
fkeys = list(iscsi_targets.c.volume_id.foreign_keys)
107
fkey_name = fkeys[0].constraint.name
108
ForeignKeyConstraint(columns=[iscsi_targets.c.volume_id],
109
refcolumns=[volumes.c.id],
110
name=fkey_name).create()
112
fkeys = list(volume_metadata.c.volume_id.foreign_keys)
114
fkey_name = fkeys[0].constraint.name
115
ForeignKeyConstraint(columns=[volume_metadata.c.volume_id],
116
refcolumns=[volumes.c.id],
117
name=fkey_name).create()
119
fkeys = list(sm_volume.c.id.foreign_keys)
121
fkey_name = fkeys[0].constraint.name
122
ForeignKeyConstraint(columns=[sm_volume.c.id],
123
refcolumns=[volumes.c.id],
124
name=fkey_name).create()
125
# NOTE(jdg) We're intentionally leaving off FK's on BDM
128
LOG.error(_("Foreign Key constraint couldn't be removed"))
132
def downgrade(migrate_engine):
133
"""Convert volume and snapshot id columns back to int."""
135
meta.bind = migrate_engine
136
dialect = migrate_engine.url.get_dialect().name
138
if dialect.startswith('sqlite'):
141
volumes = Table('volumes', meta, autoload=True)
142
snapshots = Table('snapshots', meta, autoload=True)
143
iscsi_targets = Table('iscsi_targets', meta, autoload=True)
144
volume_metadata = Table('volume_metadata', meta, autoload=True)
145
sm_volume = Table('sm_volume', meta, autoload=True)
146
block_device_mapping = Table('block_device_mapping', meta, autoload=True)
149
fkeys = list(snapshots.c.volume_id.foreign_keys)
151
fkey_name = fkeys[0].constraint.name
152
ForeignKeyConstraint(columns=[snapshots.c.volume_id],
153
refcolumns=[volumes.c.id],
154
name=fkey_name).drop()
156
fkeys = list(iscsi_targets.c.volume_id.foreign_keys)
158
fkey_name = fkeys[0].constraint.name
159
ForeignKeyConstraint(columns=[iscsi_targets.c.volume_id],
160
refcolumns=[volumes.c.id],
161
name=fkey_name).drop()
163
fkeys = list(volume_metadata.c.volume_id.foreign_keys)
165
fkey_name = fkeys[0].constraint.name
166
ForeignKeyConstraint(columns=[volume_metadata.c.volume_id],
167
refcolumns=[volumes.c.id],
168
name=fkey_name).drop()
170
fkeys = list(sm_volume.c.id.foreign_keys)
172
fkey_name = fkeys[0].constraint.name
173
ForeignKeyConstraint(columns=[sm_volume.c.id],
174
refcolumns=[volumes.c.id],
175
name=fkey_name).drop()
178
LOG.error(_("Foreign Key constraint couldn't be removed"))
181
volumes.c.id.alter(Integer, primary_key=True, autoincrement=True)
182
volumes.c.snapshot_id.alter(Integer)
183
volume_metadata.c.volume_id.alter(Integer, nullable=False)
184
snapshots.c.id.alter(Integer, primary_key=True, autoincrement=True)
185
snapshots.c.volume_id.alter(Integer)
186
sm_volume.c.id.alter(Integer)
187
block_device_mapping.c.volume_id.alter(Integer)
188
block_device_mapping.c.snapshot_id.alter(Integer)
189
iscsi_targets.c.volume_id.alter(Integer, nullable=True)
192
fkeys = list(snapshots.c.volume_id.foreign_keys)
194
fkey_name = fkeys[0].constraint.name
195
ForeignKeyConstraint(columns=[snapshots.c.volume_id],
196
refcolumns=[volumes.c.id],
197
name=fkey_name).create()
199
fkeys = list(iscsi_targets.c.volume_id.foreign_keys)
201
fkey_name = fkeys[0].constraint.name
202
ForeignKeyConstraint(columns=[iscsi_targets.c.volume_id],
203
refcolumns=[volumes.c.id],
204
name=fkey_name).create()
206
fkeys = list(volume_metadata.c.volume_id.foreign_keys)
208
fkey_name = fkeys[0].constraint.name
209
ForeignKeyConstraint(columns=[volume_metadata.c.volume_id],
210
refcolumns=[volumes.c.id],
211
name=fkey_name).create()
213
fkeys = list(sm_volume.c.id.foreign_keys)
215
fkey_name = fkeys[0].constraint.name
216
ForeignKeyConstraint(columns=[sm_volume.c.id],
217
refcolumns=[volumes.c.id],
218
name=fkey_name).create()
220
# NOTE(jdg) Put the BDM foreign keys back in place
221
fkeys = list(block_device_mapping.c.volume_id.foreign_keys)
223
fkey_name = fkeys[0].constraint.name
224
ForeignKeyConstraint(columns=[block_device_mapping.c.volume_id],
225
refcolumns=[volumes.c.id],
226
name=fkey_name).drop()
228
fkeys = list(block_device_mapping.c.snapshot_id.foreign_keys)
230
fkey_name = fkeys[0].constraint.name
231
ForeignKeyConstraint(columns=[block_device_mapping.c.snapshot_id],
232
refcolumns=[snapshots.c.id],
233
name=fkey_name).drop()
236
LOG.error(_("Foreign Key constraint couldn't be removed"))