# Copyright 2011 OpenStack LLC.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from sqlalchemy import Column, Integer, MetaData, Table
from migrate import ForeignKeyConstraint

from nova import log as logging


meta = MetaData()


instances = Table('instances', meta,
             Column('id', Integer(), primary_key=True, nullable=False),
             )


vifs = Table('virtual_interfaces', meta,
        Column('id', Integer(), primary_key=True, nullable=False),
        Column('instance_id', Integer()),
        )


def upgrade(migrate_engine):
    # Upgrade operations go here. Don't create your own engine;
    # bind migrate_engine to your metadata
    meta.bind = migrate_engine
    dialect = migrate_engine.url.get_dialect().name
    if dialect.startswith('sqlite'):
        return

    try:
        ForeignKeyConstraint(columns=[vifs.c.instance_id],
                             refcolumns=[instances.c.id]).drop()
    except Exception:
        try:
            migrate_engine.execute("ALTER TABLE virtual_interfaces DROP " \
                                   "FOREIGN KEY " \
                                   "`virtual_interfaces_ibfk_2`;")
        except Exception:
            logging.error(_("foreign key constraint couldn't be removed"))
            raise


def downgrade(migrate_engine):
    # Operations to reverse the above upgrade go here.
    meta.bind = migrate_engine
    dialect = migrate_engine.url.get_dialect().name
    if dialect.startswith('sqlite'):
        return

    try:
        ForeignKeyConstraint(columns=[vifs.c.instance_id],
                             refcolumns=[instances.c.id]).create()
    except Exception:
        logging.error(_("foreign key constraint couldn't be added"))
        raise

@@ -731,17 +731,16 @@
 ogging%0A%0A
-%0A
 meta = M
@@ -755,296 +755,8 @@
 )%0A%0A%0A
-instances = Table('instances', meta,%0A             Column('id', Integer(), primary_key=True, nullable=False),%0A             )%0A%0A%0Avifs = Table('virtual_interfaces', meta,%0A        Column('id', Integer(), primary_key=True, nullable=False),%0A        Column('instance_id', Integer()),%0A        )%0A%0A%0A
 def 
@@ -1019,36 +1019,224 @@
     return%0A%0A    
-try:
+instances = Table('instances', meta, autoload=True)%0A    vifs = Table('virtual_interfaces', meta, autoload=True)%0A%0A    try:%0A        fkey_name = vifs.c.instance_id.foreign_keys%5B0%5D.constraint.name
 %0A        Foreign
@@ -1339,47 +1339,23 @@
 .id%5D
-).drop()%0A    except Exception:%0A
+,%0A     
         
 try:
@@ -1346,29 +1346,24 @@
             
-try:%0A
             
@@ -1366,195 +1366,36 @@
     
-migrate_engine.execute(%22ALTER TABLE virtual_interfaces DROP %22 %5C%0A                                   %22FOREIGN KEY %22 %5C%0A                                   %22%60virtual_interfaces_ibfk_2%60;%22)%0A    
+    name=fkey_name).drop()%0A%0A
     
@@ -1408,28 +1408,24 @@
  Exception:%0A
-    
         logg
@@ -1479,28 +1479,24 @@
  removed%22))%0A
-    
         rais
@@ -1717,24 +1717,141 @@
     return%0A%0A
+    instances = Table('instances', meta, autoload=True)%0A    vifs = Table('virtual_interfaces', meta, autoload=True)%0A%0A
     try:%0A   

Update migration 047 to dynamically lookup the name of the instance_id fkey before dropping it. We can't hard code the name of the fkey since we didn't name it explicitly on create.