old_contents: # vim: tabstop=4 shiftwidth=4 softtabstop=4
  
  # Copyright 2013 OpenStack LLC
  # Copyright 2013 Red Hat, Inc.
  # All Rights Reserved.
  #
  #    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.
  import migrate
  import sqlalchemy
  
  
  #  Different RDBMSs use different schemes for naming the Foreign Key
  #  Constraints.  SQLAlchemy does not yet attempt to determine the name
  #  for the constraint, and instead attempts to deduce it from the column.
  #  This fails on MySQL.
  def get_fkey_constraint_name(table, column_name):
      fkeys = [fk for fk in table.constraints
               if (column_name in fk.columns and
                   isinstance(fk, sqlalchemy.ForeignKeyConstraint))]
      constraint_name = fkeys[0].name
      return constraint_name
  
  
  #  remove_constraints and add_constraints both accept a list of dictionaries
  #  that contain:
  #  {'table': a sqlalchemy table. The constraint is added to to dropped from
  #           this table.
  #  'fk_column': the name of a column on the above table,  The constraint
  #               is added to or dropped from this column
  #  'ref_column':a sqlalchemy column object.  This is the reference column
  #               for the constraint.
  def remove_constraints(constraints):
      for constraint_def in constraints:
          migrate.ForeignKeyConstraint(
              columns=[getattr(constraint_def['table'].c,
                               constraint_def['fk_column'])],
              refcolumns=[constraint_def['ref_column']],
              name=(get_fkey_constraint_name
                    (constraint_def['table'],
                     constraint_def['fk_column']))).drop()
  
  
  def add_constraints(constraints):
      for constraint_def in constraints:
          migrate.ForeignKeyConstraint(
              columns=[getattr(constraint_def['table'].c,
                               constraint_def['fk_column'])],
              refcolumns=[constraint_def['ref_column']]).create()
  
new_contents: # vim: tabstop=4 shiftwidth=4 softtabstop=4
  
  # Copyright 2013 OpenStack LLC
  # Copyright 2013 Red Hat, Inc.
  # All Rights Reserved.
  #
  #    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.
  import migrate
  import sqlalchemy
  
  
  #  Different RDBMSs use different schemes for naming the Foreign Key
  #  Constraints.  SQLAlchemy does not yet attempt to determine the name
  #  for the constraint, and instead attempts to deduce it from the column.
  #  This fails on MySQL.
  def get_constraints_names(table, column_name):
      fkeys = [fk.name for fk in table.constraints
               if (column_name in fk.columns and
                   isinstance(fk, sqlalchemy.ForeignKeyConstraint))]
      return fkeys
  
  
  #  remove_constraints and add_constraints both accept a list of dictionaries
  #  that contain:
  #  {'table': a sqlalchemy table. The constraint is added to to dropped from
  #           this table.
  #  'fk_column': the name of a column on the above table,  The constraint
  #               is added to or dropped from this column
  #  'ref_column':a sqlalchemy column object.  This is the reference column
  #               for the constraint.
  def remove_constraints(constraints):
      for constraint_def in constraints:
          constraint_names = get_constraints_names(constraint_def['table'],
                                                   constraint_def['fk_column'])
          for constraint_name in constraint_names:
              migrate.ForeignKeyConstraint(
                  columns=[getattr(constraint_def['table'].c,
                                   constraint_def['fk_column'])],
                  refcolumns=[constraint_def['ref_column']],
                  name=constraint_name).drop()
  
  
  def add_constraints(constraints):
      for constraint_def in constraints:
          migrate.ForeignKeyConstraint(
              columns=[getattr(constraint_def['table'].c,
                               constraint_def['fk_column'])],
              refcolumns=[constraint_def['ref_column']]).create()
  
subject: check for constraint before dropping