Commit 932fc184 authored by Benjamin Eberlei's avatar Benjamin Eberlei

DBAL-204 - Refactored towards search-path and database/schema handling in comparator code.

parent 2a9e9943
...@@ -2239,7 +2239,7 @@ abstract class AbstractPlatform ...@@ -2239,7 +2239,7 @@ abstract class AbstractPlatform
return Connection::TRANSACTION_READ_COMMITTED; return Connection::TRANSACTION_READ_COMMITTED;
} }
/* supports*() metods */ /* supports*() methods */
/** /**
* Whether the platform supports sequences. * Whether the platform supports sequences.
......
...@@ -816,10 +816,16 @@ abstract class AbstractSchemaManager ...@@ -816,10 +816,16 @@ abstract class AbstractSchemaManager
{ {
$schemaConfig = new SchemaConfig(); $schemaConfig = new SchemaConfig();
$schemaConfig->setMaxIdentifierLength($this->_platform->getMaxIdentifierLength()); $schemaConfig->setMaxIdentifierLength($this->_platform->getMaxIdentifierLength());
$schemaConfig->setSearchPaths($this->getSchemaSearchPaths());
return $schemaConfig; return $schemaConfig;
} }
public function getSchemaSearchPaths()
{
return array($this->_conn->getDatabase());
}
/** /**
* Given a table comment this method tries to extract a typehint for Doctrine Type, or returns * Given a table comment this method tries to extract a typehint for Doctrine Type, or returns
* the type given as default. * the type given as default.
......
...@@ -61,11 +61,11 @@ class Comparator ...@@ -61,11 +61,11 @@ class Comparator
$foreignKeysToTable = array(); $foreignKeysToTable = array();
foreach ( $toSchema->getTables() AS $tableName => $table ) { foreach ( $toSchema->getFullQualifiedTableNames() AS $tableName ) {
if ( !$fromSchema->hasTable($tableName) ) { if ( !$fromSchema->hasFullQualifiedTable($tableName)) {
$diff->newTables[$tableName] = $table; $diff->newTables[$tableName] = $toSchema->getFullQualifiedTable($tableName);
} else { } else {
$tableDifferences = $this->diffTable( $fromSchema->getTable($tableName), $table ); $tableDifferences = $this->diffTable( $fromSchema->getFullQualifiedTable($tableName), $toSchema->getFullQualifiedTable($tableName) );
if ( $tableDifferences !== false ) { if ( $tableDifferences !== false ) {
$diff->changedTables[$tableName] = $tableDifferences; $diff->changedTables[$tableName] = $tableDifferences;
} }
...@@ -73,8 +73,9 @@ class Comparator ...@@ -73,8 +73,9 @@ class Comparator
} }
/* Check if there are tables removed */ /* Check if there are tables removed */
foreach ( $fromSchema->getTables() AS $tableName => $table ) { foreach ( $fromSchema->getFullQualifiedTableNames() AS $tableName ) {
if ( !$toSchema->hasTable($tableName) ) { $table = $fromSchema->getFullQualifiedTable($tableName);
if ( !$toSchema->hasFullQualifiedTable($tableName) ) {
$diff->removedTables[$tableName] = $table; $diff->removedTables[$tableName] = $table;
} }
......
...@@ -81,6 +81,11 @@ class Schema extends AbstractAsset ...@@ -81,6 +81,11 @@ class Schema extends AbstractAsset
return $this->_schemaConfig->hasExplicitForeignKeyIndexes(); return $this->_schemaConfig->hasExplicitForeignKeyIndexes();
} }
public function getName()
{
return $this->_schemaConfig->getName();
}
/** /**
* @param Table $table * @param Table $table
*/ */
...@@ -143,6 +148,73 @@ class Schema extends AbstractAsset ...@@ -143,6 +148,73 @@ class Schema extends AbstractAsset
return isset($this->_tables[$tableName]); return isset($this->_tables[$tableName]);
} }
/**
* Get all table names, prefixed with a schema name, even the default one
* if present.
*
* @return array
*/
public function getFullQualifiedTableNames()
{
$names = array();
foreach ($this->_tables as $table) {
$names[] = $table->getFullQualifiedTableName($this->_schemaConfig->getName());
}
return $names;
}
/**
* Does this schema have a table with the given FQN?
*
* @return bool
*/
public function hasFullQualifiedTable($fqTableName)
{
$fqTableName = strtolower($fqTableName);
if (strpos($fqTableName, ".") === false) {
$shortTableName = $fqTableName;
} else {
$parts = explode(".", $fqTableName);
$shortTableName = $fqTableName[1];
}
foreach ($this->_tables as $table) {
foreach ($this->_schemaConfig->getSearchPaths() as $searchPathSchema) {
if (strtolower($table->getFullQualifiedTableName($searchPathSchema)) == $fqTableName) {
return true;
}
}
if (strtolower($table->getName()) == $shortTableName || strtolower($table->getName()) == $fqTableName) {
return true;
}
}
return false;
}
public function getFullQualifiedTable($fqTableName)
{
$fqTableName = strtolower($fqTableName);
if (strpos($fqTableName, ".") === false) {
$shortTableName = $fqTableName;
} else {
$parts = explode(".", $fqTableName);
$shortTableName = $fqTableName[1];
}
foreach ($this->_tables as $table) {
foreach ($this->_schemaConfig->getSearchPaths() as $searchPathSchema) {
if (strtolower($table->getFullQualifiedTableName($searchPathSchema)) == $fqTableName) {
return $table;
}
}
if (strtolower($table->getName()) == $shortTableName) {
return $table;
}
}
throw SchemaException::tableDoesNotExist($fqTableName);
}
/** /**
* @param string $sequenceName * @param string $sequenceName
* @return bool * @return bool
......
<?php <?php
/* /*
* $Id: Schema.php 6876 2009-12-06 23:11:35Z beberlei $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
...@@ -27,7 +25,6 @@ namespace Doctrine\DBAL\Schema; ...@@ -27,7 +25,6 @@ namespace Doctrine\DBAL\Schema;
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Benjamin Eberlei <kontakt@beberlei.de> * @author Benjamin Eberlei <kontakt@beberlei.de>
*/ */
class SchemaConfig class SchemaConfig
...@@ -42,6 +39,11 @@ class SchemaConfig ...@@ -42,6 +39,11 @@ class SchemaConfig
*/ */
protected $_maxIdentifierLength = 63; protected $_maxIdentifierLength = 63;
/**
* @var array
*/
protected $_searchPaths = array();
/** /**
* @return bool * @return bool
*/ */
...@@ -73,4 +75,22 @@ class SchemaConfig ...@@ -73,4 +75,22 @@ class SchemaConfig
{ {
return $this->_maxIdentifierLength; return $this->_maxIdentifierLength;
} }
}
\ No newline at end of file public function setSearchPaths($paths)
{
$this->_searchPaths = $paths;
}
public function getSearchPaths()
{
return $this->_searchPaths;
}
public function getName()
{
if ($this->_searchPaths) {
return $this->_searchPaths[0];
}
return "";
}
}
...@@ -596,6 +596,27 @@ class Table extends AbstractAsset ...@@ -596,6 +596,27 @@ class Table extends AbstractAsset
return $this->_options; return $this->_options;
} }
/**
* Get the Fully-Qualified Table Name.
*
* The full-qualified table name is "schema.name". If the table name
* already has a schema, then the schema passed to the method is ignored.
* If no schema is set, then the passed schema is prepended as the table is
* in the current default schema.
*
* @param string $schemaName - Current schema this table is in.
* @return string
*/
public function getFullQualifiedTableName($schemaName)
{
if (strpos($this->_name, ".") !== false) {
return $this->_name;
} else if ($schemaName) {
return $schemaName . "." . $this->_name;
}
return $this->_name;
}
/** /**
* @param Visitor $visitor * @param Visitor $visitor
*/ */
...@@ -632,4 +653,4 @@ class Table extends AbstractAsset ...@@ -632,4 +653,4 @@ class Table extends AbstractAsset
$this->_fkConstraints[$k]->setLocalTable($this); $this->_fkConstraints[$k]->setLocalTable($this);
} }
} }
} }
\ No newline at end of file
...@@ -22,6 +22,7 @@ namespace Doctrine\Tests\DBAL\Schema; ...@@ -22,6 +22,7 @@ namespace Doctrine\Tests\DBAL\Schema;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
use Doctrine\DBAL\Schema\Schema, use Doctrine\DBAL\Schema\Schema,
Doctrine\DBAL\Schema\SchemaConfig,
Doctrine\DBAL\Schema\Table, Doctrine\DBAL\Schema\Table,
Doctrine\DBAL\Schema\Column, Doctrine\DBAL\Schema\Column,
Doctrine\DBAL\Schema\Index, Doctrine\DBAL\Schema\Index,
...@@ -702,6 +703,62 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -702,6 +703,62 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(), $c->diffColumn($column, $column2)); $this->assertEquals(array(), $c->diffColumn($column, $column2));
} }
/**
* @group DBAL-204
*/
public function testFqnSchemaComparision()
{
$config = new SchemaConfig();
$config->setSearchPaths(array("foo"));
$oldSchema = new Schema(array(), array(), $config);
$oldSchema->createTable('bar');
$newSchema = new Schema();
$newSchema->createTable('foo.bar');
$c = new Comparator();
$this->assertEquals(new SchemaDiff(), $c->compare($oldSchema, $newSchema));
}
/**
* @group DBAL-204
*/
public function testFqnSchemaComparisionDifferent()
{
$config = new SchemaConfig();
$config->setSearchPaths(array("foo"));
$oldSchema = new Schema(array(), array(), $config);
$oldSchema->createTable('bar');
$newSchema = new Schema();
$newSchema->createTable('bar.bar');
$c = new Comparator();
$diff = $c->compare($oldSchema, $newSchema);
$this->assertTrue(isset($diff->newTables["bar.bar"]));
$this->assertTrue(isset($diff->removedTables["foo.bar"]));
}
/**
* @group DBAL-204
*/
public function testFqnSchemaComparisionNoSchemaSame()
{
$config = new SchemaConfig();
$config->setSearchPaths(array("foo"));
$oldSchema = new Schema(array(), array(), $config);
$oldSchema->createTable('bar');
$newSchema = new Schema();
$newSchema->createTable('bar');
$c = new Comparator();
$diff = $c->compare($oldSchema, $newSchema);
$this->assertEquals(new SchemaDiff(), $c->compare($oldSchema, $newSchema));
}
/** /**
* @param SchemaDiff $diff * @param SchemaDiff $diff
* @param int $newTableCount * @param int $newTableCount
...@@ -727,4 +784,4 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -727,4 +784,4 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($changeSequenceCount, count($diff->changedSequences), "Expected number of changed sequences is wrong."); $this->assertEquals($changeSequenceCount, count($diff->changedSequences), "Expected number of changed sequences is wrong.");
$this->assertEquals($removeSequenceCount, count($diff->removedSequences), "Expected number of removed sequences is wrong."); $this->assertEquals($removeSequenceCount, count($diff->removedSequences), "Expected number of removed sequences is wrong.");
} }
} }
\ No newline at end of file
...@@ -483,4 +483,18 @@ class TableTest extends \Doctrine\Tests\DbalTestCase ...@@ -483,4 +483,18 @@ class TableTest extends \Doctrine\Tests\DbalTestCase
$this->assertEquals("test.test", $table->getName()); $this->assertEquals("test.test", $table->getName());
$this->assertEquals("`test`.`test`", $table->getQuotedName(new \Doctrine\DBAL\Platforms\MySqlPlatform)); $this->assertEquals("`test`.`test`", $table->getQuotedName(new \Doctrine\DBAL\Platforms\MySqlPlatform));
} }
}
\ No newline at end of file /**
* @group DBAL-204
*/
public function testFullQualifiedTableName()
{
$table = new Table("`test`.`test`");
$this->assertEquals('test.test', $table->getFullQualifiedTableName("test"));
$this->assertEquals('test.test', $table->getFullQualifiedTableName("other"));
$table = new Table("test");
$this->assertEquals('test.test', $table->getFullQualifiedTableName("test"));
$this->assertEquals('other.test', $table->getFullQualifiedTableName("other"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment