1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (c) 2012 Red Hat, Inc.
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
10
# http://www.apache.org/licenses/LICENSE-2.0
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
18
from sqlalchemy import Boolean, Column, DateTime, ForeignKey
19
from sqlalchemy import MetaData, String, Table
21
from nova import log as logging
23
LOG = logging.getLogger(__name__)
26
def run(migrate_engine):
28
meta.bind = migrate_engine
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:
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).
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'....
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.
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.
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')
56
#Bind new metadata to avoid issues after the rename
58
meta.bind = migrate_engine
59
projects = Table('projects', meta, autoload=True) # Required for fkey
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,
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',
75
dns_domains_new.create()
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'],
91
dns_domains_old.drop()