function tripal_pub_update_7201

2.x tripal_pub.install tripal_pub_update_7201()
3.x tripal_pub.install tripal_pub_update_7201()

Adds missing foreign key constraints

File

tripal_pub/tripal_pub.install, line 388
Installation of the publication module

Code

function tripal_pub_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' => 'pubauthor_contact_pubauthor_id_fkey'))->fetchField();
    if ($fkey_exists) {
      chado_query('
        ALTER TABLE {pubauthor_contact}
        DROP CONSTRAINT pubauthor_contact_pubauthor_id_fkey CASCADE
      ');
      chado_query('
        ALTER TABLE {pubauthor_contact}
        DROP CONSTRAINT pubauthor_contact_contact_id_fkey CASCADE
      ');
    }
    chado_query('
      ALTER TABLE {pubauthor_contact}
      ADD CONSTRAINT pubauthor_contact_pubauthor_id_fkey
      FOREIGN KEY (pubauthor_id) REFERENCES {pubauthor} (pubauthor_id)
      ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
    ');
    chado_query('
      ALTER TABLE {pubauthor_contact}
      ADD CONSTRAINT pubauthor_contact_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);
  }
}