|  | @@ -123,32 +123,46 @@ function tripal_add_mview($name, $modulename, $mv_schema, $query, $comment = NUL
 | 
	
		
			
				|  |  |       return NULL;
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -  // Create a new record
 | 
	
		
			
				|  |  | -  $record = new stdClass();
 | 
	
		
			
				|  |  | -  $record->name = $name;
 | 
	
		
			
				|  |  | -  $record->modulename = $modulename;
 | 
	
		
			
				|  |  | -  $record->mv_table = $mv_table;
 | 
	
		
			
				|  |  | -  $record->query = $query;
 | 
	
		
			
				|  |  | -  $record->comment = $comment;
 | 
	
		
			
				|  |  | -  $record->mv_schema = $mv_schema;
 | 
	
		
			
				|  |  | +  // see if the mv_table name already exsists
 | 
	
		
			
				|  |  | +  $mview_id = db_query(
 | 
	
		
			
				|  |  | +    'SELECT mview_id FROM {tripal_mviews} WHERE name = :name',
 | 
	
		
			
				|  |  | +    array(':name' => $name))->fetchField();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -  // 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)) {
 | 
	
		
			
				|  |  | +  if(!$mview_id) {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    // drop the table from chado if it exists
 | 
	
		
			
				|  |  | -    if (chado_table_exists($mv_table)) {
 | 
	
		
			
				|  |  | -      $sql = 'DROP TABLE {' . $mv_table . '}';
 | 
	
		
			
				|  |  | -      chado_query($sql);
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -    // create the table
 | 
	
		
			
				|  |  | -    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)));
 | 
	
		
			
				|  |  | +    // Create a new record
 | 
	
		
			
				|  |  | +    $record = new stdClass();
 | 
	
		
			
				|  |  | +    $record->name = $name;
 | 
	
		
			
				|  |  | +    $record->modulename = $modulename;
 | 
	
		
			
				|  |  | +    $record->mv_table = $mv_table;
 | 
	
		
			
				|  |  | +    $record->query = $query;
 | 
	
		
			
				|  |  | +    $record->comment = $comment;
 | 
	
		
			
				|  |  | +    $record->mv_schema = $mv_schema;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    // 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);
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +      // create the table
 | 
	
		
			
				|  |  | +      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)));
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  | +  else {
 | 
	
		
			
				|  |  | +    tripal_report_error('tripal_cv', TRIPAL_WARNING,
 | 
	
		
			
				|  |  | +      "Materialized view, %vname, already exists. Cannot create.",
 | 
	
		
			
				|  |  | +      array('%vname' => $name));
 | 
	
		
			
				|  |  | +    drupal_set_message(t("Materialized view, $name, already exists. Cannot create.", array('%name' => $name)));
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 |