Skip to content

Commit

Permalink
Use SQLAlchemy to drop foreign key in DB migrate
Browse files Browse the repository at this point in the history
A foreign key constraint needs to be removed in order to succeed in the
DB migration. An earlier submitted fix for this uses a mysql drop
statement. I think it's cleaner to use sqlalchemy to do this, as it's
done this way in other version migrations as well. Fix for bug 907254.

Update: IBM from 1928 called, they want their 80 CPL limit
back. - on a more serious note, fixed jenkins PEP8 failure
Update2: Replaced tab by a spaces
Update3: Don't do Foreign Key stuff if the engine is sqlite
Update4: Add myself to the Authors file
Update5: With the correct mail address

Change-Id: Ib021e0ddb2c80ee63888435a9e3761c053534160
  • Loading branch information
Cor Cornelisse committed Dec 22, 2011
1 parent 5bcaecd commit d503d6b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions Authors
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Chris Behrens <cbehrens@codestud.com>
Christian Berendt <berendt@b1-systems.de>
Christopher MacGown <chris@pistoncloud.com>
Chuck Short <zulcss@ubuntu.com>
Cor Cornelisse <cor@hyves.nl>
Cory Wright <corywright@gmail.com>
Dan Prince <dan.prince@rackspace.com>
Dan Wendlandt <dan@nicira.com>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import sqlalchemy
from sqlalchemy import select, Column, ForeignKey, Integer, String
from migrate import ForeignKeyConstraint

from nova import log as logging

Expand All @@ -31,6 +32,7 @@ def _get_table(name):

def upgrade(migrate_engine):
meta.bind = migrate_engine
dialect = migrate_engine.url.get_dialect().name
instance_actions = _get_table('instance_actions')
instances = _get_table('instances')
uuid_column = Column('instance_uuid', String(36),
Expand All @@ -48,12 +50,16 @@ def upgrade(migrate_engine):
uuid_column.drop()
raise

if migrate_engine.name == "mysql":
if not dialect.startswith('sqlite'):
try:
migrate_engine.execute("ALTER TABLE instance_actions " \
"DROP FOREIGN KEY instance_actions_ibfk_1;")
except Exception: # Don't care, just fail silently.
pass
fkey_name = list(instance_actions.c.instance_id.foreign_keys)[0].\
constraint.name
ForeignKeyConstraint(columns=[instance_actions.c.instance_id],
refcolumns=[instances.c.id],
name=fkey_name).drop()
except Exception:
logging.error(_("foreign key constraint couldn't be removed"))
raise

instance_actions.c.instance_id.drop()

Expand Down

0 comments on commit d503d6b

Please sign in to comment.