[icinga-checkins] icinga.org: icinga-web/master: *db-update and db-purge-userpreferences added

git at icinga.org git at icinga.org
Fri May 28 17:32:47 CEST 2010


Module: icinga-web
Branch: master
Commit: d370018a76296604a48ede712828961b4c4f8018
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=d370018a76296604a48ede712828961b4c4f8018

Author: jmosshammer <jannis.mosshammer at netways.de>
Date:   Fri May 28 17:12:06 2010 +0200

*db-update and db-purge-userpreferences added

---

 etc/build.xml            |   11 +++--------
 etc/lib/dbUpdateTask.php |   44 +++++++++++++++++++++++++++++++++++++++-----
 etc/lib/doctrineTask.php |   14 ++++++++++++--
 3 files changed, 54 insertions(+), 15 deletions(-)

diff --git a/etc/build.xml b/etc/build.xml
index 4140d34..9510b1a 100755
--- a/etc/build.xml
+++ b/etc/build.xml
@@ -51,14 +51,9 @@
 	</target>
 
 	<target name="db-purge-userprefs" depends="db-prepare">
-		
-		<property name="db.sql.truncateprefs" value="truncate table ${db.name}.nsm_user_preference" />
-		
-		<exec
-			command="${db.mysql.call_global} -e '${db.sql.truncateprefs}'"
-			dir="${builddir}"
-			passthru="true"
-			checkreturn="false" />
+		<taskdef name="doctrine" classname="etc.lib.doctrineTask"/>
+		<doctrine action="truncateTable" targettable="nsm_user_preference" icingapath="${src}" modelPath="${db.doctrine.models}" dsn="${db.dsn}"/>
+
 		
 	</target>
 	
diff --git a/etc/lib/dbUpdateTask.php b/etc/lib/dbUpdateTask.php
index 92cdc79..2b6f845 100644
--- a/etc/lib/dbUpdateTask.php
+++ b/etc/lib/dbUpdateTask.php
@@ -9,7 +9,8 @@ class dbUpdateTask extends doctrineTask {
 		parent::main();
 		$this->checkIfDBExists();
 		$this->checkVersion();
-		$this->updateStructure();
+		$altered = $this->updateStructure();
+		$this->updateVersion();
 	}
 	
 	public function checkVersion() {
@@ -37,10 +38,43 @@ class dbUpdateTask extends doctrineTask {
 	}
 	
 	public function updateStructure() {
-		echo "\nUpdating DB Structure";
-		$models = Doctrine::getLoadedModels();
-		print_r($models);
+		echo "\nUpdating DB Structure\n";
+		$models = Doctrine::loadModels($this->modelPath,Doctrine_Core::MODEL_LOADING_AGGRESSIVE,"Nsm");
+		$allAltered = array();
+		foreach(Doctrine::getLoadedModels() as $model) {
+			$class = new $model;
+			$altered = array();
+			//test
+			$cols = $class->getTable()->getColumns();
+			$tn = $class->getTable()->getTableName();
 		
+			$conn = Doctrine_Manager::connection();
+			foreach($cols as $name=>$columns) {
+				try {
+					$conn->export->alterTable($tn,array(
+						add=>array($name=>$columns)
+					));
+					$altered[] = $name;
+					echo "Added column ".$name." to ".$model."\n";
+				} catch(Exception $e) {
+				}
+			}
+
+			$allAltered[$model] = $altered;
+		}	
+		return $allAltered;
+	}
+
+	protected function updateVersion() {
+		$thisVersion = NsmDbVersion::getInitialData();
+		$newVersion = $thisVersion[0]["version"];
+		
+		$vers = Doctrine_Core::getTable("NsmDbVersion")->findBy("vers_id",1)->getFirst();
+		if(!$vers instanceof NsmDbVersion) { 
+			$vers = new NsmDbVersion();
+			$vers->set("vers_id",1);
+		}
+		$vers->set("version",$newVersion);
+		$vers->save();
 	}
-	
 }
\ No newline at end of file
diff --git a/etc/lib/doctrineTask.php b/etc/lib/doctrineTask.php
index 0e800a8..1a6e635 100644
--- a/etc/lib/doctrineTask.php
+++ b/etc/lib/doctrineTask.php
@@ -9,6 +9,7 @@ class doctrineTask extends Task {
 	protected $modelPath = "app/modules/AppKit/lib/database/models/";
 	protected $dsn;
 	protected $action;
+	protected $targetTable;
 	
 	public function setAction($action) {
 		$this->action = $action;
@@ -20,6 +21,10 @@ class doctrineTask extends Task {
 	public function setModelpath($path) {
 		$this->modelPath = $path;
 	}
+	public function setTargettable($table) {
+		$this->targetTable = $table;
+	}
+	
 	public function setDsn($conn) {
 		$this->dsn = $conn;
 	}
@@ -33,10 +38,15 @@ class doctrineTask extends Task {
 	public function main() {
 		Doctrine_Manager::connection($this->dsn,"mainConnection");
 	
-		Doctrine::loadModels($this->modelPath."generated/");
+		Doctrine::loadModels($this->modelPath."/generated/");
 		Doctrine::setModelsDirectory($this->modelPath."/");
-		if($this->action == 'dropDB')
+		if($this->action == 'dropDB') {
 			$this->dropDB();
+		} else if($this->action="truncateTable" && $this->targetTable) {
+			Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh()->query(
+				"truncate table ".$this->targetTable.";"
+			);
+		} 
 	}
 	
 	public function dropDB() {





More information about the icinga-checkins mailing list