~yolanda.robla/nova/precise-security

« back to all changes in this revision

Viewing changes to nova/db/sqlalchemy/fix_dns_domains.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-06-05 09:50:59 UTC
  • mto: (79.1.1 precise-proposed)
  • mto: This revision was merged to the branch mainline in revision 81.
  • Revision ID: package-import@ubuntu.com-20120605095059-kb1utt7b1a5yff2a
Tags: upstream-2012.1+stable~20120612-3ee026e
ImportĀ upstreamĀ versionĀ 2012.1+stable~20120612-3ee026e

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright (c) 2012 Red Hat, Inc.
 
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 sqlalchemy import Boolean, Column, DateTime, ForeignKey
 
19
from sqlalchemy import MetaData, String, Table
 
20
 
 
21
from nova import log as logging
 
22
 
 
23
LOG = logging.getLogger(__name__)
 
24
 
 
25
 
 
26
def run(migrate_engine):
 
27
    meta = MetaData()
 
28
    meta.bind = migrate_engine
 
29
 
 
30
    # NOTE(dprince): The old dns_domains table is in the 'latin1'
 
31
    # charset and had its primary key length set to 512.
 
32
    # This is too long to be a valid pkey in the 'utf8' table charset
 
33
    # and is the root cause of errors like:
 
34
    #
 
35
    # 1) Dumping a database with mysqldump and trying to import it fails
 
36
    #    because this table is latin1 but fkeys to utf8 tables (projects).
 
37
    #
 
38
    # 2) Trying to alter the old dns_domains table fails with errors like:
 
39
    #    mysql> ALTER TABLE dns_domains DROP PRIMARY KEY;
 
40
    #    ERROR 1025 (HY000): Error on rename of './nova/#sql-6cf_855'....
 
41
    #
 
42
    # In short this table is just in a bad state. So... lets create a new one
 
43
    # with a shorter 'domain' column which is valid for the utf8 charset.
 
44
    #
 
45
    # Since this is stable/essex we fix the table without bumping
 
46
    # the migration number. The same upgrade will run during Folsom but
 
47
    # that won't hurt anything.
 
48
    #
 
49
    # https://bugs.launchpad.net/nova/+bug/993663
 
50
    dns_domains_old = Table('dns_domains', meta, autoload=True)
 
51
    domain_type = str(dns_domains_old.c.domain.type)
 
52
    if migrate_engine.name == 'mysql' and domain_type == 'VARCHAR(512)':
 
53
        LOG.audit('Applying database fix for Essex dns_domains table.')
 
54
        dns_domains_old.rename(name='dns_domains_old')
 
55
 
 
56
        #Bind new metadata to avoid issues after the rename
 
57
        meta = MetaData()
 
58
        meta.bind = migrate_engine
 
59
        projects = Table('projects', meta, autoload=True)  # Required for fkey
 
60
 
 
61
        dns_domains_new = Table('dns_domains', meta,
 
62
            Column('created_at', DateTime),
 
63
            Column('updated_at', DateTime),
 
64
            Column('deleted_at', DateTime),
 
65
            Column('deleted', Boolean),
 
66
            Column('domain', String(length=255), nullable=False,
 
67
                   primary_key=True),
 
68
            Column('scope', String(length=255)),
 
69
            Column('availability_zone', String(length=255)),
 
70
            Column('project_id', String(length=255),
 
71
                   ForeignKey('projects.id')),
 
72
            mysql_engine='InnoDB',
 
73
            mysql_charset='utf8',
 
74
        )
 
75
        dns_domains_new.create()
 
76
 
 
77
        dns_domains_old = Table('dns_domains_old', meta, autoload=True)
 
78
        record_list = list(dns_domains_old.select().execute())
 
79
        for rec in record_list:
 
80
            row = dns_domains_new.insert()
 
81
            row.execute({'created_at': rec['created_at'],
 
82
                        'updated_at': rec['updated_at'],
 
83
                        'deleted_at': rec['deleted_at'],
 
84
                        'deleted': rec['deleted'],
 
85
                        'domain': rec['domain'],
 
86
                        'scope': rec['scope'],
 
87
                        'availability_zone': rec['availability_zone'],
 
88
                        'project_id': rec['project_id'],
 
89
                        })
 
90
 
 
91
        dns_domains_old.drop()