From ba99f53fd5655b84e4131068a6b288237628e2d4 Mon Sep 17 00:00:00 2001 From: beberlei Date: Sun, 6 Dec 2009 18:55:08 +0000 Subject: [PATCH] [2.0] DDC-169 - Fix implicit/explicit index creation differences between platforms --- lib/Doctrine/DBAL/Platforms/MySqlPlatform.php | 5 ++ .../DBAL/Schema/AbstractSchemaManager.php | 4 +- lib/Doctrine/DBAL/Schema/Comparator.php | 4 + lib/Doctrine/DBAL/Schema/Schema.php | 16 +++- .../DBAL/Schema/Visitor/FixSchema.php | 77 +++++++++++++++++++ 5 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 lib/Doctrine/DBAL/Schema/Visitor/FixSchema.php diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php index 6726ea823..d95ecac02 100644 --- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php @@ -794,4 +794,9 @@ class MySqlPlatform extends AbstractPlatform { return 'mysql'; } + + public function createsExplicitIndexForForeignKeys() + { + return true; + } } diff --git a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php index 1a423b2ae..4528d6c1a 100644 --- a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php @@ -51,7 +51,7 @@ abstract class AbstractSchemaManager /** * Holds instance of the database platform used for this schema manager * - * @var \Doctrine\DBAL\Platform\AbstractPlatform + * @var \Doctrine\DBAL\Platforms\AbstractPlatform */ protected $_platform; @@ -936,6 +936,6 @@ abstract class AbstractSchemaManager } $tables = $this->listTables(); - return new Schema($tables, $sequences); + return new Schema($tables, $sequences, $this->_platform->createsExplicitIndexForForeignKeys()); } } \ No newline at end of file diff --git a/lib/Doctrine/DBAL/Schema/Comparator.php b/lib/Doctrine/DBAL/Schema/Comparator.php index 06e4c42b1..21aa43ad8 100644 --- a/lib/Doctrine/DBAL/Schema/Comparator.php +++ b/lib/Doctrine/DBAL/Schema/Comparator.php @@ -72,6 +72,10 @@ class Comparator */ public function compare(Schema $fromSchema, Schema $toSchema) { + if ($fromSchema->hasExplicitForeignKeyIndexes() && !$toSchema->hasExplicitForeignKeyIndexes()) { + $toSchema->visit(new \Doctrine\DBAL\Schema\Visitor\FixSchema(true)); + } + $diff = new SchemaDiff(); $foreignKeysToTable = array(); diff --git a/lib/Doctrine/DBAL/Schema/Schema.php b/lib/Doctrine/DBAL/Schema/Schema.php index e1b90a3ff..19a97917f 100644 --- a/lib/Doctrine/DBAL/Schema/Schema.php +++ b/lib/Doctrine/DBAL/Schema/Schema.php @@ -46,11 +46,16 @@ class Schema extends AbstractAsset */ protected $_sequences = array(); + /** + * @var bool + */ + protected $_hasExplicitForeignKeyIndexes = false; + /** * @param array $tables * @param array $sequences */ - public function __construct(array $tables=array(), array $sequences=array()) + public function __construct(array $tables=array(), array $sequences=array(), $hasExplicitForeignKeyIndexes=false) { foreach ($tables AS $table) { $this->_addTable($table); @@ -58,6 +63,15 @@ class Schema extends AbstractAsset foreach ($sequences AS $sequence) { $this->_addSequence($sequence); } + $this->_hasExplicitForeignKeyIndexes = $hasExplicitForeignKeyIndexes; + } + + /** + * @return bool + */ + public function hasExplicitForeignKeyIndexes() + { + return $this->_hasExplicitForeignKeyIndexes; } /** diff --git a/lib/Doctrine/DBAL/Schema/Visitor/FixSchema.php b/lib/Doctrine/DBAL/Schema/Visitor/FixSchema.php new file mode 100644 index 000000000..4e3e0458b --- /dev/null +++ b/lib/Doctrine/DBAL/Schema/Visitor/FixSchema.php @@ -0,0 +1,77 @@ +_addExplicitIndexForForeignKey = $addExplicitIndexForForeignKey; + } + + /** + * @param Schema $schema + */ + public function acceptSchema(Schema $schema) + { + + } + + /** + * @param Table $table + */ + public function acceptTable(Table $table) + { + + } + + /** + * @param Column $column + */ + public function acceptColunn(Table $table, Column $column) + { + + } + + /** + * @param Table $localTable + * @param ForeignKeyConstraint $fkConstraint + */ + public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) + { + if ($this->_addExplicitIndexForForeignKey) { + $localTable->addIndex($fkConstraint->getColumns()); + } + } + + /** + * @param Table $table + * @param Index $index + */ + public function acceptIndex(Table $table, Index $index) + { + + } + + /** + * @param Sequence $sequence + */ + public function acceptSequence(Sequence $sequence) + { + + } +} \ No newline at end of file