~ubuntu-branches/ubuntu/raring/cinder/raring-updates

« back to all changes in this revision

Viewing changes to cinder/db/sqlalchemy/migrate_repo/versions/004_volume_type_to_uuid.py

Tags: upstream-2013.1~g2
ImportĀ upstreamĀ versionĀ 2013.1~g2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
4
#    not use this file except in compliance with the License. You may obtain
 
5
#    a copy of the License at
 
6
#
 
7
#         http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
#    Unless required by applicable law or agreed to in writing, software
 
10
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
11
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
12
#    License for the specific language governing permissions and limitations
 
13
#    under the License.
 
14
 
 
15
import uuid
 
16
 
 
17
from cinder.openstack.common import log as logging
 
18
from migrate import ForeignKeyConstraint
 
19
from sqlalchemy import Integer, MetaData, String, Table
 
20
 
 
21
LOG = logging.getLogger(__name__)
 
22
 
 
23
 
 
24
def upgrade(migrate_engine):
 
25
    """Convert volume_type_id to UUID."""
 
26
    meta = MetaData()
 
27
    meta.bind = migrate_engine
 
28
 
 
29
    volumes = Table('volumes', meta, autoload=True)
 
30
    volume_types = Table('volume_types', meta, autoload=True)
 
31
    extra_specs = Table('volume_type_extra_specs', meta, autoload=True)
 
32
 
 
33
    fkey_remove_list = [volumes.c.volume_type_id,
 
34
                        volume_types.c.id,
 
35
                        extra_specs.c.volume_type_id]
 
36
 
 
37
    for column in fkey_remove_list:
 
38
        fkeys = list(column.foreign_keys)
 
39
        if fkeys:
 
40
            fkey_name = fkeys[0].constraint.name
 
41
            fkey = ForeignKeyConstraint(columns=[column],
 
42
                                        refcolumns=[volume_types.c.id],
 
43
                                        name=fkey_name)
 
44
 
 
45
            try:
 
46
                fkey.drop()
 
47
            except Exception:
 
48
                if migrate_engine.url.get_dialect().name.startswith('sqlite'):
 
49
                    pass
 
50
                else:
 
51
                    raise
 
52
 
 
53
    volumes.c.volume_type_id.alter(String(36))
 
54
    volume_types.c.id.alter(String(36))
 
55
    extra_specs.c.volume_type_id.alter(String(36))
 
56
 
 
57
    vtype_list = list(volume_types.select().execute())
 
58
    for t in vtype_list:
 
59
        new_id = str(uuid.uuid4())
 
60
 
 
61
        volumes.update().\
 
62
            where(volumes.c.volume_type_id == t['id']).\
 
63
            values(volume_type_id=new_id).execute()
 
64
 
 
65
        extra_specs.update().\
 
66
            where(extra_specs.c.volume_type_id == t['id']).\
 
67
            values(volume_type_id=new_id).execute()
 
68
 
 
69
        volume_types.update().\
 
70
            where(volume_types.c.id == t['id']).\
 
71
            values(id=new_id).execute()
 
72
 
 
73
    for column in fkey_remove_list:
 
74
        fkeys = list(column.foreign_keys)
 
75
        if fkeys:
 
76
            fkey_name = fkeys[0].constraint.name
 
77
            fkey = ForeignKeyConstraint(columns=[column],
 
78
                                        refcolumns=[volume_types.c.id],
 
79
                                        name=fkey_name)
 
80
            try:
 
81
                fkey.create()
 
82
                LOG.info('Created foreign key %s' % fkey_name)
 
83
            except Exception:
 
84
                if migrate_engine.url.get_dialect().name.startswith('sqlite'):
 
85
                    pass
 
86
                else:
 
87
                    raise
 
88
 
 
89
 
 
90
def downgrade(migrate_engine):
 
91
    """Convert volume_type from UUID back to int."""
 
92
    meta = MetaData()
 
93
    meta.bind = migrate_engine
 
94
 
 
95
    volumes = Table('volumes', meta, autoload=True)
 
96
    volume_types = Table('volume_types', meta, autoload=True)
 
97
    extra_specs = Table('volume_type_extra_specs', meta, autoload=True)
 
98
 
 
99
    fkey_remove_list = [volumes.c.volume_type_id,
 
100
                        volume_types.c.id,
 
101
                        extra_specs.c.volume_type_id]
 
102
 
 
103
    for column in fkey_remove_list:
 
104
        fkeys = list(column.foreign_keys)
 
105
        if fkeys:
 
106
            fkey_name = fkeys[0].constraint.name
 
107
            fkey = ForeignKeyConstraint(columns=[column],
 
108
                                        refcolumns=[volume_types.c.id],
 
109
                                        name=fkey_name)
 
110
 
 
111
            try:
 
112
                fkey.drop()
 
113
            except Exception:
 
114
                if migrate_engine.url.get_dialect().name.startswith('sqlite'):
 
115
                    pass
 
116
                else:
 
117
                    raise
 
118
 
 
119
    volumes.c.volume_type_id.alter(Integer)
 
120
    volume_types.c.id.alter(Integer)
 
121
    extra_specs.c.volume_type_id.alter(Integer)
 
122
 
 
123
    vtype_list = list(volume_types.select().execute())
 
124
    new_id = 1
 
125
 
 
126
    for t in vtype_list:
 
127
        volumes.update().\
 
128
            where(volumes.c.volume_type_id == t['id']).\
 
129
            values(volume_type_id=new_id).execute()
 
130
 
 
131
        extra_specs.update().\
 
132
            where(extra_specs.c.volume_type_id == t['id']).\
 
133
            values(volume_type_id=new_id).execute()
 
134
 
 
135
        volume_types.update().\
 
136
            where(volume_types.c.id == t['id']).\
 
137
            values(id=new_id).execute()
 
138
 
 
139
        new_id += 1
 
140
 
 
141
    for column in fkey_remove_list:
 
142
        fkeys = list(column.foreign_keys)
 
143
        if fkeys:
 
144
            fkey_name = fkeys[0].constraint.name
 
145
            fkey = ForeignKeyConstraint(columns=[column],
 
146
                                        refcolumns=[volume_types.c.id],
 
147
                                        name=fkey_name)
 
148
            try:
 
149
                fkey.create()
 
150
                LOG.info('Created foreign key %s' % fkey_name)
 
151
            except Exception:
 
152
                if migrate_engine.url.get_dialect().name.startswith('sqlite'):
 
153
                    pass
 
154
                else:
 
155
                    raise