Browse Source

Fixes from 2.x copied over to 3.x

Stephen Ficklin 8 years ago
parent
commit
6cd8b469b8

+ 64 - 22
tripal_chado/api/modules/tripal_chado.cv.api.inc

@@ -406,9 +406,7 @@ function tripal_insert_cv($name, $definition) {
  *  This function will add a cvterm record (and a dbxref record if appropriate
  *  values are provided). If the parent vocabulary does not exist then
  *  that also is added to the cv table.  If the cvterm is a relationship term
- *  then the 'is_relationship' value should be set.  The function will try
- *  to first find the relationship in the relationship ontology for updating and
- *  if it can not be found it will add the relationship to the "local" CV.  All
+ *  then the 'is_relationship' value should be set.  All
  *  terms must also have a corresponding database.  This is specified in the
  *  term's ID just before the colon (e.g. GO:003824).  If the database does not
  *  exist in the DB table then it will be added automatically.  The accession
@@ -518,7 +516,7 @@ function tripal_insert_cvterm($term, $options = array()) {
     $id = $name;
   }
 
-  // get the accession and the database from the cvterm id
+  // Get the accession and the database from the cvterm id.
   if ($dbname) {
     $accession = $id;
   }
@@ -528,7 +526,8 @@ function tripal_insert_cvterm($term, $options = array()) {
     $dbname = preg_replace('/^(.+?):.*$/', '\1', $id);
   }
 
-  // check that we have a database name, give a different message if it's a relationship
+  // Check that we have a database name, give a different message if it's a
+  // relationship.
   if ($is_relationship and !$dbname) {
     tripal_report_error('tripal_chado', TRIPAL_WARNING, "A database name is not provided for this relationship term: $id", NULL);
     return 0;
@@ -538,7 +537,7 @@ function tripal_insert_cvterm($term, $options = array()) {
     return 0;
   }
 
-  // make sure the CV name exists
+  // Make sure the CV name exists
   $cv = tripal_get_cv(array('name' => $cvname));
   if (!$cv) {
     $cv = tripal_insert_cv($cvname, '');
@@ -562,7 +561,7 @@ function tripal_insert_cvterm($term, $options = array()) {
     WHERE DBX.accession = :accession and DB.name = :name
   ";
 
-  // add the database. The function will just return the DB object if the
+  // Add the database. The function will just return the DB object if the
   // database already exists.
   $db = tripal_get_db(array('name' => $dbname));
   if (!$db) {
@@ -586,12 +585,19 @@ function tripal_insert_cvterm($term, $options = array()) {
   if (count($result) == 1) {
     $cvterm = $result[0];
 
-    // get the dbxref record
+    // Get the dbxref record.
     $values = array('dbxref_id' => $cvterm->dbxref_id);
     $result = chado_select_record('dbxref', array('*'), $values);
     $dbxref = $result[0];
+    if (!$dbxref) {
+      tripal_report_error('tripal_cv', TRIPAL_ERROR,
+        'Unable to access the dbxref record for the :term cvterm. Term Record: !record',
+        array(':term' => $name, '!record' => print_r($cvterm, TRUE))
+      );
+      return FALSE;
+    }
 
-    // get the db
+    // Get the db.
     $values = array('db_id' => $dbxref->db_id);
     $result = chado_select_record('db', array('*'), $values);
     $db_check = $result[0];
@@ -608,7 +614,6 @@ function tripal_insert_cvterm($term, $options = array()) {
 //         'db_id' => $db->db_id,
 //         'accession' => $accession,
 //       );
-
 //       $result = chado_select_record('dbxref', array('*'), $values);
 
 //       // If we already have a good dbxref then we want to update our cvterm
@@ -632,13 +637,13 @@ function tripal_insert_cvterm($term, $options = array()) {
 //       }
 //     }
 
-    // check that the accession matches.  Sometimes an OBO can define the same term
+    // Check that the accession matches.  Sometimes an OBO can define a term
     // multiple times but with different accessions.  If this is the case we
     // can't do an insert or it will violate the constraint in the cvterm table.
-    // so we'll need to add the record to the cvterm_dbxref table instead
+    // So we'll need to add the record to the cvterm_dbxref table instead.
     if ($dbxref->accession != $accession) {
 
-      // get/add the dbxref fort his term
+      // Get/add the dbxref for his term.
       $dbxref_new =  tripal_insert_dbxref(array(
         'db_id' => $db->db_id,
         'accession' => $accession
@@ -649,7 +654,7 @@ function tripal_insert_cvterm($term, $options = array()) {
         return 0;
       }
 
-      // check to see if the cvterm_dbxref record already exists
+      // Check to see if the cvterm_dbxref record already exists.
       $values = array(
         'cvterm_id' => $cvterm->cvterm_id,
         'dbxref_id' => $dbxref_new->dbxref_id,
@@ -675,8 +680,8 @@ function tripal_insert_cvterm($term, $options = array()) {
       return $cvterm;
     }
 
-    // continue on, we've fixed the record if the db_id did not match,
-    // we can now perform and updated if we need to.
+    // Continue on, we've fixed the record if the db_id did not match.
+    // We can now perform and updated if we need to.
   }
 
   // get the CVterm record
@@ -684,7 +689,7 @@ function tripal_insert_cvterm($term, $options = array()) {
   $cvterm = $result->fetchObject();
   if (!$cvterm) {
 
-    // check to see if the dbxref exists if not, add it
+    // Check to see if the dbxref exists if not, add it.
     $dbxref =  tripal_insert_dbxref(array(
       'db_id' => $db->db_id,
       'accession' => $accession
@@ -695,8 +700,7 @@ function tripal_insert_cvterm($term, $options = array()) {
       return 0;
     }
 
-    // check to see if the dbxref already has an entry in the cvterm table
-    // this is the second constraint in the cvterm table
+    // Check to see if the dbxref already has an entry in the cvterm table.
     $values = array('dbxref_id' => $dbxref->dbxref_id);
     $check = chado_select_record('cvterm', array('cvterm_id'), $values);
     if (count($check) == 0) {
@@ -721,7 +725,7 @@ function tripal_insert_cvterm($term, $options = array()) {
         }
       }
     }
-    // this dbxref already exists in the cvterm table
+    // This dbxref already exists in the cvterm table.
     else {
       tripal_report_error('tripal_chado', TRIPAL_WARNING, "The dbxref already exists for another cvterm record: $name (cv: " . $cvname . " db: $dbname)", NULL);
       return 0;
@@ -729,8 +733,10 @@ function tripal_insert_cvterm($term, $options = array()) {
     $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
     $cvterm = $result->fetchObject();
   }
-  // upate the cvterm
+  // Update the cvterm.
   elseif ($update) {
+
+    // First, basic update of the term.
     $match = array('cvterm_id' => $cvterm->cvterm_id);
     $upd_values = array(
       'name'                => $name,
@@ -743,7 +749,43 @@ function tripal_insert_cvterm($term, $options = array()) {
       tripal_report_error('tripal_chado', TRIPAL_WARNING, "Failed to update the term: $name", NULL);
       return 0;
     }
-    $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
+
+    // Second, check that the dbxref has not changed and if it has then update it.
+    $checksql = "
+      SELECT cvterm_id
+      FROM {cvterm} CVT
+        INNER JOIN {dbxref} DBX on CVT.dbxref_id = DBX.dbxref_id
+        INNER JOIN {db} DB on DBX.db_id = DB.db_id
+        INNER JOIN {cv} CV on CV.cv_id = CVT.cv_id
+      WHERE DBX.accession = :accession and DB.name = :dbname and CVT.name = :term and CV.name = :cvname
+    ";
+    $check = chado_query($checksql, array(':accession' => $dbxref->accession, ':dbname' => $dbname, ':term' => $name, ':cvname' => $cvname))->fetchObject();
+    if (!$check) {
+
+      // check to see if the dbxref exists if not, add it.
+      $dbxref =  tripal_insert_dbxref(array(
+        'db_id' => $db->db_id,
+        'accession' => $accession
+      ));
+      if (!$dbxref) {
+        tripal_report_error('tripal_chado', TRIPAL_WARNING, "Failed to find or insert the dbxref record for cvterm, " .
+            "$name (id: $accession), for database $dbname", NULL);
+        return 0;
+      }
+
+      $match = array('cvterm_id' => $cvterm->cvterm_id);
+      $upd_values = array(
+        'dbxref_id' => $dbxref->dbxref_id,
+      );
+      $success = chado_update_record('cvterm', $match, $upd_values);
+      if (!$success) {
+        tripal_report_error('tripal_chado', TRIPAL_WARNING, "Failed to update the term $name with new accession $db:$accession", NULL);
+        return 0;
+      }
+    }
+
+    // Finally grab the updated details.
+    $result = chado_query($cvtermsql, array(':accession' => $dbxref->accession, ':name' => $dbname));
     $cvterm = $result->fetchObject();
   }
   else {

+ 91 - 4
tripal_chado/api/tripal_chado.schema.api.inc

@@ -26,9 +26,10 @@
  */
 
 /**
- * Check that any given Chado table exists.  This function
- * is necessary because Drupa's db_table_exists will not
- * look in any other schema but the one were Drupal is installed
+ * Check that any given Chado table exists.
+ *
+ * This function is necessary because Drupal's db_table_exists() function will
+ * not look in any other schema but the one were Drupal is installed
  *
  * @return
  *   TRUE/FALSE depending upon whether it exists
@@ -55,7 +56,89 @@ function chado_table_exists($table) {
   }
   return TRUE;
 }
+/**
+ * Check that any given column in a Chado table exists.
+ *
+ * This function is necessary because Drupal's db_field_exists() will not
+ * look in any other schema but the one were Drupal is installed
+ *
+ * @param $table
+ *   The name of the chado table.
+ * @param $column
+ *   The name of the column in the chado table.
+
+ * @return
+ *   TRUE if the column exists for the table in the chado schema and
+ *   FALSE if it does not.
+ *
+ * @ingroup tripal_chado_schema_api
+ */
+function chado_column_exists($table, $column) {
+  global $databases;
 
+  $default_db = $databases['default']['default']['database'];
+
+  $sql = "
+    SELECT 1
+    FROM information_schema.columns
+    WHERE
+      table_name = :table_name AND
+      column_name = :column_name AND
+      table_schema = :chado AND
+      table_catalog = :default_db
+  ";
+  $args = array(
+    ':table_name' => $table,
+    ':column_name' => $column,
+    ':chado' => tripal_get_schema_name('chado'),
+    ':default_db' => $default_db
+  );
+  $results = db_query($sql, $args);
+  $exists = $results->fetchField();
+  if (!$exists) {
+    return FALSE;
+  }
+  return TRUE;
+}
+
+/**
+ * Check that any given column in a Chado table exists.
+ *
+ * This function is necessary because Drupal's db_field_exists() will not
+ * look in any other schema but the one were Drupal is installed
+ *
+ * @param sequence
+ *   The name of the sequence
+ * @return
+ *   TRUE if the seqeuence exists in the chado schema and FALSE if it does not.
+ *
+ * @ingroup tripal_chado_schema_api
+ */
+function chado_sequence_exists($sequence) {
+  global $databases;
+
+  $default_db = $databases['default']['default']['database'];
+
+  $sql = "
+    SELECT 1
+    FROM information_schema.sequences
+    WHERE
+      sequence_name = :sequence_name AND
+      sequence_schema = :sequence_schema AND
+      sequence_catalog = :sequence_catalog
+  ";
+  $args = array(
+    ':sequence_name' => $sequence,
+    ':sequence_schema' => tripal_get_schema_name('chado'),
+    ':sequence_catalog' => $default_db
+  );
+  $results = db_query($sql, $args);
+  $exists = $results->fetchField();
+  if (!$exists) {
+    return FALSE;
+  }
+  return TRUE;
+}
 /**
  * A Chado-aware replacement for the db_index_exists() function.
  *
@@ -71,7 +154,11 @@ function chado_index_exists($table, $name) {
 
   $default_db = $databases['default']['default']['database'];
 
-  $sql = "SELECT 1 as exists FROM pg_indexes WHERE indexname = :indexname";
+  $sql = "
+    SELECT 1 as exists
+    FROM pg_indexes
+    WHERE indexname = :indexname
+  ";
 
   $result = db_query($sql, array(':indexname' => $indexname));
   $exists = $result->fetchObject();

File diff suppressed because it is too large
+ 245 - 242
tripal_chado/chado_schema/default_schema-1.2-1.3-diff.sql


+ 289 - 76
tripal_chado/includes/tripal_chado.install.inc

@@ -9,7 +9,7 @@
  *
  * @ingroup tripal_chado
  */
-function tripal_chado_install_form() {
+function tripal_chado_load_form($form, $form_state) {
 
   // we want to force the version of Chado to be set properly
   $real_version = chado_get_version(TRUE);
@@ -17,26 +17,66 @@ function tripal_chado_install_form() {
   // get the effective version.  Pass true as second argument
   // to warn the user if the current version is not compatible
   $version = chado_get_version(FALSE, TRUE);
-
-  if ($real_version == '1.2') {
-    drupal_set_message('Please note: the upgrade of Chado from v1.2 to v1.3 may
-        require three fixes to your database. All of the primary keys
-        in Chado were changed from integers to big integers to support larger
-        tables.  First, if your site has custom materialized views that will hold
-        data derived from fields changed to big integers then you may need to
-        alter the views to change the fields from integers to big integers
-        and repopulate those views.  Second, if you have made
-        any custom PL/pgSQL functions that expect primary and foreign key fields
-        to be integers, then those functions will need to be altered to accept
-        big integers.  Third, if you have PostgreSQL Views that use fields
-        that are converted to big integers then most likely this upgrade will
-        fail.  You must first remove those views, perform the upgrade and then
-        recreate them with the appropriate fields change to big integers.
-        The Tripal upgrader is not able to fix these problems automatically',
-        'warning');
+  if (array_key_exists('values', $form_state)) {
+    if ($form_state['values']['action_to_do'] == "Upgrade Chado v1.2 to v1.3") {
+      $tables_list = implode(', ', array('analysis_cvterm', 'analysis_dbxref', 'analysis_pub',
+        'analysis_relationship', 'contactprop', 'dbprop', 'feature_contact',
+        'featuremap_contact', 'featuremap_dbxref', 'featuremap_organism', 'featuremapprop',
+        'featureposprop', 'library_contact', 'library_expression', 'library_expressionprop',
+        'library_featureprop', 'library_relationship', 'library_relationship_pub', 'nd_experiment_analysis',
+        'organism_cvterm', 'organism_cvtermprop', 'organism_pub', 'organism_relationship',
+        'organismprop_pub', 'phenotypeprop', 'phylotreeprop', 'project_analysis',
+        'project_dbxref', 'project_feature', 'project_stock', 'pubauthor_contact',
+        'stock_feature', 'stock_featuremap', 'stock_library', 'stockcollection_db'));
+      $items = array(
+        'PostgreSQL version 9.1 is required to perform this upgrade. If your Tripal
+         site uses an older version please upgrade before proceeding.',
+        'A major change between Chado v1.2 and v1.3 is that primary and foreign
+         keys were upgraded from integers to big integers. If your site has custom
+         materialized views that will hold data derived from fields changed to
+         big integers then you may need to alter the views to change the fields
+         from integers to big integers and repopulate those views. If you have not
+         added any materialized views you can ignore this issue.',
+        'Custom PL/pgSQL functions that expect primary and
+         foreign key fields to be integers will not work after the upgrade.
+         Those functions will need to be altered to accept big integers. If you
+         do not have any custom PL/pgSQL functions you can ignore this issue.',
+        'PostgreSQL Views that use fields that are converted to big
+         integers will cause this upgrade to fail.  You must first remove
+         those views, perform the upgrade and then recreate them with the
+         appropriate fields change to big integers. If you do not have custom
+         PostgreSQL Views you can ignore this issue.',
+        'Several new tables were added to Chado v1.3.  However, some groups have
+         added these tables to their Chado v1.2 installation.  The Tripal upgrader
+         will alter the primary and foreign keys of those tables to be "bigints"
+         if they already exist but will otherwise leave them the same.  You should
+         verify that any tables with Chado v1.3 names correctly match the v1.3 schema.
+         Otherwise you may have problems using Tripal. If you have not added any
+         Chado v1.3 tables to your Chado v1.2 database you can ignore this issue.
+         These are the newly added tables:  ' .
+         $tables_list  . '.'
+      );
+      $list = theme_item_list(array(
+        'items' => $items,
+        'title' => '',
+        'type' => 'ul',
+        'attributes' => array(),
+      ));
+      drupal_set_message('Please note: the upgrade of Chado from v1.2 to v1.3 may
+          require several fixes to your database. Please review the following
+          list to ensure a safe upgrade. The Tripal upgrader is
+          not able to fix these problems automatically: ' . $list, 'warning');
+    }
+    if ($form_state['values']['action_to_do'] == "Install Chado v1.3" or
+        $form_state['values']['action_to_do'] == "Install Chado v1.2" or
+        $form_state['values']['action_to_do'] == "Install Chado v1.11") {
+      drupal_set_message('Please note: if Chado is already installed it will
+          be removed and recreated and all data will be lost. If this is
+          desired or if this is the first time Chado has been installed
+          you can ignore this issue.', 'warning');
+    }
   }
 
-
   $form['current_version'] = array(
     '#type' => 'item',
     '#title' => t("Current installed version of Chado:"),
@@ -44,17 +84,23 @@ function tripal_chado_install_form() {
   );
 
   $form['action_to_do'] = array(
-     '#type' => 'radios',
-     '#title' => 'Installation/Upgrade Action',
-     '#options' => array(
-        'Install Chado v1.3' => t('New Install of Chado v1.3 (erases all existing Chado data if Chado already exists)'),
-        'Upgrade Chado v1.2 to v1.3' => t('Upgrade existing Chado v1.2 to v1.3 (no data is lost)'),
-        'Install Chado v1.2' => t('New Install of Chado v1.2 (erases all existing Chado data if Chado already exists)'),
-        'Upgrade Chado v1.11 to v1.2' => t('Upgrade existing Chado v1.11 to v1.2 (no data is lost)'),
-        'Install Chado v1.11' => t('New Install of Chado v1.11 (erases all existing Chado data if Chado already exists)'),
-     ),
-     '#description' => t('Select an action to perform. If you want to install Chado all other Tripal modules must not be installed.'),
-     '#required' => TRUE,
+    '#type' => 'radios',
+    '#title' => 'Installation/Upgrade Action',
+    '#options' => array(
+      'Install Chado v1.3' => t('New Install of Chado v1.3 (erases all existing Chado data if Chado already exists)'),
+      'Upgrade Chado v1.2 to v1.3' => t('Upgrade existing Chado v1.2 to v1.3 (no data is lost)'),
+      'Install Chado v1.2' => t('New Install of Chado v1.2 (erases all existing Chado data if Chado already exists)'),
+      'Upgrade Chado v1.11 to v1.2' => t('Upgrade existing Chado v1.11 to v1.2 (no data is lost)'),
+      'Install Chado v1.11' => t('New Install of Chado v1.11 (erases all existing Chado data if Chado already exists)'),
+    ),
+    '#description' => t('Select an action to perform. If you want to install Chado all other Tripal modules must not be installed.'),
+    '#required' => TRUE,
+    '#ajax' => array(
+      'callback' => "tripal_chado_load_form_ajax_callback",
+      'wrapper'  => 'tripal_chado_load_form',
+      'effect'   => 'fade',
+      'method'   => 'replace',
+    ),
   );
 
   $form['warning'] = array(
@@ -67,11 +113,49 @@ function tripal_chado_install_form() {
     '#value' => t('Install/Upgrade Chado'),
   );
 
+  $form['#prefix'] = '<div id="tripal_chado_load_form">';
+  $form['#suffix'] = '</div>';
   return $form;
 }
 
-function tripal_chado_install_form_validate($form, &$form_state) {
+/**
+ * Ajax callback function for the gensas_job_view_panel_form.
+ *
+ * @param $form
+ * @param $form_state
+ */
+function tripal_chado_load_form_ajax_callback($form, $form_state) {
+  // drupal_debug($form['tree_settings']['org_table']['num_orgs']);
+
+  return $form;
+}
 
+function tripal_chado_load_form_validate($form, &$form_state) {
+  // We do not want to allow re-installation of Chado if other
+  // Tripal modules are installed.  This is because the install files
+  // of those modules may add content to Chado and reinstalling Chado
+  // removes that content which may break the modules.
+  if ($form_state['values']['action_to_do'] == "Install Chado v1.3" or
+      $form_state['values']['action_to_do'] == "Install Chado v1.2" or
+      $form_state['values']['action_to_do'] == "Install Chado v1.11") {
+
+    $modules = system_get_info('module');
+    // The tripal_views module should not be included as it's a rquired
+    // dependency of tripal_chado
+    unset($modules['tripal_views']);
+    $list = array();
+    foreach ($modules as $mname => $module) {
+      if (array_key_exists('dependencies', $module) and in_array('tripal_chado', $module['dependencies'])) {
+        $list[] = $module['name'] . " ($mname)";
+      }
+    }
+    if (count($list) > 0) {
+      form_set_error("action_to_do", "Chado cannot be installed while other Tripal modules
+          are enabled.  You must fully uninstall the following modules if you
+          would like to install or re-install chado.<br>" .
+          implode("<br>", $list));
+    }
+  }
   if ($form_state['values']['action_to_do'] == "Upgrade Chado v1.11 to v1.2") {
     // Make sure we are already not at v1.2
     $real_version = chado_get_version(TRUE);
@@ -80,7 +164,7 @@ function tripal_chado_install_form_validate($form, &$form_state) {
     }
   }
   if ($form_state['values']['action_to_do'] == "Upgrade Chado v1.2 to v1.3") {
-    // Make sure we are already not at v1.2
+    // Make sure we are already not at v1.3
     $real_version = chado_get_version(TRUE);
     if ($real_version == "1.3") {
       form_set_error("action_to_do", "You are already at v1.3.  There is no need to upgrade.");
@@ -92,13 +176,12 @@ function tripal_chado_install_form_validate($form, &$form_state) {
  *
  * @ingroup tripal_chado
  */
-function tripal_chado_install_form_submit($form, &$form_state) {
+function tripal_chado_load_form_submit($form, &$form_state) {
   global $user;
   $action_to_do = trim($form_state['values']['action_to_do']);
   $args = array($action_to_do);
-  $includes = array(module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.install'));
   tripal_add_job($action_to_do, 'tripal_chado',
-      'tripal_chado_install_chado', $args, $user->uid, 10, $includes);
+      'tripal_chado_install_chado', $args, $user->uid);
 }
 
 /**
@@ -157,16 +240,7 @@ function tripal_chado_install_chado($action) {
   }
   return TRUE;
 
-  // Set a variable to indicate the site is prepared.
-  variable_set('tripal_chado_is_prepared', FALSE);
-
-  module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.setup');
 
-  tripal_chado_set_globals();
-  tripal_chado_init();
-  tripal_chado_prepare_chado();
-
-  return TRUE;
 }
 
 /**
@@ -268,7 +342,14 @@ function tripal_chado_install_chado_1_11() {
  */
 function tripal_chado_upgrade_chado_1_2_to_1_3() {
 
+  // Upgrade some of the custom tables that Tripal created that are now in
+  // Chado v1.3.  We'll do this ahead of time because the upgrade script won't
+  // upgrade tables if they already exist.
+  print "Checking for existing v1.3 tables in v1.2 and fixing bigints...\n";
+  tripal_chado_upgrade_chado_1_2_to_1_3_pre_alter();
+
   // Get the path to the diff schema and upgrade SQL files.
+  print "Incorporating additional changes...\n";
   $diff_file = drupal_get_path('module', 'tripal_chado') .
     '/chado_schema/default_schema-1.2-1.3-diff.sql';
 
@@ -280,6 +361,129 @@ function tripal_chado_upgrade_chado_1_2_to_1_3() {
     throw new Exception("Upgrade problems!  Please check output above for errors.");
   }
 }
+
+/**
+ * Upgrade custom tables that may match the tables now in Chado v1.3.
+ *
+ * There were many new tables that were added to Chado v1.3 that were
+ * suggested by the Chado user community.  Some of those were Tripal users.
+ * Therefore, to help these Tripal users upgrade more seemlessly this function
+ * checks if those custom tables already exists, and if so updates them as
+ * best it can to match.  At a minimum it will create the table if it doesn't
+ * exist and if it does it will change the primary keys and foreign keys to
+ * be big ints.
+ */
+function tripal_chado_upgrade_chado_1_2_to_1_3_pre_alter() {
+
+  // Include the Chado v1.3 schema definitions.
+  module_load_include('inc', 'tripal_chado', '/api/tripal_chado.schema_v1.3.api');
+
+  // The list of new tables in Chado v1.3
+  $new_tables = array('analysis_cvterm', 'analysis_dbxref', 'analysis_pub', 'analysis_relationship',
+    'contactprop', 'dbprop', 'feature_contact', 'featuremap_contact', 'featuremap_dbxref',
+    'featuremap_organism', 'featuremapprop', 'featureposprop', 'library_contact',
+    'library_expression', 'library_expressionprop', 'library_featureprop',
+    'library_relationship', 'library_relationship_pub', 'nd_experiment_analysis',
+    'organism_cvterm', 'organism_cvtermprop', 'organism_pub', 'organism_relationship',
+    'organismprop_pub', 'phenotypeprop', 'phylotreeprop', 'project_analysis',
+    'project_dbxref', 'project_feature', 'project_stock', 'pubauthor_contact',
+    'stock_feature', 'stock_featuremap', 'stock_library', 'stockcollection_db',
+  );
+
+  // Get the name of the chado schema.
+  $chado_schema = tripal_get_schema_name('chado');
+
+  // Iterate through the new Chado tables and create them or if they already
+  // exist then update them.
+  foreach ($new_tables as $table) {
+
+    // Get the schema for this table.
+    $function = 'tripal_chado_schema_v1_3_' . $table;
+    $schema = $function();
+
+    // If the table exists then fix the pkeys and fkeys.
+    if (chado_table_exists($table)) {
+
+      // Update the primary key fields to be bigints.
+      $fields = $schema['fields'];
+      foreach ($fields as $field_name => $field) {
+        if ($field['type'] == 'serial') {
+          if (chado_column_exists($table, $field_name)) {
+            $sql = 'ALTER TABLE {' . $table . '} ALTER COLUMN ' . $field_name . ' TYPE bigint';
+            chado_query($sql);
+          }
+          else {
+            throw new Exception('Could not alter primary key to bigint: ' . $table . '.' . $field_name);
+          }
+        }
+      }
+
+      // Update the foreign key fields to be bigints.
+      $fkeys = $schema['foreign keys'];
+      foreach ($fkeys as $fktable => $details) {
+        foreach ($details['columns'] as $leftkey => $rightkey) {
+          if (chado_column_exists($table, $leftkey)) {
+            $sql = 'ALTER TABLE {' . $table . '} ALTER COLUMN ' . $leftkey . ' TYPE bigint';
+            chado_query($sql);
+          }
+          else {
+            throw new Exception('Could not alter foreign key to bigint: ' . $table . '.' . $leftkey);
+          }
+        }
+      }
+
+    }
+  }
+  // Now create the sequences if they don't already exist.
+  $sequences = array(
+    'analysis_cvterm_analysis_cvterm_id_seq',
+    'analysis_dbxref_analysis_dbxref_id_seq',
+    'analysis_pub_analysis_pub_id_seq',
+    'analysis_relationship_analysis_relationship_id_seq',
+    'contactprop_contactprop_id_seq',
+    'dbprop_dbprop_id_seq',
+    'feature_contact_feature_contact_id_seq',
+    'featuremap_contact_featuremap_contact_id_seq',
+    'featuremap_dbxref_featuremap_dbxref_id_seq',
+    'featuremap_organism_featuremap_organism_id_seq',
+    'featuremapprop_featuremapprop_id_seq',
+    'featureposprop_featureposprop_id_seq',
+    'library_contact_library_contact_id_seq',
+    'library_expression_library_expression_id_seq',
+    'library_expressionprop_library_expressionprop_id_seq',
+    'library_featureprop_library_featureprop_id_seq',
+    'library_relationship_library_relationship_id_seq',
+    'library_relationship_pub_library_relationship_pub_id_seq',
+    'nd_experiment_analysis_nd_experiment_analysis_id_seq',
+    'organism_cvterm_organism_cvterm_id_seq',
+    'organism_cvtermprop_organism_cvtermprop_id_seq',
+    'organism_pub_organism_pub_id_seq',
+    'organism_relationship_organism_relationship_id_seq',
+    'organismprop_pub_organismprop_pub_id_seq',
+    'phenotypeprop_phenotypeprop_id_seq',
+    'phylotreeprop_phylotreeprop_id_seq',
+    'project_analysis_project_analysis_id_seq',
+    'project_dbxref_project_dbxref_id_seq',
+    'project_feature_project_feature_id_seq',
+    'project_stock_project_stock_id_seq',
+    'pubauthor_contact_pubauthor_contact_id_seq',
+    'stock_feature_stock_feature_id_seq',
+    'stock_featuremap_stock_featuremap_id_seq',
+    'stock_library_stock_library_id_seq',
+    'stockcollection_db_stockcollection_db_id_seq'
+  );
+  foreach ($sequences as $sequence) {
+
+    // Now add in the sequences if they don't already exist. There is no
+    // PostgreSQL 'CREATE SEQUENCE IF NOT EXIST' so we're forced to do it here
+    // and these create statements were removed from the diff upgrade file.
+    if (!chado_sequence_exists($sequence)) {
+      $sql = "CREATE SEQUENCE {" . $sequence . "} START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1";
+      chado_query($sql);
+    }
+  }
+}
+
 /**
  * Upgrades Chado from v1.11 to v1.2
  */
@@ -315,6 +519,10 @@ function tripal_chado_upgrade_chado_1_11_to_1_2() {
  */
 function tripal_chado_reset_chado_schema() {
 
+  // determine the schema name.
+  $chado_schema = tripal_get_schema_name('chado');
+  $chado_dot = $chado_schema . '.';
+
   // drop current chado and chado-related schema
   if (chado_dbschema_exists('genetic_code')) {
     print "Dropping existing 'genetic_code' schema\n";
@@ -330,12 +538,12 @@ function tripal_chado_reset_chado_schema() {
   }
   if (chado_dbschema_exists('chado')) {
     print "Dropping existing 'chado' schema\n";
-    db_query("drop schema chado cascade");
+    db_query("drop schema $chado_schema cascade");
   }
 
   // create the new chado schema
   print "Creating 'chado' schema\n";
-  db_query("create schema chado");
+  db_query("create schema $chado_schema");
   if (chado_dbschema_exists('chado')) {
     // before creating the plpgsql language let's check to make sure
     // it doesn't already exists
@@ -363,8 +571,12 @@ function tripal_chado_install_sql($sql_file) {
 
   $chado_local = chado_dbschema_exists('chado');
 
+  // determine the schema name.
+  $chado_schema = tripal_get_schema_name('chado');
+  $chado_dot = $chado_schema . '.';
+
   if ($chado_local) {
-    db_query("set search_path to chado");
+    db_query("set search_path to $chado_schema");
   }
   print "Loading $sql_file...\n";
   $lines = file($sql_file, FILE_SKIP_EMPTY_LINES);
@@ -396,11 +608,11 @@ function tripal_chado_install_sql($sql_file) {
     // Find SQL for new objects
     if (preg_match('/^\s*CREATE\s+TABLE/i', $line) and !$in_string and !$in_function) {
       $stack[] = 'table';
-      $line = preg_replace("/public\./", "chado.", $line);
+      $line = preg_replace("/public\./", $chado_dot, $line);
     }
     if (preg_match('/^\s*ALTER\s+TABLE\s+/i', $line) and !$in_string and !$in_function) {
       $stack[] = 'alter_table';
-      $line = preg_replace("/public\./", "chado.", $line);
+      $line = preg_replace("/public\./", $chado_dot, $line);
     }
     if (preg_match('/^\s*SET/i', $line) and !$in_string and !$in_function) {
       $stack[] = 'set';
@@ -410,27 +622,27 @@ function tripal_chado_install_sql($sql_file) {
     }
     if (preg_match('/^\s*CREATE\s+SEQUENCE/i', $line) and !$in_string and !$in_function) {
       $stack[] = 'sequence';
-      $line = preg_replace("/public\./", "chado.", $line);
+      $line = preg_replace("/public\./", $chado_dot, $line);
     }
     if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i', $line) and !$in_string and !$in_function) {
       $stack[] = 'view';
-      $line = preg_replace("/public\./", "chado.", $line);
+      $line = preg_replace("/public\./", $chado_dot, $line);
     }
     if (preg_match('/^\s*COMMENT/i', $line) and !$in_string and sizeof($stack)==0 and !$in_function) {
       $stack[] = 'comment';
-      $line = preg_replace("/public\./", "chado.", $line);
+      $line = preg_replace("/public\./", $chado_dot, $line);
     }
     if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i', $line) and !$in_string and !$in_function) {
       $in_function = TRUE;
       $stack[] = 'function';
-      $line = preg_replace("/public\./", "chado.", $line);
+      $line = preg_replace("/public\./", $chado_dot, $line);
     }
     if (preg_match('/^\s*CREATE\s+INDEX/i', $line) and !$in_string and !$in_function) {
       $stack[] = 'index';
     }
     if (preg_match('/^\s*INSERT\s+INTO/i', $line) and !$in_string and !$in_function) {
       $stack[] = 'insert';
-      $line = preg_replace("/public\./", "chado.", $line);
+      $line = preg_replace("/public\./", $chado_dot, $line);
     }
     if (preg_match('/^\s*CREATE\s+TYPE/i', $line) and !$in_string and !$in_function) {
       $stack[] = 'type';
@@ -469,25 +681,25 @@ function tripal_chado_install_sql($sql_file) {
     if (strcmp($stack[sizeof($stack)-1], 'table') == 0 and preg_match('/\);\s*$/', $line)) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'alter_table') == 0 and preg_match('/;\s*$/', $line)) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'alter_table') == 0 and preg_match('/;\s*$/', $line)) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'set') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'set') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'schema') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'schema') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'sequence') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'sequence') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'view') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'view') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'comment') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'comment') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'function') == 0) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'function') == 0) {
 
       if(preg_match('/LANGUAGE.*?;\s*$/i', $line)) {
         $type = array_pop($stack);
@@ -502,43 +714,43 @@ function tripal_chado_install_sql($sql_file) {
       else if(preg_match('/\$\$;\s*$/i', $line)) {
         $type = array_pop($stack);
         $in_function = FALSE;
-        // print "FUNCTION DONE ($i): $line";
+       // print "FUNCTION DONE ($i): $line";
       }
       else {
-        // print "FUNCTION ($i): $line";
+       // print "FUNCTION ($i): $line";
       }
     }
-    if (strcmp($stack[sizeof($stack)-1], 'index') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'index') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'insert') == 0 and preg_match('/\);\s*$/', $line)) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'insert') == 0 and preg_match('/\);\s*$/', $line)) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'type') == 0 and preg_match('/\);\s*$/', $line)) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'type') == 0 and preg_match('/\);\s*$/', $line)) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'grant') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'grant') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'aggregate') == 0 and preg_match('/\);\s*$/', $line)) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'aggregate') == 0 and preg_match('/\);\s*$/', $line)) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'drop_function') == 0 and preg_match('/;\s*$/i', $line)) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'drop_function') == 0 and preg_match('/;\s*$/i', $line)) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'drop_view') == 0 and preg_match('/;\s*$/i', $line)) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'drop_view') == 0 and preg_match('/;\s*$/i', $line)) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'drop_index') == 0 and preg_match("/;\s*$/i", $line)) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'drop_index') == 0 and preg_match("/;\s*$/i", $line)) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'drop_seq') == 0 and preg_match("/;\s*$/i", $line)) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'drop_seq') == 0 and preg_match("/;\s*$/i", $line)) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'alter_type') == 0 and preg_match('/;\s*$/i', $line)) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'alter_type') == 0 and preg_match('/;\s*$/i', $line)) {
       $type = array_pop($stack);
     }
-    if (strcmp($stack[sizeof($stack)-1], 'alter_seq') == 0 and preg_match('/;\s*$/i', $line)) {
+    elseif (strcmp($stack[sizeof($stack)-1], 'alter_seq') == 0 and preg_match('/;\s*$/i', $line)) {
       $type = array_pop($stack);
     }
     // if we're in a recognized SQL statement then let's keep track of lines
@@ -556,7 +768,7 @@ function tripal_chado_install_sql($sql_file) {
       // rewrite the set search_path to make 'public' be 'chado', but only if the
       // chado schema exists
       if (strcmp($type, 'set') == 0 and $chado_local) {
-        $query = preg_replace("/public/m", "chado", $query);
+        $query = preg_replace("/public/m", $chado_schema, $query);
       }
 
       // execute the statement
@@ -586,5 +798,6 @@ function tripal_chado_install_sql($sql_file) {
  */
 function tripal_chado_install_done() {
 
-  db_query("set search_path to default");
+  $drupal_schema = tripal_get_schema_name('drupal');
+  db_query("set search_path to $drupal_schema");
 }

+ 4 - 4
tripal_chado/tripal_chado.module

@@ -117,18 +117,18 @@ function tripal_chado_menu() {
     'access arguments' => array('administer tripal'),
   );
 
-  $items['admin/tripal/storage/chado/chado_install'] = array(
+  $items['admin/tripal/storage/chado/install'] = array(
     'title' => 'Install Chado',
     'description' => t('Installs the Chado database tables, views, etc., inside the current Drupal database'),
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('tripal_chado_install_form'),
+    'page arguments' => array('tripal_chado_load_form'),
     'type' => MENU_NORMAL_ITEM,
     'access arguments' => array('install chado'),
     'file' => 'includes/tripal_chado.install.inc',
     'file path' => drupal_get_path('module', 'tripal_chado'),
     'weight' => -100
   );
-  $items['admin/tripal/storage/chado/chado_prepare'] = array(
+  $items['admin/tripal/storage/chado/prepare'] = array(
     'title' => 'Prepare Chado',
     'description' => t('Prepares Drupal to use Chado.'),
     'page callback' => 'drupal_get_form',
@@ -476,7 +476,7 @@ function tripal_chado_menu() {
     'file path' => drupal_get_path('module', 'tripal_chado'),
     'weight' => 10
   );
-  
+
   //////////////////////////////////////////////////////////////////////////////
   //                           Semantic Web Settings
   //////////////////////////////////////////////////////////////////////////////

Some files were not shown because too many files changed in this diff