Move management of the schema_info table entirely into the Migrator class.
authorChuck Hagenbuch <chuck@horde.org>
Wed, 13 Jan 2010 21:51:17 +0000 (16:51 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Thu, 14 Jan 2010 02:20:35 +0000 (21:20 -0500)
Also make the schema_info table name configurable.

framework/Db/lib/Horde/Db/Adapter/Base/Schema.php
framework/Db/lib/Horde/Db/Migration/Migrator.php
framework/Db/test/Horde/Db/Adapter/MysqliTest.php
framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php
framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php
framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php
framework/Db/test/Horde/Db/Migration/MigratorTest.php

index aa5d9e9..690cc73 100644 (file)
@@ -669,20 +669,6 @@ abstract class Horde_Db_Adapter_Base_Schema
     abstract public function currentDatabase();
 
     /**
-     * Should not be called normally, but this operation is non-destructive.
-     * The migrations module handles this automatically.
-     */
-    public function initializeSchemaInformation()
-    {
-        try {
-            $this->execute("CREATE TABLE schema_info (".
-                           "  version ".$this->typeToSql('integer').
-                           ")");
-            return $this->execute("INSERT INTO schema_info (version) VALUES (0)");
-        } catch (Exception $e) {}
-    }
-
-    /**
      * The sql for this column type
      *
      * @param   string  $type
index 7bf75f0..0895867 100644 (file)
@@ -38,6 +38,11 @@ class Horde_Db_Migration_Migrator
      */
     protected $_targetVersion = null;
 
+    /**
+     * @var string
+     */
+    protected $_schemaTableName = 'schema_info';
+
 
     /*##########################################################################
     # Constructor
@@ -63,7 +68,7 @@ class Horde_Db_Migration_Migrator
         $this->_logger         = new Horde_Support_Stub();
         $this->_inflector      = new Horde_Support_Inflector();
 
-        $this->_connection->initializeSchemaInformation();
+        $this->_initializeSchemaInformation();
     }
 
 
@@ -111,15 +116,28 @@ class Horde_Db_Migration_Migrator
         $this->_doMigrate();
     }
 
+
+    /*##########################################################################
+    # Accessor
+    ##########################################################################*/
+
     /**
-     * @return  int
+     * @return  integer
      */
     public function getCurrentVersion()
     {
-        $sql = 'SELECT version FROM schema_info';
+        $sql = 'SELECT version FROM ' . $this->_schemaTableName;
         return $this->_connection->selectValue($sql);
     }
 
+    /**
+     * @param  string  $schemaTableName
+     */
+    public function setSchemaTableName($schemaTableName)
+    {
+        $this->_schemaTableName = $schemaTableName;
+    }
+
 
     /*##########################################################################
     # Protected
@@ -216,12 +234,25 @@ class Horde_Db_Migration_Migrator
     }
 
     /**
+     * @TODO
+     */
+    protected function _initializeSchemaInformation()
+    {
+        try {
+            $schemaTable = $this->_connection->createTable($this->_schemaTableName, array('primaryKey' => false));
+              $schemaTable->column('version', 'integer');
+            $schemaTable->end();
+            return $this->_connection->insert('INSERT INTO ' . $this->_schemaTableName . ' (version) VALUES (0)');
+        } catch (Exception $e) {}
+    }
+
+    /**
      * @param   integer $version
      */
     protected function _setSchemaVersion($version)
     {
         $version = $this->_isDown() ? $version - 1 : $version;
-        $sql = "UPDATE schema_info SET version = " . (int)$version;
+        $sql = 'UPDATE ' . $this->_schemaTableName . ' SET version = ' . (int)$version;
         $this->_connection->update($sql);
     }
 
index 3ace11b..0b6e17a 100644 (file)
@@ -1059,14 +1059,6 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
         $this->assertNotEquals($expected, $structure);
     }
 
-    public function testInitializeSchemaInformation()
-    {
-        $this->_conn->initializeSchemaInformation();
-
-        $sql = "SELECT version FROM schema_info";
-        $this->assertEquals(0, $this->_conn->selectValue($sql));
-    }
-
     public function testTypeToSqlTypePrimaryKey()
     {
         $result = $this->_conn->typeToSql('primaryKey');
index c0fe67b..cb787cd 100644 (file)
@@ -1079,14 +1079,6 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
         $this->assertNotEquals($expected, $structure);
     }
 
-    public function testInitializeSchemaInformation()
-    {
-        $this->_conn->initializeSchemaInformation();
-
-        $sql = "SELECT version FROM schema_info";
-        $this->assertEquals(0, $this->_conn->selectValue($sql));
-    }
-
     public function testTypeToSqlTypePrimaryKey()
     {
         $result = $this->_conn->typeToSql('primaryKey');
index 79322c9..cab6db3 100644 (file)
@@ -927,14 +927,6 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('test', $name);
     }
 
-    public function testInitializeSchemaInformation()
-    {
-        $this->_conn->initializeSchemaInformation();
-
-        $sql = "SELECT version FROM schema_info";
-        $this->assertEquals(0, $this->_conn->selectValue($sql));
-    }
-
     public function testTypeToSqlTypePrimaryKey()
     {
         $result = $this->_conn->typeToSql('primaryKey');
index 065fc55..d597bad 100644 (file)
@@ -946,14 +946,6 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase
         $this->assertNotEquals($expected, $structure);
     }
 
-    public function testInitializeSchemaInformation()
-    {
-        $this->_conn->initializeSchemaInformation();
-
-        $sql = "SELECT version FROM schema_info";
-        $this->assertEquals(0, $this->_conn->selectValue($sql));
-    }
-
     public function testTypeToSqlTypePrimaryKey()
     {
         $result = $this->_conn->typeToSql('primaryKey');
index 7d88506..a486d6c 100644 (file)
@@ -96,6 +96,15 @@ client:
         */
     }
 
+    public function testInitializeSchemaInformation()
+    {
+        $dir = dirname(dirname(__FILE__)).'/fixtures/migrations/';
+        $migrator = new Horde_Db_Migration_Migrator($this->_conn, $dir);
+
+        $sql = "SELECT version FROM schema_info";
+        $this->assertEquals(0, $this->_conn->selectValue($sql));
+    }
+
     public function testMigrator()
     {
         $columns = $this->_columnNames('users');