function tripal_contact_update_7201

2.x tripal_contact.install tripal_contact_update_7201()

Adds missing foreign key constraints

File

tripal_contact/tripal_contact.install, line 357
Handles install, uninstall, disable and enable functionality including database tables.

Code

function tripal_contact_update_7201() {
  // Make sure we have the full API loaded this will help during a
  // site upgrade when the tripal_core module is disabled.
  module_load_include('module', 'tripal_core', 'tripal_core');
  tripal_core_import_api();

  // there was a bug in the function for creating a custom table that
  // kept foreign key constraints from being added.  So, we need to add those
  // to keep from error messages appear, we will drop the FK if it already
  // exists and then re-add it.
  try {
    $fkey_exists = db_query('SELECT TRUE FROM pg_constraint WHERE conname = :constraint', array(':constraint' => 'contactprop_type_id_fkey'))->fetchField();
    if ($fkey_exists) {
      chado_query('
        ALTER TABLE {contactprop}
        DROP CONSTRAINT contactprop_type_id_fkey CASCADE
      ');
      chado_query('
        ALTER TABLE {contactprop}
        DROP CONSTRAINT contactprop_contact_id_fkey CASCADE
      ');
    }
    chado_query('
      ALTER TABLE {contactprop}
      ADD CONSTRAINT contactprop_type_id_fkey
      FOREIGN KEY (type_id) REFERENCES {cvterm} (cvterm_id)
      ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
    ');
    chado_query('
      ALTER TABLE {contactprop}
      ADD CONSTRAINT contactprop_contact_id_fkey
      FOREIGN KEY (contact_id) REFERENCES {contact} (contact_id)
      ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
    ');
  }
  catch (\PDOException $e) {
    $error = $e->getMessage();
    throw new DrupalUpdateException('Failed to update foriegn key: ' . $error);
  }
}