incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maha...@apache.org
Subject svn commit: r1360023 - in /incubator/ambari/trunk: CHANGES.txt hmc/php/upgrade/upgradeDB.php
Date Wed, 11 Jul 2012 06:22:46 GMT
Author: mahadev
Date: Wed Jul 11 06:22:46 2012
New Revision: 1360023

URL: http://svn.apache.org/viewvc?rev=1360023&view=rev
Log:
AMBARI-622. Upgrade DB only when Ambari Config table exists. (mahadev)

Modified:
    incubator/ambari/trunk/CHANGES.txt
    incubator/ambari/trunk/hmc/php/upgrade/upgradeDB.php

Modified: incubator/ambari/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/CHANGES.txt?rev=1360023&r1=1360022&r2=1360023&view=diff
==============================================================================
--- incubator/ambari/trunk/CHANGES.txt (original)
+++ incubator/ambari/trunk/CHANGES.txt Wed Jul 11 06:22:46 2012
@@ -11,6 +11,7 @@ Trunk (unreleased changes)
   INCOMPATIBLE CHANGES
 
   NEW FEATURES
+    AMBARI-622. Upgrade DB only when Ambari Config table exists. (mahadev)
 
     AMBARI-621. On Cluster Summary page, show Hadoop stack version information
     and "Upgrade available" link if a newer version of the stack is available

Modified: incubator/ambari/trunk/hmc/php/upgrade/upgradeDB.php
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/hmc/php/upgrade/upgradeDB.php?rev=1360023&r1=1360022&r2=1360023&view=diff
==============================================================================
--- incubator/ambari/trunk/hmc/php/upgrade/upgradeDB.php (original)
+++ incubator/ambari/trunk/hmc/php/upgrade/upgradeDB.php Wed Jul 11 06:22:46 2012
@@ -21,18 +21,86 @@
  */
 
 
-$db = new PDO("sqlite:".$argv[1]);
+class UpgradeDB {
+
+  private $dbHandle;
+
+  private $dbPath;
+
+  private $logger;
+
+  function __construct($db) {
+    $this->dbPath = $db;
+    $dsn = "sqlite:".$db;
+    if (!file_exists($db)) {
+      print("Sqlite db file does not exist, db=$db");
+      throw new Exception("Could not find sqlite db file, db=$db");
+    }
+    $this->dbHandle = new PDO($dsn);
+    if ($this->dbHandle === FALSE) {
+      $this->logger->log_error("Could not open sqlite db, db=$db");
+      throw new Exception("Could not open sqlite db, db=$db");
+    }
+  }
+  
+  /**
+    * Get the version of Ambari that needs to be upgraded
+    * @return mixed
+    * array ("version" => $ambariVersion,
+    *        "result" => 0,
+    *        "error" => "");
+    */
+  public function getAmbariVersion () {
+    $query = "select name from sqlite_master where type = \"table\" and name LIKE \"AmbariConfig\"";
+    $response = array( "result" => 0, "error" => "");
+    $pdoStmt = $this->dbHandle->query($query);
+    if ($pdoStmt === FALSE) {
+      $error = "Failed to run sql query $query";
+      print($error);
+      $response["result"] = 1;
+      $response["error"] = $error;
+      return $response;
+    }
+    $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
+    if (isset($result) && is_array($result) && count($result) == 0) {
+      //there is no table named AmbariConfig - 1.0
+      $response["version"] = "1.0.0";
+    }
+    else {
+      $query = "SELECT version FROM AmbariConfig WHERE key_name = \"Ambari\" AND
+        tag_name = \"Current\"";
+      $pdoStmt = $this->dbHandle->query($query);
+      if ($pdoStmt === FALSE) {
+        $error = "Error when executing query $query";
+        $response["result"] = 1;
+        $response["error"] = $error;
+        return $response;
+      }
+      $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
+      if (isset($result) && is_array($result) && count($result) == 1) {
+        $response["version"] = $result[0]["version"];
+      }
+    }
+    return $response;
+  }
+}
 $upgradeDir = $argv[2];
-if ($handle = opendir($upgradeDir)) {
-  while (false !== ($entry = readdir($handle))) {
-    if ($entry != "." && $entry != "..") { 
-      $absolutePath = $upgradeDir."/".$entry;
-      print_r("DB file path is $absolutePath");
-      $sql = file_get_contents($absolutePath);
-      $db->exec($sql) or die(print_r($db->errorInfo(), true));
+$hmcHandle = new UpgradeDB($argv[1]);
+$response = $hmcHandle->getAmbariVersion();
+if ($response["version"] == "1.0.0") {
+  $db = new PDO("sqlite:".$argv[1]);
+  if ($handle = opendir($upgradeDir)) {
+    while (false !== ($entry = readdir($handle))) {
+      if ($entry != "." && $entry != "..") { 
+        $absolutePath = $upgradeDir."/".$entry;
+        $sql = file_get_contents($absolutePath);
+        $db->exec($sql) or die(print_r($db->errorInfo(), true));
+      }
     }
+    closedir($handle);
   }
-  closedir($handle);
+} else {
+  // no need for upgrade
+  //print ("No Need for upgrade");
 }
-
 ?>



Mime
View raw message