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 sqlalchemy import Integer
19
from sqlalchemy import MetaData, String, Table
20
from migrate import ForeignKeyConstraint
21
from nova import log as logging
23
LOG = logging.getLogger(__name__)
26
def upgrade(migrate_engine):
27
"""Convert volume and snapshot id columns from int to varchar."""
29
meta.bind = migrate_engine
30
dialect = migrate_engine.url.get_dialect().name
32
volumes = Table('volumes', meta, autoload=True)
33
snapshots = Table('snapshots', meta, autoload=True)
34
iscsi_targets = Table('iscsi_targets', meta, autoload=True)
35
volume_metadata = Table('volume_metadata', meta, autoload=True)
36
sm_volume = Table('sm_volume', meta, autoload=True)
37
block_device_mapping = Table('block_device_mapping', meta, autoload=True)
40
fkeys = list(snapshots.c.volume_id.foreign_keys)
42
fkey_name = fkeys[0].constraint.name
43
ForeignKeyConstraint(columns=[snapshots.c.volume_id],
44
refcolumns=[volumes.c.id],
45
name=fkey_name).drop()
47
fkeys = list(iscsi_targets.c.volume_id.foreign_keys)
49
fkey_name = fkeys[0].constraint.name
50
ForeignKeyConstraint(columns=[iscsi_targets.c.volume_id],
51
refcolumns=[volumes.c.id],
52
name=fkey_name).drop()
54
fkeys = list(volume_metadata.c.volume_id.foreign_keys)
56
fkey_name = fkeys[0].constraint.name
57
ForeignKeyConstraint(columns=[volume_metadata.c.volume_id],
58
refcolumns=[volumes.c.id],
59
name=fkey_name).drop()
61
fkeys = list(sm_volume.c.id.foreign_keys)
63
fkey_name = fkeys[0].constraint.name
64
ForeignKeyConstraint(columns=[sm_volume.c.id],
65
refcolumns=[volumes.c.id],
66
name=fkey_name).drop()
68
fkeys = list(block_device_mapping.c.volume_id.foreign_keys)
70
fkey_name = fkeys[0].constraint.name
71
ForeignKeyConstraint(columns=[block_device_mapping.c.volume_id],
72
refcolumns=[volumes.c.id],
73
name=fkey_name).drop()
75
fkeys = list(block_device_mapping.c.snapshot_id.foreign_keys)
77
fkey_name = fkeys[0].constraint.name
78
ForeignKeyConstraint(columns=[block_device_mapping.c.snapshot_id],
79
refcolumns=[snapshots.c.id],
80
name=fkey_name).drop()
83
LOG.error(_("Foreign Key constraint couldn't be removed"))
86
volumes.c.id.alter(String(36), primary_key=True)
87
volumes.c.snapshot_id.alter(String(36))
88
volume_metadata.c.volume_id.alter(String(36), nullable=False)
89
snapshots.c.id.alter(String(36), primary_key=True)
90
snapshots.c.volume_id.alter(String(36))
91
sm_volume.c.id.alter(String(36))
92
block_device_mapping.c.volume_id.alter(String(36))
93
block_device_mapping.c.snapshot_id.alter(String(36))
94
iscsi_targets.c.volume_id.alter(String(36), nullable=True)
97
fkeys = list(snapshots.c.volume_id.foreign_keys)
99
fkey_name = fkeys[0].constraint.name
100
ForeignKeyConstraint(columns=[snapshots.c.volume_id],
101
refcolumns=[volumes.c.id],
102
name=fkey_name).create()
104
fkeys = list(iscsi_targets.c.volume_id.foreign_keys)
106
fkey_name = fkeys[0].constraint.name
107
ForeignKeyConstraint(columns=[iscsi_targets.c.volume_id],
108
refcolumns=[volumes.c.id],
109
name=fkey_name).create()
111
fkeys = list(volume_metadata.c.volume_id.foreign_keys)
113
fkey_name = fkeys[0].constraint.name
114
ForeignKeyConstraint(columns=[volume_metadata.c.volume_id],
115
refcolumns=[volumes.c.id],
116
name=fkey_name).create()
118
fkeys = list(sm_volume.c.id.foreign_keys)
120
fkey_name = fkeys[0].constraint.name
121
ForeignKeyConstraint(columns=[sm_volume.c.id],
122
refcolumns=[volumes.c.id],
123
name=fkey_name).create()
124
# NOTE(jdg) We're intentionally leaving off FK's on BDM
127
LOG.error(_("Foreign Key constraint couldn't be removed"))
131
def downgrade(migrate_engine):
132
"""Convert volume and snapshot id columns back to int."""
134
meta.bind = migrate_engine
135
dialect = migrate_engine.url.get_dialect().name
137
if dialect.startswith('sqlite'):
140
volumes = Table('volumes', meta, autoload=True)
141
snapshots = Table('snapshots', meta, autoload=True)
142
iscsi_targets = Table('iscsi_targets', meta, autoload=True)
143
volume_metadata = Table('volume_metadata', meta, autoload=True)
144
sm_volume = Table('sm_volume', meta, autoload=True)
145
block_device_mapping = Table('block_device_mapping', meta, autoload=True)
148
fkeys = list(snapshots.c.volume_id.foreign_keys)
150
fkey_name = fkeys[0].constraint.name
151
ForeignKeyConstraint(columns=[snapshots.c.volume_id],
152
refcolumns=[volumes.c.id],
153
name=fkey_name).drop()
155
fkeys = list(iscsi_targets.c.volume_id.foreign_keys)
157
fkey_name = fkeys[0].constraint.name
158
ForeignKeyConstraint(columns=[iscsi_targets.c.volume_id],
159
refcolumns=[volumes.c.id],
160
name=fkey_name).drop()
162
fkeys = list(volume_metadata.c.volume_id.foreign_keys)
164
fkey_name = fkeys[0].constraint.name
165
ForeignKeyConstraint(columns=[volume_metadata.c.volume_id],
166
refcolumns=[volumes.c.id],
167
name=fkey_name).drop()
169
fkeys = list(sm_volume.c.id.foreign_keys)
171
fkey_name = fkeys[0].constraint.name
172
ForeignKeyConstraint(columns=[sm_volume.c.id],
173
refcolumns=[volumes.c.id],
174
name=fkey_name).drop()
177
LOG.error(_("Foreign Key constraint couldn't be removed"))
180
volumes.c.id.alter(Integer, primary_key=True, autoincrement=True)
181
volumes.c.snapshot_id.alter(Integer)
182
volume_metadata.c.volume_id.alter(Integer, nullable=False)
183
snapshots.c.id.alter(Integer, primary_key=True, autoincrement=True)
184
snapshots.c.volume_id.alter(Integer)
185
sm_volume.c.id.alter(Integer)
186
block_device_mapping.c.volume_id.alter(Integer)
187
block_device_mapping.c.snapshot_id.alter(Integer)
188
iscsi_targets.c.volume_id.alter(Integer, nullable=True)
191
fkeys = list(snapshots.c.volume_id.foreign_keys)
193
fkey_name = fkeys[0].constraint.name
194
ForeignKeyConstraint(columns=[snapshots.c.volume_id],
195
refcolumns=[volumes.c.id],
196
name=fkey_name).create()
198
fkeys = list(iscsi_targets.c.volume_id.foreign_keys)
200
fkey_name = fkeys[0].constraint.name
201
ForeignKeyConstraint(columns=[iscsi_targets.c.volume_id],
202
refcolumns=[volumes.c.id],
203
name=fkey_name).create()
205
fkeys = list(volume_metadata.c.volume_id.foreign_keys)
207
fkey_name = fkeys[0].constraint.name
208
ForeignKeyConstraint(columns=[volume_metadata.c.volume_id],
209
refcolumns=[volumes.c.id],
210
name=fkey_name).create()
212
fkeys = list(sm_volume.c.id.foreign_keys)
214
fkey_name = fkeys[0].constraint.name
215
ForeignKeyConstraint(columns=[sm_volume.c.id],
216
refcolumns=[volumes.c.id],
217
name=fkey_name).create()
219
# NOTE(jdg) Put the BDM foreign keys back in place
220
fkeys = list(block_device_mapping.c.volume_id.foreign_keys)
222
fkey_name = fkeys[0].constraint.name
223
ForeignKeyConstraint(columns=[block_device_mapping.c.volume_id],
224
refcolumns=[volumes.c.id],
225
name=fkey_name).drop()
227
fkeys = list(block_device_mapping.c.snapshot_id.foreign_keys)
229
fkey_name = fkeys[0].constraint.name
230
ForeignKeyConstraint(columns=[block_device_mapping.c.snapshot_id],
231
refcolumns=[snapshots.c.id],
232
name=fkey_name).drop()
235
LOG.error(_("Foreign Key constraint couldn't be removed"))