[pLog-svn] r3951 - plog/trunk

oscar at devel.lifetype.net oscar at devel.lifetype.net
Sat Sep 9 12:46:28 GMT 2006


Author: oscar
Date: 2006-09-09 12:46:27 +0000 (Sat, 09 Sep 2006)
New Revision: 3951

Added:
   plog/trunk/updatedbschema.php
Log:
just to make it easier to update the database schema


Added: plog/trunk/updatedbschema.php
===================================================================
--- plog/trunk/updatedbschema.php	2006-09-09 11:53:15 UTC (rev 3950)
+++ plog/trunk/updatedbschema.php	2006-09-09 12:46:27 UTC (rev 3951)
@@ -0,0 +1,63 @@
+<?php
+
+/**
+ * Quick and dirty script to update a database schema to the most recent version of the schema
+ * as defined by file install/dbschemas.properties.php
+ */
+
+if (!defined( "PLOG_CLASS_PATH" )) {
+    define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
+}
+
+include_once( PLOG_CLASS_PATH."class/database/db.class.php" );
+include_once( PLOG_CLASS_PATH."class/config/configfilestorage.class.php" );
+include_once( PLOG_CLASS_PATH."install/dbschemas.properties.php" );
+
+$config = new ConfigFileStorage();
+// open a connection to the database
+$db = PDb::getDriver('mysql');
+
+if( !$db->Connect($config->getValue( "db_host" ), $config->getValue( "db_username" ), $config->getValue( "db_password" ), $config->getValue( "db_database" ), $config->getValue( "db_character_set" ))) {
+	die( "could not connect to database!" );
+}
+
+$prefix = $config->getValue( "db_prefix" );
+
+$dict = NewPDbDataDictionary( $db );            
+$errors = false;
+$message = "";
+foreach( $Tables as $name => $table ) {
+    $errorMessage = "";
+    $table_errors = false;
+	$upperName = $dict->upperName;
+	$tableSchema = $table["schema"];
+	if ( isset( $table["options"] ) )
+	{
+		$tableOptions = $table["options"];
+		$options = array ( $upperName => $tableOptions );
+	} else {
+		$options = array ();
+    }
+   
+    // generate the code with the changes for the table
+    $sqlarray = $dict->ChangeTableSQL( $prefix.$name, $tableSchema, $options );
+    
+    foreach( $sqlarray as $sql ) {
+        // and run the query
+        if( !$db->Execute( $sql )) {
+            $table_errors = true;
+            $errors = true;
+            $errorMessage .= $db->ErrorMsg()."<br/>";
+        }
+    }
+
+    if( !$table_errors )
+        $message .= "Changes to table <strong>$name</strong> executed successfully.<br/>";
+    else {
+        $message .= "Error modifying table $name: ".$errorMessage;
+    }
+}
+
+print($message);
+
+?>
\ No newline at end of file



More information about the pLog-svn mailing list