소스 검색

Removed transactions from Chado API and updated MView code to use Drupal transactions

Stephen Ficklin 11 년 전
부모
커밋
81d6f01681
2개의 변경된 파일27개의 추가작업 그리고 71개의 파일을 삭제
  1. 0 49
      tripal_core/api/tripal_core_chado.api.inc
  2. 27 22
      tripal_core/api/tripal_core_mviews.api.inc

+ 0 - 49
tripal_core/api/tripal_core_chado.api.inc

@@ -2538,55 +2538,6 @@ function tripal_core_delete_property_by_id($basetable, $record_id) {
 }
 
 
-/**
- * Start a transaction block. Ensures the use of a persistent chado connection
- */
-function tripal_db_start_transaction() {
-  $transaction = db_transaction();
-  return $transaction;
-}
-
-/**
- * Set a savepoint to roll the current transaction back to if an error is encountered
- */
-function tripal_db_set_savepoint_transaction($savepoint, $release = FALSE) {
-  // Postgresql requires a savepoint of the same name to be unset before re-use
-  if ($release) {
-    chado_query("RELEASE SAVEPOINT :savepoint", array(':savepoint' => $savepoint));
-  }
-  chado_query("SAVEPOINT :savepoint", array(':savepoint' => $savepoint));
-}
-
-/**
- * Rollback changes.
- *
- * If $savepoint is NULL then rollback to the beginning of the transaction,
- * Otherwise, rollback to the point at which the named $savepoint was created
- *
- * @param $savepoint
- *   The name of the saved point in the transaction to rollback to
- *
- * @param $commit
- *   The transcation will only be committed if this value is TRUE. The
- *   default is TRUE.
- *
- * @return nothing
- */
-function tripal_db_rollback_transaction($savepoint = NULL, $commit = TRUE) {
-
-  if ($savepoint) {
-    chado_query("ROLLBACK TO SAVEPOINT :savepoint", array(':savepoint' => $savepoint));
-  }
-  else {
-    chado_query("ROLLBACK");
-  }
-
-  if ($commit) {
-    tripal_db_commit_transaction();
-  }
-
-}
-
 /**
  * Retrieves the schema in an array for the specified custom table.
  *

+ 27 - 22
tripal_core/api/tripal_core_mviews.api.inc

@@ -378,34 +378,39 @@ function tripal_update_mview($mview_id) {
   if ($mview) {    
     // execute the query inside a transaction so that it doesn't destroy existing data
     // that may leave parts of the site unfunctional
-    tripal_db_start_transaction();
-    $previous_db = tripal_db_set_active('chado');  // use chado database
-    $results = db_query("DELETE FROM {" . $mview->mv_table . "}");
-    $results = db_query("INSERT INTO {" . $mview->mv_table . "} ($mview->query)");    
-    tripal_db_set_active($previous_db);  // now use drupal database
-    if ($results) {
-      // commit the transaction
-      tripal_db_commit_transaction();
-      $sql = "SELECT count(*) as cnt FROM {" . $mview->mv_table . "}";
-      $results = chado_query($sql);
-      $count = $results->fetchObject();
-      $record = new stdClass();
-      $record->mview_id = $mview_id;
-      $record->last_update = REQUEST_TIME;
-      $record->status = "Populated with " . number_format($count->cnt) . " rows";
-      drupal_write_record('tripal_mviews', $record, 'mview_id');
-      return TRUE;
+    $transaction = db_transaction();
+    try {
+      $previous_db = tripal_db_set_active('chado');  // use chado database
+      $success = db_query("DELETE FROM {" . $mview->mv_table . "}");
+      $success = db_query("INSERT INTO {" . $mview->mv_table . "} ($mview->query)");    
+      tripal_db_set_active($previous_db);  // now use drupal database
+      // if success get the number of results and update the table record
+      if ($success) { 
+        $sql = "SELECT count(*) as cnt FROM {" . $mview->mv_table . "}";
+        $results = chado_query($sql);
+        $count = $results->fetchObject();
+        $record = new stdClass();
+        $record->mview_id = $mview_id;
+        $record->last_update = REQUEST_TIME;
+        $record->status = "Populated with " . number_format($count->cnt) . " rows";
+        drupal_write_record('tripal_mviews', $record, 'mview_id');
+      }
+      // if not success then throw an error 
+      else {
+        throw new Exception("ERROR populating the materialized view ". $mview->mv_table . ". See Drupal's recent log entries for details.");
+      }
     }
-    else {
-      // rollback the transaction
-      tripal_db_rollback_transaction();
+    catch (Exception $e) {
       // print and save the error message
       $record = new stdClass();
       $record->mview_id = $mview_id;
-      $record->status = "ERROR populating. See Drupal's recent log entries for details.";
-      print $record->status . "\n";
+      $record->status = "ERROR populating $mview->mv_table. See Drupal's recent log entries for details.\n";
       drupal_write_record('tripal_mviews', $record, 'mview_id');
+      watchdog_exception('tripal_mviews', $e);
+      $transaction->rollback();
       return FALSE;
     }
+    print "Done.\n";
+    return TRUE;
   }
 }