~ubuntu-branches/ubuntu/saucy/nova/saucy-proposed

« back to all changes in this revision

Viewing changes to nova/db/sqlalchemy/migrate_repo/versions/096_recreate_dns_domains.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-24 13:12:53 UTC
  • mfrom: (1.1.55)
  • Revision ID: package-import@ubuntu.com-20120524131253-ommql08fg1en06ut
Tags: 2012.2~f1-0ubuntu1
* New upstream release.
* Prepare for quantal:
  - Dropped debian/patches/upstream/0006-Use-project_id-in-ec2.cloud._format_image.patch
  - Dropped debian/patches/upstream/0005-Populate-image-properties-with-project_id-again.patch
  - Dropped debian/patches/upstream/0004-Fixed-bug-962840-added-a-test-case.patch
  - Dropped debian/patches/upstream/0003-Allow-unprivileged-RADOS-users-to-access-rbd-volumes.patch
  - Dropped debian/patches/upstream/0002-Stop-libvirt-test-from-deleting-instances-dir.patch
  - Dropped debian/patches/upstream/0001-fix-bug-where-nova-ignores-glance-host-in-imageref.patch 
  - Dropped debian/patches/0001-fix-useexisting-deprecation-warnings.patch
* debian/control: Add python-keystone as a dependency. (LP: #907197)
* debian/patches/kombu_tests_timeout.patch: Refreshed.
* debian/nova.conf, debian/nova-common.postinst: Convert to new ini
  file configuration
* debian/patches/nova-manage_flagfile_location.patch: Refreshed

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 select, Boolean, Column, DateTime, ForeignKey
 
19
from sqlalchemy import MetaData, String, Table
 
20
from migrate import ForeignKeyConstraint
 
21
 
 
22
 
 
23
def upgrade(migrate_engine):
 
24
    meta = MetaData()
 
25
    meta.bind = migrate_engine
 
26
 
 
27
    # NOTE(dprince): The old dns_domains table is in the 'latin1'
 
28
    # charset and had its primary key length set to 512.
 
29
    # This is too long to be a valid pkey in the 'utf8' table charset
 
30
    # and is the root cause of errors like:
 
31
    #
 
32
    # 1) Dumping a database with mysqldump and trying to import it fails
 
33
    #    because this table is latin1 but fkeys to utf8 tables (projects).
 
34
    #
 
35
    # 2) Trying to alter the old dns_domains table fails with errors like:
 
36
    #    mysql> ALTER TABLE dns_domains DROP PRIMARY KEY;
 
37
    #    ERROR 1025 (HY000): Error on rename of './nova/#sql-6cf_855'....
 
38
    #
 
39
    # In short this table is just in a bad state. So... lets create a new one
 
40
    # with a shorter 'domain' column which is valid for the utf8 charset.
 
41
    # https://bugs.launchpad.net/nova/+bug/993663
 
42
 
 
43
    #rename old table
 
44
    dns_domains_old = Table('dns_domains', meta, autoload=True)
 
45
    dns_domains_old.rename(name='dns_domains_old')
 
46
 
 
47
    # NOTE(dprince): manually remove pkey/fkey for postgres
 
48
    if migrate_engine.name == "postgresql":
 
49
        sql = """ALTER TABLE ONLY dns_domains_old DROP CONSTRAINT
 
50
              dns_domains_pkey;
 
51
              ALTER TABLE ONLY dns_domains_old DROP CONSTRAINT
 
52
              dns_domains_project_id_fkey;"""
 
53
        migrate_engine.execute(sql)
 
54
 
 
55
    #Bind new metadata to avoid issues after the rename
 
56
    meta = MetaData()
 
57
    meta.bind = migrate_engine
 
58
    projects = Table('projects', meta, autoload=True)  # Required for fkey
 
59
 
 
60
    dns_domains_new = Table('dns_domains', meta,
 
61
        Column('created_at', DateTime),
 
62
        Column('updated_at', DateTime),
 
63
        Column('deleted_at', DateTime),
 
64
        Column('deleted', Boolean),
 
65
        Column('domain', String(length=255), nullable=False, primary_key=True),
 
66
        Column('scope', String(length=255)),
 
67
        Column('availability_zone', String(length=255)),
 
68
        Column('project_id', String(length=255), ForeignKey('projects.id')),
 
69
        mysql_engine='InnoDB',
 
70
        mysql_charset='utf8',
 
71
    )
 
72
    dns_domains_new.create()
 
73
 
 
74
    dns_domains_old = Table('dns_domains_old', meta, autoload=True)
 
75
    record_list = list(dns_domains_old.select().execute())
 
76
    for rec in record_list:
 
77
        row = dns_domains_new.insert()
 
78
        row.execute({'created_at': rec['created_at'],
 
79
                    'updated_at': rec['updated_at'],
 
80
                    'deleted_at': rec['deleted_at'],
 
81
                    'deleted': rec['deleted'],
 
82
                    'domain': rec['domain'],
 
83
                    'scope': rec['scope'],
 
84
                    'availability_zone': rec['availability_zone'],
 
85
                    'project_id': rec['project_id'],
 
86
                    })
 
87
 
 
88
    dns_domains_old.drop()
 
89
 
 
90
 
 
91
def downgrade(migrate_engine):
 
92
    meta = MetaData()
 
93
    meta.bind = migrate_engine
 
94
    dns_domains_old = Table('dns_domains', meta, autoload=True)
 
95
    dns_domains_old.rename(name='dns_domains_old')
 
96
 
 
97
    # NOTE(dprince): manually remove pkey/fkey for postgres
 
98
    if migrate_engine.name == "postgresql":
 
99
        sql = """ALTER TABLE ONLY dns_domains_old DROP CONSTRAINT
 
100
              dns_domains_pkey;
 
101
              ALTER TABLE ONLY dns_domains_old DROP CONSTRAINT
 
102
              dns_domains_project_id_fkey;"""
 
103
        migrate_engine.execute(sql)
 
104
 
 
105
    #Bind new metadata to avoid issues after the rename
 
106
    meta = MetaData()
 
107
    meta.bind = migrate_engine
 
108
 
 
109
    dns_domains_new = Table('dns_domains', meta,
 
110
        Column('created_at', DateTime),
 
111
        Column('updated_at', DateTime),
 
112
        Column('deleted_at', DateTime),
 
113
        Column('deleted', Boolean),
 
114
        Column('domain', String(length=512), primary_key=True, nullable=False),
 
115
        Column('scope', String(length=255)),
 
116
        Column('availability_zone', String(length=255)),
 
117
        Column('project_id', String(length=255)),
 
118
        mysql_engine='InnoDB',
 
119
        mysql_charset='latin1',
 
120
    )
 
121
    dns_domains_new.create()
 
122
 
 
123
    dns_domains_old = Table('dns_domains_old', meta, autoload=True)
 
124
    record_list = list(dns_domains_old.select().execute())
 
125
    for rec in record_list:
 
126
        row = dns_domains_new.insert()
 
127
        row.execute({'created_at': rec['created_at'],
 
128
                    'updated_at': rec['updated_at'],
 
129
                    'deleted_at': rec['deleted_at'],
 
130
                    'deleted': rec['deleted'],
 
131
                    'domain': rec['domain'],
 
132
                    'scope': rec['scope'],
 
133
                    'availability_zone': rec['availability_zone'],
 
134
                    'project_id': rec['project_id'],
 
135
                    })
 
136
 
 
137
    dns_domains_old.drop()
 
138
 
 
139
    # NOTE(dprince): We can't easily add the MySQL Fkey on the downgrade
 
140
    # because projects is 'utf8' where dns_domains is 'latin1'.
 
141
    if migrate_engine.name != "mysql":
 
142
        projects = Table('projects', meta, autoload=True)
 
143
        fkey = ForeignKeyConstraint(columns=[dns_domains_new.c.project_id],
 
144
                               refcolumns=[projects.c.id])
 
145
        fkey.create()