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

1.1.59 by Chuck Short
Import upstream version 2012.2~f3
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3
# Copyright 2012 OpenStack LLC.
4
# All Rights Reserved.
5
#
6
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
7
#    not use this file except in compliance with the License. You may obtain
8
#    a copy of the License at
9
#
10
#         http://www.apache.org/licenses/LICENSE-2.0
11
#
12
#    Unless required by applicable law or agreed to in writing, software
13
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
#    License for the specific language governing permissions and limitations
16
#    under the License.
17
18
from migrate import ForeignKeyConstraint
19
from sqlalchemy import MetaData, Table
20
21
from nova.openstack.common import log as logging
22
23
24
LOG = logging.getLogger(__name__)
25
26
27
def upgrade(migrate_engine):
28
    meta = MetaData()
29
    meta.bind = migrate_engine
30
31
    dns_domains = Table('dns_domains', meta, autoload=True)
32
    projects = Table('projects', meta, autoload=True)
33
34
    fkeys = list(dns_domains.c.project_id.foreign_keys)
35
    if fkeys:
36
        try:
37
            fkey_name = fkeys[0].constraint.name
38
            ForeignKeyConstraint(
39
                columns=[dns_domains.c.project_id],
40
                refcolumns=[projects.c.id],
41
                name=fkey_name).drop()
42
        except Exception:
43
            LOG.error(_("foreign key constraint couldn't be removed"))
44
            raise
45
46
47
def downgrade(migrate_engine):
48
    meta = MetaData()
49
    meta.bind = migrate_engine
50
51
    dns_domains = Table('dns_domains', meta, autoload=True)
52
    projects = Table('projects', meta, autoload=True)
53
54
    kwargs = {
55
        'columns': [dns_domains.c.project_id],
56
        'refcolumns': [projects.c.id],
57
    }
58
59
    if migrate_engine.name == 'mysql':
60
        # For MySQL we name our fkeys explicitly so they match Essex
61
        kwargs['name'] = 'dns_domains_ibfk_1'
62
63
    ForeignKeyConstraint(**kwargs).create()