~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/db/sqlalchemy/migrate_repo/versions/113_fixed_ips_uses_uuid.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2011 OpenStack LLC.
 
4
# Copyright 2012 Michael Still and Canonical Inc
 
5
# All Rights Reserved.
 
6
#
 
7
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
8
#    not use this file except in compliance with the License. You may obtain
 
9
#    a copy of the License at
 
10
#
 
11
#         http://www.apache.org/licenses/LICENSE-2.0
 
12
#
 
13
#    Unless required by applicable law or agreed to in writing, software
 
14
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
15
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
16
#    License for the specific language governing permissions and limitations
 
17
#    under the License.
 
18
 
 
19
from migrate import ForeignKeyConstraint
 
20
from sqlalchemy import MetaData, String, Table
 
21
from sqlalchemy import select, Column, ForeignKey, Integer
 
22
 
 
23
from nova.openstack.common import log as logging
 
24
 
 
25
 
 
26
LOG = logging.getLogger(__name__)
 
27
 
 
28
 
 
29
def upgrade(migrate_engine):
 
30
    meta = MetaData()
 
31
    meta.bind = migrate_engine
 
32
    fixed_ips = Table('fixed_ips', meta, autoload=True)
 
33
    instances = Table('instances', meta, autoload=True)
 
34
    uuid_column = Column('instance_uuid', String(36))
 
35
    uuid_column.create(fixed_ips)
 
36
 
 
37
    try:
 
38
        fixed_ips.update().values(
 
39
            instance_uuid=select(
 
40
                [instances.c.uuid],
 
41
                instances.c.id == fixed_ips.c.instance_id)
 
42
        ).execute()
 
43
    except Exception:
 
44
        uuid_column.drop()
 
45
        raise
 
46
 
 
47
    fkeys = list(fixed_ips.c.instance_id.foreign_keys)
 
48
    if fkeys:
 
49
        try:
 
50
            fkey_name = fkeys[0].constraint.name
 
51
            ForeignKeyConstraint(
 
52
                columns=[fixed_ips.c.instance_id],
 
53
                refcolumns=[instances.c.id],
 
54
                name=fkey_name).drop()
 
55
        except Exception:
 
56
            LOG.error(_("foreign key constraint couldn't be removed"))
 
57
            raise
 
58
 
 
59
    fixed_ips.c.instance_id.drop()
 
60
 
 
61
    try:
 
62
        ForeignKeyConstraint(
 
63
            columns=[fixed_ips.c.instance_uuid],
 
64
            refcolumns=[instances.c.uuid]).create()
 
65
    except Exception:
 
66
        LOG.error(_("foreign key constraint couldn't be created"))
 
67
        raise
 
68
 
 
69
 
 
70
def downgrade(migrate_engine):
 
71
    meta = MetaData()
 
72
    meta.bind = migrate_engine
 
73
    fixed_ips = Table('fixed_ips', meta, autoload=True)
 
74
    instances = Table('instances', meta, autoload=True)
 
75
    id_column = Column('instance_id', Integer, ForeignKey('instances.id'))
 
76
    id_column.create(fixed_ips)
 
77
 
 
78
    fkeys = list(fixed_ips.c.instance_uuid.foreign_keys)
 
79
    if fkeys:
 
80
        try:
 
81
            fkey_name = fkeys[0].constraint.name
 
82
            ForeignKeyConstraint(
 
83
                columns=[fixed_ips.c.instance_uuid],
 
84
                refcolumns=[instances.c.uuid],
 
85
                name=fkey_name).drop()
 
86
        except Exception:
 
87
            LOG.error(_("foreign key constraint couldn't be removed"))
 
88
            raise
 
89
 
 
90
    try:
 
91
        fixed_ips.update().values(
 
92
            instance_id=select(
 
93
                [instances.c.id],
 
94
                instances.c.uuid == fixed_ips.c.instance_uuid)
 
95
        ).execute()
 
96
    except Exception:
 
97
        id_column.drop()
 
98
        raise
 
99
 
 
100
    fixed_ips.c.instance_uuid.drop()
 
101
 
 
102
    try:
 
103
        ForeignKeyConstraint(
 
104
            columns=[fixed_ips.c.instance_id],
 
105
            refcolumns=[instances.c.id]).create()
 
106
    except Exception:
 
107
        LOG.error(_("foreign key constraint couldn't be created"))
 
108
        raise