Browse Source

Fixed bug in chado_set_active that was cuasing the search path to not get reset

Stephen Ficklin 8 years ago
parent
commit
c475797679

+ 1 - 2
legacy/tripal_analysis/includes/tripal_analysis.delete.inc

@@ -88,7 +88,6 @@ function tripal_analysis_delete_analyses($analyses, $job = NULL) {
   // function which is not prefix with the schema, and an error occurs.
   // Therefore, we set the active database to chado to get around that
   // problem.
-//  $previous_db = chado_set_active('chado');
 
   // begin the transaction
   $transaction = db_transaction();
@@ -114,6 +113,6 @@ function tripal_analysis_delete_analyses($analyses, $job = NULL) {
     watchdog_exception('tripal_analysis', $e);
     return 0;
   }
-//  chado_set_active($previous_db);
+
   print "\nDone\n";
 }

+ 0 - 2
legacy/tripal_organism/includes/tripal_organism.delete.inc

@@ -87,7 +87,6 @@ function tripal_organism_delete_organisms($organisms, $job = NULL) {
   // function which is not prefix with the schema, and an error occurs.
   // Therefore, we set the active database to chado to get around that
   // problem.
-//  $previous_db = chado_set_active('chado');
 
   // begin the transaction
   $transaction = db_transaction();
@@ -113,6 +112,5 @@ function tripal_organism_delete_organisms($organisms, $job = NULL) {
     watchdog_exception('tripal_organism', $e);
     return 0;
   }
-//  chado_set_active($previous_db);
   print "\nDone\n";
 }

+ 0 - 1
tripal/tripal.module

@@ -50,7 +50,6 @@ function tripal_views_api() {
 
 /**
  * Implements hook_init().
- * Used to set the search_path, create default content and set default variables.
  *
  * @ingroup tripal
  */

+ 2 - 1
tripal_chado/api/modules/tripal_chado.cv.api.inc

@@ -347,6 +347,8 @@ function tripal_update_cvtermpath($cv_id, $job_id = NULL) {
 
   print "\nUpdating cvtermpath for $cv->name...\n";
 
+  // We need to set the chado schema as active because some of the
+  // functions call other functions which would not be in scope.
   $previous = chado_set_active('chado');
   try {
     $sql = "SELECT * FROM fill_cvtermpath(:name)";
@@ -359,7 +361,6 @@ function tripal_update_cvtermpath($cv_id, $job_id = NULL) {
     tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not fill cvtermpath table: @error", array('@error' => $error));
     return FALSE;
   }
-
   return TRUE;
 }
 

+ 1 - 1
tripal_chado/api/tripal_chado.custom_tables.api.inc

@@ -122,7 +122,7 @@ function chado_edit_custom_table($table_id, $table_name, $schema, $skip_if_exist
  *
  * @ingroup tripal_custom_tables_api
  */
-function chado_create_custom_table($table, $schema, $skip_if_exists = 1, $mview_id = NULL) {
+function chado_create_custom_table($table, $schema, $skip_if_exists = TRUE, $mview_id = NULL) {
   global $databases;
   $created = 0;
   $recreated = 0;

+ 2 - 81
tripal_chado/api/tripal_chado.mviews.api.inc

@@ -14,83 +14,6 @@
  * @}
  */
 
-/**
- * Add a materialized view to the chado database to help speed data access. This
- * function supports the older style where postgres column specifications
- * are provided using the $mv_table, $mv_specs and $indexed variables. It also
- * supports the newer preferred method where the materialized view is described
- * using the Drupal Schema API array.
- *
- * @param $name
- *   The name of the materialized view.
- * @param $modulename
- *   The name of the module submitting the materialized view (e.g. 'tripal_library')
- * @param $mv_table
- *   The name of the table to add to chado. This is the table that can be queried.
- * @param $mv_specs
- *   The table definition
- * @param $indexed
- *   The columns that are to be indexed
- * @param $query
- *   The SQL query that loads the materialized view with data
- * @param $special_index
- *   currently not used
- * @param $comment
- *   A string containing a description of the materialized view
- *
- * @ingroup tripal_mviews_api
- *
-function tripal_add_legacy_mview($name, $modulename, $mv_table, $mv_specs, $indexed,
-  $query, $special_index, $comment =OR NULL) {
-
-  // Create a new record
-  $record = new stdClass();
-  $record->name = $name;
-  $record->modulename = $modulename;
-  $record->mv_table = $mv_table;
-  $record->mv_specs = $mv_specs;
-  $record->indexed = $indexed;
-  $record->query = $query;
-  $record->special_index = $special_index;
-  $record->comment = $comment;
-
-  // add the record to the tripal_mviews table and if successful
-  // create the new materialized view in the chado schema
-  if (drupal_write_record('tripal_mviews', $record)) {
-
-    // drop the table from chado if it exists
-    if (chado_table_exists($mv_table)) {
-      $sql = "DROP TABLE {$mv_table}";
-      chado_query($sql);
-    }
-
-    // now construct the indexes
-    $index = '';
-    if ($indexed) {
-      // add to the array of values
-      $vals = preg_split("/[\n,]+/", $indexed);
-      $index = '';
-      foreach ($vals as $field) {
-        $field = trim($field);
-        $index .= "CREATE INDEX idx_${mv_table}_${field} ON $mv_table ($field);";
-      }
-    }
-  }
-
-  // add the table to the database
-  $sql = "CREATE TABLE {$mv_table} ($mv_specs); $index";
-  $previous_db = chado_set_active('chado');  // use chado database
-  $results = db_query($sql);
-  chado_set_active($previous_db);  // now use drupal database
-  if ($results) {
-    drupal_set_message(t("Materialized view '%name' created", array('%name' => $name)));
-  }
-  else {
-    drupal_set_message(t("Failed to create the materialized view table: '%mv_table'", array('%mv_table' => $mv_table)), 'error');
-  }
-}
-*/
-
 /**
  * Add a materialized view to the chado database to help speed data access. This
  * function supports the older style where postgres column specifications
@@ -432,10 +355,8 @@ function tripal_populate_mview($mview_id) {
     // that may leave parts of the site unfunctional
     $transaction = db_transaction();
     try {
-      $previous_db = chado_set_active('chado');  // use chado database
-      $success = db_query("DELETE FROM {" . $mview->mv_table . "}");
-      $success = db_query("INSERT INTO {" . $mview->mv_table . "} ($mview->query)");
-      chado_set_active($previous_db);  // now use drupal database
+      $success = chado_query("DELETE FROM {" . $mview->mv_table . "}");
+      $success = chado_query("INSERT INTO {" . $mview->mv_table . "} ($mview->query)");
       // if success get the number of results and update the table record
       if ($success) {
         $sql = "SELECT count(*) as cnt FROM {" . $mview->mv_table . "}";

+ 17 - 6
tripal_chado/api/tripal_chado.query.api.inc

@@ -113,14 +113,18 @@ function chado_set_active($dbname = 'default') {
     $GLOBALS['chado_active_db'] = 'default';
   }
 
-  $previous_db = $active_db = $GLOBALS['chado_active_db'];
-  $search_path = 'public';
+  $previous_db = $GLOBALS['chado_active_db'];
+
 
   // Change only if 'chado' has been specified.
   if ($dbname == 'chado') {
     $active_db = 'chado';
     $search_path = 'chado,public';
   }
+  else {
+    $active_db = $dbname;
+    $search_path = 'public';
+  }
 
   $settings = array(
     'dbname'          => $dbname,
@@ -132,10 +136,10 @@ function chado_set_active($dbname = 'default') {
   // note: hooks can alter $active_db and $search_path.
   drupal_alter('chado_connection', $settings);
 
-  // set chado_active_db to remember active db
+  // Set chado_active_db to remember active db.
   $GLOBALS['chado_active_db'] = $active_db;
 
-  // set PostgreSQL search_path
+  // Set PostgreSQL search_path.
   db_query('SET search_path TO ' . $search_path);
 
   return $previous_db;
@@ -1633,8 +1637,15 @@ function chado_query($sql, $args = array()) {
     // must automaticaly have the chado schema set as active to find
     if (preg_match('/chado.featureloc/i', $sql) or preg_match('/chado.feature/i', $sql)) {
       $previous_db = chado_set_active('chado') ;
-      $results = db_query($sql, $args);
-      chado_set_active($previous_db);
+      try {
+        $results = db_query($sql, $args);
+        chado_set_active($previous_db);
+      }
+      catch (Exception $e) {
+        chado_set_active($previous_db);
+        throw $e;
+      }
+
     }
     // for all other tables we should have everything in scope so just run the query
     else {

+ 1 - 1
tripal_chado/api/tripal_chado.schema.api.inc

@@ -438,7 +438,7 @@ function chado_get_version($exact = FALSE, $warn_if_unsupported = FALSE) {
 function chado_get_table_names($include_custom = NULL) {
 
   // first get the chado version that is installed
-  $v = $GLOBALS["chado_version"];
+  $v = array_key_exists('chado_version', $GLOBALS) ? $GLOBALS["chado_version"] : '';
 
   $tables = array();
   if ($v == '1.3') {

+ 3 - 0
tripal_chado/includes/tripal_chado.fields.inc

@@ -347,6 +347,9 @@ function tripal_chado_bundle_create_fields_custom(&$info, $details, $entity_type
       'type' => 'image',
       'cardinality' => 1,
       'locked' => FALSE,
+      'storage' => array(
+        'type' => 'field_sql_storage',
+      ),
     );
   }
 }

+ 56 - 56
tripal_chado/includes/tripal_chado.setup.inc

@@ -126,6 +126,12 @@ function tripal_chado_prepare_chado() {
 
   try {
 
+    // We want to provide a set of commonly used entity types by default. This
+    // way when a user first installs Tripal there are some commonly used
+    // formats.
+    module_load_include('inc', 'tripal', 'api/tripal.api');
+    module_load_include('inc', 'tripal', 'includes/tripal.admin');
+
     // 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, FALSE);
@@ -148,64 +154,15 @@ function tripal_chado_prepare_chado() {
     // add new content types it simplifies the form for them.
     tripal_chado_map_cvterms();
 
-    // Add the cv_root_mview.
-    tripal_chado_add_cv_root_mview_mview();
-
-    // Support for Analyses
-    // we may need the analysisfeatureprop table if it doesn't already exist
-    tripal_chado_add_analysisfeatureprop_table();
-
-    // add materialized views
-    tripal_chado_add_analysis_organism_mview();
-
-    // Support for Contacts
-    // Add the contactprop table to Chado.
-    tripal_chado_add_contactprop_table();
-
-    // Support for Features
-    // Add the materialized view.
-    tripal_chado_add_organism_feature_count_mview();
-
-    // Add the custom tables for the GFF loader.
-    tripal_chado_add_tripal_gff_temp_table();
-    tripal_chado_add_tripal_gffcds_temp_table();
-    tripal_chado_add_tripal_gffprotein_temp_table();
-
-    // Support for FeatureMaps.
-    // add the featuremapprop table to Chado
-    tripal_chado_add_featuremap_dbxref_table();
-    tripal_chado_add_featuremapprop_table();
-    //tripal_chado_add_featuremappos_table();
-
-    // Support for Libraries.
-    // add the materialized view
-    tripal_chado_add_library_feature_count_mview();
-
-    // Support for Pubs.
-    // add the custom tables
-    tripal_chado_add_pubauthor_contact_table();
-    // Add the supported loaders
-    variable_set('tripal_pub_supported_dbs', array('PMID', 'AGL'));
-
-    // Support for Stocks.
-    // add the materialized view
-    tripal_chado_add_organism_stock_count_mview();
-
-    // We want to provide a set of commonly used entity types by default. This
-    // way when a user first installs Tripal there are some commonly used
-    // formats.
-    module_load_include('inc', 'tripal', 'api/tripal.api');
-    module_load_include('inc', 'tripal', 'includes/tripal.admin');
-
     // Create the 'Organism' entity type. This uses the obi:organism term.
     $error = '';
     $args = array(
-     'vocabulary' => 'OBI',
-     'accession' => '0100026',
-     'term_name' => 'organism',
-     'storage_args' => array(
-       'data_table' => 'organism',
-     )
+      'vocabulary' => 'OBI',
+      'accession' => '0100026',
+      'term_name' => 'organism',
+      'storage_args' => array(
+        'data_table' => 'organism',
+      )
     );
     $term = tripal_load_term_entity(array('vocabulary' => 'OBI', 'accession' => '0100026'));
     $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
@@ -325,6 +282,49 @@ function tripal_chado_prepare_chado() {
       }
     }
 
+    // Add the cv_root_mview.
+    tripal_chado_add_cv_root_mview_mview();
+
+    // Support for Analyses
+    // we may need the analysisfeatureprop table if it doesn't already exist
+    tripal_chado_add_analysisfeatureprop_table();
+
+    // add materialized views
+    tripal_chado_add_analysis_organism_mview();
+
+    // Support for Contacts
+    // Add the contactprop table to Chado.
+    tripal_chado_add_contactprop_table();
+
+    // Support for Features
+    // Add the materialized view.
+    tripal_chado_add_organism_feature_count_mview();
+
+    // Add the custom tables for the GFF loader.
+    tripal_chado_add_tripal_gff_temp_table();
+    tripal_chado_add_tripal_gffcds_temp_table();
+    tripal_chado_add_tripal_gffprotein_temp_table();
+
+    // Support for FeatureMaps.
+    // add the featuremapprop table to Chado
+    tripal_chado_add_featuremap_dbxref_table();
+    tripal_chado_add_featuremapprop_table();
+    //tripal_chado_add_featuremappos_table();
+
+    // Support for Libraries.
+    // add the materialized view
+    tripal_chado_add_library_feature_count_mview();
+
+    // Support for Pubs.
+    // add the custom tables
+    tripal_chado_add_pubauthor_contact_table();
+    // Add the supported loaders
+    variable_set('tripal_pub_supported_dbs', array('PMID', 'AGL'));
+
+    // Support for Stocks.
+    // add the materialized view
+    tripal_chado_add_organism_stock_count_mview();
+
     // Set a variable to indicate the site is prepared.
     variable_set('tripal_chado_is_prepared', TRUE);
   }
@@ -1094,7 +1094,7 @@ function tripal_chado_add_tripal_obo_temp_table() {
   // do not want to use the Tripal Custom Table API because we don't want it to appear in the
   // list of custom tables.  It needs to be available for the Tripal Chado API so we create it
   // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
-  if (!db_table_exists('chado.tripal_obo_temp')) {
+  if (!chado_table_exists('tripal_obo_temp')) {
     $sql = "
       CREATE TABLE {tripal_obo_temp} (
         id character varying(255) NOT NULL,

+ 0 - 1
tripal_chado/tripal_chado.module

@@ -71,7 +71,6 @@ function tripal_chado_set_globals() {
 
 /**
  * Implements hook_init().
- * Used to set the search_path, create default content and set default variables.
  *
  * @ingroup tripal_chado
  */