Procházet zdrojové kódy

Fixes to mviews and custom table API/includes. Still some more fixes to go

Stephen Ficklin před 11 roky
rodič
revize
bb7484a963

+ 35 - 1
tripal_analysis/tripal_analysis.install

@@ -216,7 +216,7 @@ function tripal_analysis_schema() {
 }
 
 /**
- * Creates a view showing hte link between an organism & it's analysis through associated features.
+ * Creates a view showing the link between an organism & it's analysis through associated features.
  *
  * @ingroup tripal_analysis
  */
@@ -348,4 +348,38 @@ function tripal_analysis_update_dependencies() {
   );
 
   return $dependencies;
+}
+
+/**
+ * Fixes an error with the materialized view installation
+ *
+ */
+function tripal_analysis_update_7201() {
+  
+  // there is a bug in the Tripal v2.0-alpha release that didn't add the
+  // materialized view schema to the mviews table.  
+  // get the schema for the materialized view from the custom_tables table
+  // as there is a copy there, but only if the schema is missing from the
+  // materialized view table
+  $view_name = 'analysis_organism';
+  $schema = db_select('tripal_mviews', 'tm')
+    ->fields('tm', array('mv_schema'))
+    ->condition('name', $view_name)
+    ->execute()
+    ->fetchField();
+  if (!$schema or $schema == 'Array') {
+    $schema = db_select('tripal_custom_tables', 'tct')
+      ->fields('tct', array('schema'))
+      ->condition('table_name', $view_name)
+      ->execute()
+      ->fetchField();
+    $schema_str = var_export(unserialize($schema), TRUE);
+    $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
+    db_update('tripal_mviews')
+      ->fields(array(
+        'mv_schema' => $schema_str
+      ))
+      ->condition('name', $view_name)
+      ->execute();
+  }
 }

+ 11 - 7
tripal_core/api/tripal_core.mviews.api.inc

@@ -115,13 +115,13 @@ function tripal_add_legacy_mview($name, $modulename, $mv_table, $mv_specs, $inde
  */
 function tripal_add_mview($name, $modulename, $mv_schema, $query, $comment = NULL) {
 
-  $mv_table = $mv_schema['table'];
-
-  if (!$mv_table) {
+  if (!array_key_exists('table', $mv_schema)) {
      tripal_report_error('tripal_core', TRIPAL_ERROR,
        'Must have a table name when creating an mview.', array());
      return NULL;
   }
+  
+  $mv_table = $mv_schema['table'];
 
   // see if the mv_table name already exsists
   $mview_id = db_query(
@@ -137,7 +137,11 @@ function tripal_add_mview($name, $modulename, $mv_schema, $query, $comment = NUL
     $record->mv_table = $mv_table;
     $record->query = $query;
     $record->comment = $comment;
-    $record->mv_schema = $mv_schema;
+    
+    // convert the schema into a string format
+    $str_schema = var_export($mv_schema, TRUE);
+    $str_schema = preg_replace('/=>\s+\n\s+array/', '=> array', $str_schema);
+    $record->mv_schema = $str_schema;
 
     // add the record to the tripal_mviews table and if successful
     // create the new materialized view in the chado schema
@@ -149,11 +153,11 @@ function tripal_add_mview($name, $modulename, $mv_schema, $query, $comment = NUL
         chado_query($sql);
       }
       // create the table
-      if (!chado_create_custom_table ($mv_table, $mv_schema, 0)) {
+      if (!chado_create_custom_table($mv_table, $mv_schema, 0)) {
         drupal_set_message(t("Could not create the materialized view. Check Drupal error report logs."), 'error');
       }
       else {
-        drupal_set_message(t("View '%name' created", array('%name' => $name)));
+        drupal_set_message(t("Materialized view '%name' created", array('%name' => $name)));
       }
     }
   }
@@ -166,7 +170,7 @@ function tripal_add_mview($name, $modulename, $mv_schema, $query, $comment = NUL
 }
 
 /**
- * Edits a materialized view to the chado database to help speed data access.This
+ * Edits 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

+ 1 - 1
tripal_core/includes/tripal_core.custom_tables.inc

@@ -370,7 +370,7 @@ function tripal_custom_tables_form_submit($form, &$form_state) {
      $skip_creation = 0;
   }
 
-  // conver the schema into a PHP array
+  // convert the schema into a PHP array
   $schema_arr = array();
   eval("\$schema_arr = $schema;");
 

+ 102 - 40
tripal_core/includes/tripal_core.mviews.inc

@@ -230,21 +230,20 @@ function tripal_mviews_form($form, &$form_state = NULL, $mview_id = NULL) {
   // get this requested view
   if (strcmp($action, 'Edit') == 0 ) {
     $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id ";
-    $results = db_query($sql, array(':mview_id' => $mview_id));
-    $mview = $results->fetchObject();
+    $mview = db_query($sql, array(':mview_id' => $mview_id))->fetchObject();
 
     // set the default values.  If there is a value set in the
     // form_state then let's use that, otherwise, we'll pull
     // the values from the database
     if (array_key_exists('values', $form_state)) {
-      $default_name = $form_state['values']['name'];
-      $default_mv_table = $form_state['values']['mv_table'];
-      $default_mv_specs = $form_state['values']['mv_specs'];
-      $default_indexed = $form_state['values']['indexed'];
-      $default_mvquery = $form_state['values']['mvquery'];
+      $default_name          = $form_state['values']['name'];
+      $default_mv_table      = $form_state['values']['mv_table'];
+      $default_mv_specs      = $form_state['values']['mv_specs'];
+      $default_indexed       = $form_state['values']['indexed'];
+      $default_mvquery       = $form_state['values']['mvquery'];
       $default_special_index = $form_state['values']['special_index'];
-      $default_comment = $form_state['values']['comment'];
-      $default_modulename = $form_state['values']['modulename'];
+      $default_comment       = $form_state['values']['comment'];
+      $default_modulename    = $form_state['values']['modulename'];
     }
 
     if (!$default_name) {
@@ -341,10 +340,9 @@ function tripal_mviews_form($form, &$form_state = NULL, $mview_id = NULL) {
   // add a fieldset for the Drupal Schema API
   $form['schema'] = array(
     '#type' => 'fieldset',
-    '#title' => 'Drupal Schema API Setup',
-    '#description' => t('Use the Drupal Schema API array to describe a table. The benefit is that it ' .
-                       'can be fully integrated with Tripal Views.  Tripal supports an extended ' .
-                       'array format to allow for descriptoin of foreign key relationships.'),
+    '#title' => 'Table Schema',
+    '#description' => t('Use a ' . l("Drupal Schema API", "https://api.drupal.org/api/drupal/includes!database!schema.inc/group/schemaapi/7", array('attributes' => array('target' => '_blank'))) . 
+        ' array to describe the table. See the bottom of this page for an example.'),
     '#collapsible' => 1,
     '#collapsed' => $schema_collapsed ,
   );
@@ -352,7 +350,8 @@ function tripal_mviews_form($form, &$form_state = NULL, $mview_id = NULL) {
   $form['schema']['schema']= array(
     '#type'          => 'textarea',
     '#title'         => t('Schema Array'),
-    '#description'   => t('Please enter the Drupal Schema API compatible array that defines the table.'),
+    '#description'   => t('Please enter the ' . l("Drupal Schema API", "https://api.drupal.org/api/drupal/includes!database!schema.inc/group/schemaapi/7", array('attributes' => array('target' => '_blank'))) . 
+        ' compatible array that defines the table. There must also be a "table" key with the name of the table as the value. See the example at the bottom of this page.'),
     '#required'      => FALSE,
     '#default_value' => $default_schema,
     '#rows'          => 25,
@@ -431,11 +430,58 @@ function tripal_mviews_form($form, &$form_state = NULL, $mview_id = NULL) {
   $form['submit'] = array(
     '#type'         => 'submit',
     '#value'        => t($value),
-    '#weight'       => 9,
     '#executes_submit_callback' => TRUE,
   );
   $form['#redirect'] = 'admin/tripal/schema/mviews';
-
+  
+  $form['example']= array(
+    '#type'          => 'item',
+    '#description'         => "<br>An example Schema API definition for a materialized view: <pre>
+array (
+  'description' => 'Stores the type and number of features per organism',
+  'table' => 'organism_feature_count',
+  'fields' => array (
+    'organism_id' => array (
+      'type' => 'int',
+      'not null' => true,
+    ),
+    'genus' => array (
+      'type' => 'varchar',
+      'length' => '255',
+      'not null' => true,
+    ),
+    'species' => array (
+      'type' => 'varchar',
+      'length' => '255',
+      'not null' => true,
+    ),
+    'common_name' => array (
+      'type' => 'varchar',
+      'length' => '255',
+      'not null' => false,
+    ),
+    'num_features' => array (
+      'type' => 'int',
+      'not null' => true,
+    ),
+    'cvterm_id' => array (
+      'type' => 'int',
+      'not null' => true,
+    ),
+    'feature_type' => array (
+      'type' => 'varchar',
+      'length' => '255',
+      'not null' => true,
+    ),
+  ),
+  'indexes' => array (
+    'organism_feature_count_idx1' => array ('organism_id'),
+    'organism_feature_count_idx2' => array ('cvterm_id'),
+    'organism_feature_count_idx3' => array ('feature_type'),
+  ),
+)
+</pre>"
+  );
   return $form;
 }
 
@@ -446,15 +492,20 @@ function tripal_mviews_form($form, &$form_state = NULL, $mview_id = NULL) {
  * @ingroup tripal_mviews
  */
 function tripal_mviews_form_validate($form, &$form_state) {
-  $action = $form_state['values']['action'];
-  $mview_id = $form_state['values']['mview_id'];
-  $name = $form_state['values']['name'];
+  
+  
+  $action    = $form_state['values']['action'];
+  $mview_id  = $form_state['values']['mview_id'];
+  $name      = $form_state['values']['name'];
   $is_legacy = $form_state['values']['is_legacy'];
-  $query = $form_state['values']['mvquery'];
+  $query     = $form_state['values']['mvquery'];
+  
+  // if this is a legacy materialized view (no longer supported in Tripal v2.0
+  // but code left just in case)
   if ($is_legacy) {
     $mv_table = $form_state['values']['mv_table'];
     $mv_specs = $form_state['values']['mv_specs'];
-    $indexed = $form_state['values']['indexed'];
+    $indexed  = $form_state['values']['indexed'];
     $special_index = '';//$form_state['values']['special_index'];
   }
   else {
@@ -464,13 +515,18 @@ function tripal_mviews_form_validate($form, &$form_state) {
     $special_index = '';
   }
   $comment = $form_state['values']['comment'];
-  $schema = $form_state['values']['schema'];
+  $schema  = $form_state['values']['schema'];
 
+  // if both the schema and the older fields for the legacy view are populated then
+  // this is an error and we need to let the user know.
   if ($schema and ($mv_table or $mv_specs or $indexed or $special_index)) {
     form_set_error($form_state['values']['schema'],
       t('You can create an MView using the Drupal Schema API method or the ' .
         'traditional method but not both.'));
   }
+  
+  // if we don't have a schema and are missing fields for the legacy views then
+  // inform the user.
   if (!$schema) {
     if (!$mv_specs) {
       form_set_error($form_state['values']['mv_specs'],
@@ -494,7 +550,6 @@ function tripal_mviews_form_validate($form, &$form_state) {
       form_set_error($form_state['values']['schema'],
         t("The schema array must have key named 'table'"));
     }
-
     // TODO: add in more validation checks of the array to help the user
   }
 }
@@ -508,37 +563,44 @@ function tripal_mviews_form_validate($form, &$form_state) {
 function tripal_mviews_form_submit($form, &$form_state) {
 
   $ret = array();
-  $action = $form_state['values']['action'];
-  $mview_id = $form_state['values']['mview_id'];
-  $name = $form_state['values']['name'];
-  $is_legacy = $form_state['values']['is_legacy'];
-  $query = $form_state['values']['mvquery'];
+  
+  $action     = $form_state['values']['action'];
+  $mview_id   = $form_state['values']['mview_id'];
+  $name       = $form_state['values']['name'];
+  $is_legacy  = $form_state['values']['is_legacy'];
+  $query      = $form_state['values']['mvquery'];
+  $comment    = $form_state['values']['comment'];
+  $schema     = $form_state['values']['schema'];
+  $modulename = $form_state['values']['modulename'];
+  $mv_table = '';
+  $mv_specs = '';
+  $indexed = '';
+  $special_index = '';
+  
+  // if this is a legacy materialized view (no longer supported in Tripal v2.0
+  // but code left just in case)
   if ($is_legacy) {
     $mv_table = $form_state['values']['mv_table'];
     $mv_specs = $form_state['values']['mv_specs'];
-    $indexed = $form_state['values']['indexed'];
+    $indexed  = $form_state['values']['indexed'];
     $special_index = '';//$form_state['values']['special_index'];
   }
-  else {
-    $mv_table = '';
-    $mv_specs = '';
-    $indexed = '';
-    $special_index = '';
-  }
-  $comment = $form_state['values']['comment'];
-  $schema = $form_state['values']['schema'];
-  $modulename = $form_state['values']['modulename'];
+
   if (!$modulename) {
     $modulename = 'tripal_core';
   }
 
+  // if this is an edit action
   if (strcmp($action, 'Edit') == 0) {
     tripal_edit_mview($mview_id, $name, $modulename, $mv_table, $mv_specs,
       $indexed, $query, $special_index, $comment, $schema);
   }
+  // else an add action
   elseif (strcmp($action, 'Add') == 0) {
-    tripal_add_mview($name, $modulename, $mv_table, $mv_specs,
-      $indexed, $query, $special_index, $comment, $schema);
+    // convert the schema into a PHP array
+    $schema_arr = array();
+    eval("\$schema_arr = $schema;");
+    tripal_add_mview($name, $modulename, $schema_arr, $query, $comment);
   }
   else {
     drupal_set_message(t("No action performed."));

+ 34 - 0
tripal_cv/tripal_cv.install

@@ -271,4 +271,38 @@ function tripal_cv_update_7200() {
     $error = $e->getMessage();
     throw new DrupalUpdateException('Failed to create tripal_cv_defaults table: '. $error);
   }
+}
+
+/**
+ * Fixes an error with the materialized view installation
+ *
+ */
+function tripal_cv_update_7201() {
+
+  // there is a bug in the Tripal v2.0-alpha release that didn't add the
+  // materialized view schema to the mviews table.
+  // get the schema for the materialized view from the custom_tables table
+  // as there is a copy there, but only if the schema is missing from the
+  // materialized view table
+  $view_name = 'cv_root_mview';
+  $schema = db_select('tripal_mviews', 'tm')
+    ->fields('tm', array('mv_schema'))
+    ->condition('name', $view_name)
+    ->execute()
+    ->fetchField();
+  if (!$schema or $schema == 'Array') {
+    $schema = db_select('tripal_custom_tables', 'tct')
+      ->fields('tct', array('schema'))
+      ->condition('table_name', $view_name)
+      ->execute()
+      ->fetchField();
+    $schema_str = var_export(unserialize($schema), TRUE);
+    $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
+    db_update('tripal_mviews')
+      ->fields(array(
+        'mv_schema' => $schema_str
+      ))
+      ->condition('name', $view_name)
+      ->execute();
+  }
 }

+ 40 - 1
tripal_feature/tripal_feature.install

@@ -232,7 +232,12 @@ function tripal_feature_add_cvs() {
     'Contains types of relationships between features.'
   );
   
-  // the feature_property CV already exists... it comes with chado, so no need to add it here
+  // the feature_property CV already exists... it comes with chado, but we need to
+  // add it just in case it doesn't get added before the feature module is installed
+  tripal_cv_add_cv(
+    'feature_property',
+    'Stores properties about features'
+  );
   
   // the feature type vocabulary should be the sequence ontology, and even though
   // this ontology should get loaded we will create it here just so that we can 
@@ -367,4 +372,38 @@ function tripal_feature_update_dependencies() {
   );
 
   return $dependencies;
+}
+
+/**
+ * Fixes an error with the materialized view installation
+ *
+ */
+function tripal_feature_update_7201() {
+
+  // there is a bug in the Tripal v2.0-alpha release that didn't add the
+  // materialized view schema to the mviews table.
+  // get the schema for the materialized view from the custom_tables table
+  // as there is a copy there, but only if the schema is missing from the
+  // materialized view table
+  $view_name = 'organism_feature_count';
+  $schema = db_select('tripal_mviews', 'tm')
+    ->fields('tm', array('mv_schema'))
+    ->condition('name', $view_name)
+    ->execute()
+    ->fetchField();
+  if (!$schema or $schema == 'Array') {
+    $schema = db_select('tripal_custom_tables', 'tct')
+      ->fields('tct', array('schema'))
+      ->condition('table_name', $view_name)
+      ->execute()
+      ->fetchField();
+    $schema_str = var_export(unserialize($schema), TRUE);
+    $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
+    db_update('tripal_mviews')
+      ->fields(array(
+      'mv_schema' => $schema_str
+      ))
+      ->condition('name', $view_name)
+      ->execute();
+  }
 }

+ 34 - 0
tripal_library/tripal_library.install

@@ -399,4 +399,38 @@ function tripal_library_update_dependencies() {
   );
 
   return $dependencies;
+}
+
+/**
+ * Fixes an error with the materialized view installation
+ *
+ */
+function tripal_library_update_7201() {
+
+  // there is a bug in the Tripal v2.0-alpha release that didn't add the
+  // materialized view schema to the mviews table.
+  // get the schema for the materialized view from the custom_tables table
+  // as there is a copy there, but only if the schema is missing from the
+  // materialized view table
+  $view_name = 'library_feature_count';
+  $schema = db_select('tripal_mviews', 'tm')
+    ->fields('tm', array('mv_schema'))
+    ->condition('name', $view_name)
+    ->execute()
+    ->fetchField();
+  if (!$schema or $schema == 'Array') {
+    $schema = db_select('tripal_custom_tables', 'tct')
+      ->fields('tct', array('schema'))
+      ->condition('table_name', $view_name)
+      ->execute()
+      ->fetchField();
+    $schema_str = var_export(unserialize($schema), TRUE);
+    $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
+    db_update('tripal_mviews')
+      ->fields(array(
+      'mv_schema' => $schema_str
+      ))
+      ->condition('name', $view_name)
+      ->execute();
+  }
 }