From d503d6b1079f9eafe0430754214fc5b6d4e32c09 Mon Sep 17 00:00:00 2001 From: Cor Cornelisse Date: Wed, 21 Dec 2011 12:46:58 +0100 Subject: Use SQLAlchemy to drop foreign key in DB migrate 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 --- ...064_change_instance_id_to_uuid_in_instance_actions.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'nova') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/064_change_instance_id_to_uuid_in_instance_actions.py b/nova/db/sqlalchemy/migrate_repo/versions/064_change_instance_id_to_uuid_in_instance_actions.py index a10ae11fc..bc9519f1b 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/064_change_instance_id_to_uuid_in_instance_actions.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/064_change_instance_id_to_uuid_in_instance_actions.py @@ -17,6 +17,7 @@ import sqlalchemy from sqlalchemy import select, Column, ForeignKey, Integer, String +from migrate import ForeignKeyConstraint from nova import log as logging @@ -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), @@ -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() -- cgit