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
18
from sqlalchemy import MetaData, Table, Column, String
20
from nova.compute import task_states
21
from nova.compute import vm_states
27
c_task_state = Column('task_state',
28
String(length=255, convert_unicode=False,
29
assert_unicode=None, unicode_error=None,
30
_warn_on_bytestring=False),
34
_upgrade_translations = {
36
"state_description": vm_states.ACTIVE,
37
"task_state": task_states.STOPPING,
40
"state_description": vm_states.STOPPED,
44
"state_description": vm_states.DELETED,
48
"state_description": vm_states.ACTIVE,
49
"task_state": task_states.DELETING,
52
"state_description": vm_states.ACTIVE,
56
"state_description": vm_states.BUILDING,
57
"task_state": task_states.SCHEDULING,
60
"state_description": vm_states.MIGRATING,
64
"state_description": vm_states.BUILDING,
65
"task_state": task_states.SCHEDULING,
70
_downgrade_translations = {
73
task_states.DELETING: "terminating",
74
task_states.STOPPING: "stopping",
78
task_states.SCHEDULING: "scheduling",
83
vm_states.REBUILDING: {
89
vm_states.MIGRATING: {
95
def upgrade(migrate_engine):
96
meta.bind = migrate_engine
98
instance_table = Table('instances', meta, autoload=True,
99
autoload_with=migrate_engine)
101
c_state = instance_table.c.state
102
c_state.alter(name='power_state')
104
c_vm_state = instance_table.c.state_description
105
c_vm_state.alter(name='vm_state')
107
instance_table.create_column(c_task_state)
109
for old_state, values in _upgrade_translations.iteritems():
110
instance_table.update().\
112
where(c_vm_state == old_state).\
116
def downgrade(migrate_engine):
117
meta.bind = migrate_engine
119
instance_table = Table('instances', meta, autoload=True,
120
autoload_with=migrate_engine)
122
c_task_state = instance_table.c.task_state
124
c_state = instance_table.c.power_state
125
c_state.alter(name='state')
127
c_vm_state = instance_table.c.vm_state
128
c_vm_state.alter(name='state_description')
130
for old_vm_state, old_task_states in _downgrade_translations.iteritems():
131
for old_task_state, new_state_desc in old_task_states.iteritems():
132
instance_table.update().\
133
where(c_task_state == old_task_state).\
134
where(c_vm_state == old_vm_state).\
135
values(vm_state=new_state_desc).\
138
instance_table.drop_column('task_state')