1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 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 select, Column, Integer, MetaData, Table
20
from nova import exception
21
from nova import flags
26
def upgrade_libvirt(instances, instance_types):
27
# Update instance_types first
29
for inst_type in instance_types.select().execute():
30
if inst_type['name'] == 'm1.tiny':
31
tiny = inst_type['id']
36
instance_types.update()\
37
.values(root_gb=root_gb,
38
ephemeral_gb=inst_type['local_gb'])\
39
.where(instance_types.c.id == inst_type['id'])\
42
# then update instances following same pattern
45
ephemeral_gb=instances.c.local_gb)\
51
ephemeral_gb=instances.c.local_gb)\
52
.where(instances.c.instance_type_id == tiny)\
56
def upgrade_other(instances, instance_types):
57
for table in (instances, instance_types):
58
table.update().values(root_gb=table.c.local_gb,
59
ephemeral_gb=0).execute()
62
def check_instance_presence(migrate_engine, instances_table):
63
result = migrate_engine.execute(instances_table.select().limit(1))
64
return result.fetchone() is not None
67
def upgrade(migrate_engine):
69
meta.bind = migrate_engine
70
instances = Table('instances', meta, autoload=True)
72
data_present = check_instance_presence(migrate_engine, instances)
74
if data_present and not FLAGS.connection_type:
75
msg = ("Found instance records in database. You must specify "
76
"connection_type to run migration migration")
77
raise exception.Error(msg)
79
instance_types = Table('instance_types', meta, autoload=True)
81
for table in (instances, instance_types):
82
root_gb = Column('root_gb', Integer)
84
ephemeral_gb = Column('ephemeral_gb', Integer)
85
ephemeral_gb.create(table)
87
# Since this migration is part of the work to get all drivers
88
# working the same way, we need to treat the new root_gb and
89
# ephemeral_gb columns differently depending on what the
90
# driver implementation used to behave like.
91
if FLAGS.connection_type == 'libvirt':
92
upgrade_libvirt(instances, instance_types)
94
upgrade_other(instances, instance_types)
96
default_local_device = instances.c.default_local_device
97
default_local_device.alter(name='default_ephemeral_device')
99
for table in (instances, instance_types):
100
table.drop_column('local_gb')
103
def downgrade(migrate_engine):
105
meta.bind = migrate_engine
106
instances = Table('instances', meta, autoload=True)
107
instance_types = Table('instance_types', meta, autoload=True)
109
for table in (instances, instance_types):
110
local_gb = Column('local_gb', Integer)
111
local_gb.create(table)
114
for table in (instances, instance_types):
115
if FLAGS.connection_type == 'libvirt':
116
column = table.c.ephemeral_gb
118
column = table.c.root_gb
119
table.update().values(local_gb=column).execute()
121
for table in (instances, instance_types):
122
table.drop_column('local_gb')
125
default_ephemeral_device = instances.c.default_ephemeral_device
126
default_ephemeral_device.alter(name='default_local_device')
128
for table in (instances, instance_types):
129
table.drop_column('root_gb')
130
table.drop_column('ephemeral_gb')