1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010 OpenStack LLC.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
17
from sqlalchemy import MetaData, Table, Column, String
19
from nova.compute import task_states
20
from nova.compute import vm_states
23
_upgrade_translations = {
25
"state_description": vm_states.ACTIVE,
26
"task_state": task_states.STOPPING,
29
"state_description": vm_states.STOPPED,
33
"state_description": vm_states.DELETED,
37
"state_description": vm_states.ACTIVE,
38
"task_state": task_states.DELETING,
41
"state_description": vm_states.ACTIVE,
45
"state_description": vm_states.BUILDING,
46
"task_state": task_states.SCHEDULING,
49
"state_description": vm_states.MIGRATING,
53
"state_description": vm_states.BUILDING,
54
"task_state": task_states.SCHEDULING,
59
_downgrade_translations = {
62
task_states.DELETING: "terminating",
63
task_states.STOPPING: "stopping",
67
task_states.SCHEDULING: "scheduling",
72
vm_states.REBUILDING: {
78
vm_states.MIGRATING: {
84
def upgrade(migrate_engine):
86
meta.bind = migrate_engine
88
instance_table = Table('instances', meta, autoload=True)
90
c_state = instance_table.c.state
91
c_state.alter(name='power_state')
93
c_vm_state = instance_table.c.state_description
94
c_vm_state.alter(name='vm_state')
96
c_task_state = Column('task_state',
97
String(length=255, convert_unicode=False,
98
assert_unicode=None, unicode_error=None,
99
_warn_on_bytestring=False),
101
instance_table.create_column(c_task_state)
103
for old_state, values in _upgrade_translations.iteritems():
104
instance_table.update().\
106
where(c_vm_state == old_state).\
110
def downgrade(migrate_engine):
112
meta.bind = migrate_engine
114
instance_table = Table('instances', meta, autoload=True)
116
c_task_state = instance_table.c.task_state
118
c_state = instance_table.c.power_state
119
c_state.alter(name='state')
121
c_vm_state = instance_table.c.vm_state
122
c_vm_state.alter(name='state_description')
124
for old_vm_state, old_task_states in _downgrade_translations.iteritems():
125
for old_task_state, new_state_desc in old_task_states.iteritems():
126
instance_table.update().\
127
where(c_task_state == old_task_state).\
128
where(c_vm_state == old_vm_state).\
129
values(vm_state=new_state_desc).\
132
instance_table.drop_column('task_state')