~gandelman-a/ubuntu/precise/nova/UCA_2012.2.1

« back to all changes in this revision

Viewing changes to nova/db/sqlalchemy/migrate_repo/versions/100_instance_metadata_uses_uuid.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short, Adam Gandelman
  • Date: 2012-06-22 12:39:57 UTC
  • mfrom: (1.1.57)
  • Revision ID: package-import@ubuntu.com-20120622123957-hbzwg84nt9rqwg8r
Tags: 2012.2~f2~20120621.14517-0ubuntu1
[ Chuck Short ]
* New upstream version.

[ Adam Gandelman ]
* debian/rules: Temporarily disable test suite while blocking
  tests are investigated. 
* debian/patches/kombu_tests_timeout.patch: Dropped.

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 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
    instance_metadata = Table('instance_metadata', meta, autoload=True)
 
33
    instances = Table('instances', meta, autoload=True)
 
34
    uuid_column = Column('instance_uuid', String(36))
 
35
    uuid_column.create(instance_metadata)
 
36
 
 
37
    try:
 
38
        instance_metadata.update().values(
 
39
            instance_uuid=select(
 
40
                [instances.c.uuid],
 
41
                instances.c.id == instance_metadata.c.instance_id)
 
42
        ).execute()
 
43
    except Exception:
 
44
        uuid_column.drop()
 
45
        raise
 
46
 
 
47
    fkeys = list(instance_metadata.c.instance_id.foreign_keys)
 
48
    if fkeys:
 
49
        try:
 
50
            fkey_name = fkeys[0].constraint.name
 
51
            ForeignKeyConstraint(
 
52
                columns=[instance_metadata.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
    instance_metadata.c.instance_id.drop()
 
60
 
 
61
 
 
62
def downgrade(migrate_engine):
 
63
    meta = MetaData()
 
64
    meta.bind = migrate_engine
 
65
    instance_metadata = Table('instance_metadata', meta, autoload=True)
 
66
    instances = Table('instances', meta, autoload=True)
 
67
    id_column = Column('instance_id', Integer, ForeignKey('instances.id'))
 
68
    id_column.create(instance_metadata)
 
69
 
 
70
    try:
 
71
        instance_metadata.update().values(
 
72
            instance_id=select(
 
73
                [instances.c.id],
 
74
                instances.c.uuid == instance_metadata.c.instance_uuid)
 
75
        ).execute()
 
76
    except Exception:
 
77
        id_column.drop()
 
78
        raise
 
79
 
 
80
    instance_metadata.c.instance_uuid.drop()