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 Column, Integer, MetaData, String, Table
19
from nova import log as logging
21
LOG = logging.getLogger(__name__)
24
def upgrade(migrate_engine):
26
meta.bind = migrate_engine
28
instances = Table('instances', meta, autoload=True)
31
for instance in migrate_engine.execute(instances.select()):
32
if instance.instance_type_id is None:
33
types[instance.id] = None
36
types[instance.id] = int(instance.instance_type_id)
38
LOG.warn("Instance %s did not have instance_type_id "
39
"converted to an integer because its value is %s" %
40
(instance.id, instance.instance_type_id))
41
types[instance.id] = None
43
integer_column = Column('instance_type_id_int', Integer(), nullable=True)
44
string_column = instances.c.instance_type_id
46
integer_column.create(instances)
47
for instance_id, instance_type_id in types.iteritems():
48
update = instances.update().\
49
where(instances.c.id == instance_id).\
50
values(instance_type_id_int=instance_type_id)
51
migrate_engine.execute(update)
53
string_column.alter(name='instance_type_id_str')
54
integer_column.alter(name='instance_type_id')
58
def downgrade(migrate_engine):
60
meta.bind = migrate_engine
62
instances = Table('instances', meta, autoload=True)
64
integer_column = instances.c.instance_type_id
65
string_column = Column('instance_type_id_str',
66
String(length=255, convert_unicode=False,
67
assert_unicode=None, unicode_error=None,
68
_warn_on_bytestring=False),
72
for instance in migrate_engine.execute(instances.select()):
73
if instance.instance_type_id is None:
74
types[instance.id] = None
76
types[instance.id] = str(instance.instance_type_id)
78
string_column.create(instances)
79
for instance_id, instance_type_id in types.iteritems():
80
update = instances.update().\
81
where(instances.c.id == instance_id).\
82
values(instance_type_id_str=instance_type_id)
83
migrate_engine.execute(update)
85
integer_column.alter(name='instance_type_id_int')
86
string_column.alter(name='instance_type_id')