Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #473 from deeky666/DBAL-234

[DBAL-234] [DBAL-566] Add support for renaming indexes
  • Loading branch information...
commit 6d0e8e2a8598f121cdb136507022915247fc98b8 2 parents fba268b + 7ed642d
@deeky666 deeky666 authored
Showing with 858 additions and 7 deletions.
  1. +1 −0  docs/en/reference/platforms.rst
  2. +29 −0 lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
  3. +8 −1 lib/Doctrine/DBAL/Platforms/DB2Platform.php
  4. +281 −0 lib/Doctrine/DBAL/Platforms/Keywords/MySQL57Keywords.php
  5. +50 −0 lib/Doctrine/DBAL/Platforms/MySQL57Platform.php
  6. +8 −0 lib/Doctrine/DBAL/Platforms/OraclePlatform.php
  7. +9 −0 lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
  8. +10 −0 lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php
  9. +15 −1 lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
  10. +2 −1  lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
  11. +5 −0 lib/Doctrine/DBAL/Schema/Comparator.php
  12. +46 −0 lib/Doctrine/DBAL/Schema/Table.php
  13. +7 −0 lib/Doctrine/DBAL/Schema/TableDiff.php
  14. +3 −0  lib/Doctrine/DBAL/Tools/Console/Command/ReservedWordsCommand.php
  15. +16 −2 tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php
  16. +64 −0 tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
  17. +21 −0 tests/Doctrine/Tests/DBAL/Platforms/DB2PlatformTest.php
  18. +37 −0 tests/Doctrine/Tests/DBAL/Platforms/MySQL57PlatformTest.php
  19. +24 −0 tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php
  20. +21 −0 tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php
  21. +21 −0 tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php
  22. +21 −0 tests/Doctrine/Tests/DBAL/Platforms/SQLAnywherePlatformTest.php
  23. +21 −0 tests/Doctrine/Tests/DBAL/Platforms/SQLServerPlatformTest.php
  24. +31 −0 tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php
  25. +7 −2 tests/Doctrine/Tests/DBAL/Schema/ComparatorTest.php
  26. +100 −0 tests/Doctrine/Tests/DBAL/Schema/TableTest.php
View
1  docs/en/reference/platforms.rst
@@ -34,6 +34,7 @@ MySQL
^^^^^
- ``MySqlPlatform`` for version 5.0 and above.
+- ``MySQL57Platform`` for version 5.7 and above.
Oracle
^^^^^^
View
29 lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
@@ -21,6 +21,7 @@
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Connection;
+use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Types;
use Doctrine\DBAL\Schema\Constraint;
use Doctrine\DBAL\Schema\Sequence;
@@ -1823,10 +1824,12 @@ protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff)
: $diff->getName()->getQuotedName($this);
$sql = array();
+
if ($this->supportsForeignKeyConstraints()) {
foreach ($diff->addedForeignKeys as $foreignKey) {
$sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
}
+
foreach ($diff->changedForeignKeys as $foreignKey) {
$sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
}
@@ -1835,14 +1838,40 @@ protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff)
foreach ($diff->addedIndexes as $index) {
$sql[] = $this->getCreateIndexSQL($index, $tableName);
}
+
foreach ($diff->changedIndexes as $index) {
$sql[] = $this->getCreateIndexSQL($index, $tableName);
}
+ foreach ($diff->renamedIndexes as $oldIndexName => $index) {
+ $oldIndexName = new Identifier($oldIndexName);
+ $sql = array_merge(
+ $sql,
+ $this->getRenameIndexSQL($oldIndexName->getQuotedName($this), $index, $tableName)
+ );
+ }
+
return $sql;
}
/**
+ * Returns the SQL for renaming an index on a table.
+ *
+ * @param string $oldIndexName The name of the index to rename from.
+ * @param \Doctrine\DBAL\Schema\Index $index The definition of the index to rename to.
+ * @param string $tableName The table to rename the given index on.
+ *
+ * @return array The sequence of SQL statements for renaming the given index.
+ */
+ protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
+ {
+ return array(
+ $this->getDropIndexSQL($oldIndexName, $tableName),
+ $this->getCreateIndexSQL($index, $tableName)
+ );
+ }
+
+ /**
* Common code for alter table statement generation that updates the changed Index and Foreign Key definitions.
*
* @param \Doctrine\DBAL\Schema\TableDiff $diff
View
9 lib/Doctrine/DBAL/Platforms/DB2Platform.php
@@ -19,7 +19,6 @@
namespace Doctrine\DBAL\Platforms;
-use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\TableDiff;
@@ -576,6 +575,14 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
}
/**
+ * {@inheritdoc}
+ */
+ protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
+ {
+ return array('RENAME INDEX ' . $oldIndexName . ' TO ' . $index->getQuotedName($this));
+ }
+
+ /**
* {@inheritDoc}
*/
public function getDefaultValueDeclarationSQL($field)
View
281 lib/Doctrine/DBAL/Platforms/Keywords/MySQL57Keywords.php
@@ -0,0 +1,281 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace Doctrine\DBAL\Platforms\Keywords;
+
+/**
+ * MySQL 5.7 reserved keywords list.
+ *
+ * @author Steve Müller <st.mueller@dzh-online.de>
+ * @link www.doctrine-project.org
+ * @since 2.5
+ */
+class MySQL57Keywords extends MySQLKeywords
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function getName()
+ {
+ return 'MySQL57';
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * @link http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-7.html
+ */
+ protected function getKeywords()
+ {
+ return array(
+ 'ACCESSIBLE',
+ 'ADD',
+ 'ALL',
+ 'ALTER',
+ 'ANALYZE',
+ 'AND',
+ 'AS',
+ 'ASC',
+ 'ASENSITIVE',
+ 'BEFORE',
+ 'BETWEEN',
+ 'BIGINT',
+ 'BINARY',
+ 'BLOB',
+ 'BOTH',
+ 'BY',
+ 'CALL',
+ 'CASCADE',
+ 'CASE',
+ 'CHANGE',
+ 'CHAR',
+ 'CHARACTER',
+ 'CHECK',
+ 'COLLATE',
+ 'COLUMN',
+ 'CONDITION',
+ 'CONSTRAINT',
+ 'CONTINUE',
+ 'CONVERT',
+ 'CREATE',
+ 'CROSS',
+ 'CURRENT_DATE',
+ 'CURRENT_TIME',
+ 'CURRENT_TIMESTAMP',
+ 'CURRENT_USER',
+ 'CURSOR',
+ 'DATABASE',
+ 'DATABASES',
+ 'DAY_HOUR',
+ 'DAY_MICROSECOND',
+ 'DAY_MINUTE',
+ 'DAY_SECOND',
+ 'DEC',
+ 'DECIMAL',
+ 'DECLARE',
+ 'DEFAULT',
+ 'DELAYED',
+ 'DELETE',
+ 'DESC',
+ 'DESCRIBE',
+ 'DETERMINISTIC',
+ 'DISTINCT',
+ 'DISTINCTROW',
+ 'DIV',
+ 'DOUBLE',
+ 'DROP',
+ 'DUAL',
+ 'EACH',
+ 'ELSE',
+ 'ELSEIF',
+ 'ENCLOSED',
+ 'ESCAPED',
+ 'EXISTS',
+ 'EXIT',
+ 'EXPLAIN',
+ 'FALSE',
+ 'FETCH',
+ 'FLOAT',
+ 'FLOAT4',
+ 'FLOAT8',
+ 'FOR',
+ 'FORCE',
+ 'FOREIGN',
+ 'FROM',
+ 'FULLTEXT',
+ 'GET',
+ 'GRANT',
+ 'GROUP',
+ 'HAVING',
+ 'HIGH_PRIORITY',
+ 'HOUR_MICROSECOND',
+ 'HOUR_MINUTE',
+ 'HOUR_SECOND',
+ 'IF',
+ 'IGNORE',
+ 'IN',
+ 'INDEX',
+ 'INFILE',
+ 'INNER',
+ 'INOUT',
+ 'INSENSITIVE',
+ 'INSERT',
+ 'INT',
+ 'INT1',
+ 'INT2',
+ 'INT3',
+ 'INT4',
+ 'INT8',
+ 'INTEGER',
+ 'INTERVAL',
+ 'INTO',
+ 'IO_AFTER_GTIDS',
+ 'IO_BEFORE_GTIDS',
+ 'IS',
+ 'ITERATE',
+ 'JOIN',
+ 'KEY',
+ 'KEYS',
+ 'KILL',
+ 'LEADING',
+ 'LEAVE',
+ 'LEFT',
+ 'LIKE',
+ 'LIMIT',
+ 'LINEAR',
+ 'LINES',
+ 'LOAD',
+ 'LOCALTIME',
+ 'LOCALTIMESTAMP',
+ 'LOCK',
+ 'LONG',
+ 'LONGBLOB',
+ 'LONGTEXT',
+ 'LOOP',
+ 'LOW_PRIORITY',
+ 'MASTER_BIND',
+ 'MASTER_SSL_VERIFY_SERVER_CERT',
+ 'MATCH',
+ 'MAXVALUE',
+ 'MEDIUMBLOB',
+ 'MEDIUMINT',
+ 'MEDIUMTEXT',
+ 'MIDDLEINT',
+ 'MINUTE_MICROSECOND',
+ 'MINUTE_SECOND',
+ 'MOD',
+ 'MODIFIES',
+ 'NATURAL',
+ 'NO_WRITE_TO_BINLOG',
+ 'NONBLOCKING',
+ 'NOT',
+ 'NULL',
+ 'NUMERIC',
+ 'ON',
+ 'OPTIMIZE',
+ 'OPTION',
+ 'OPTIONALLY',
+ 'OR',
+ 'ORDER',
+ 'OUT',
+ 'OUTER',
+ 'OUTFILE',
+ 'PARTITION',
+ 'PRECISION',
+ 'PRIMARY',
+ 'PROCEDURE',
+ 'PURGE',
+ 'RANGE',
+ 'READ',
+ 'READ_WRITE',
+ 'READS',
+ 'REAL',
+ 'REFERENCES',
+ 'REGEXP',
+ 'RELEASE',
+ 'RENAME',
+ 'REPEAT',
+ 'REPLACE',
+ 'REQUIRE',
+ 'RESIGNAL',
+ 'RESTRICT',
+ 'RETURN',
+ 'REVOKE',
+ 'RIGHT',
+ 'RLIKE',
+ 'SCHEMA',
+ 'SCHEMAS',
+ 'SECOND_MICROSECOND',
+ 'SELECT',
+ 'SENSITIVE',
+ 'SEPARATOR',
+ 'SET',
+ 'SHOW',
+ 'SIGNAL',
+ 'SMALLINT',
+ 'SPATIAL',
+ 'SPECIFIC',
+ 'SQL',
+ 'SQL_BIG_RESULT',
+ 'SQL_CALC_FOUND_ROWS',
+ 'SQL_SMALL_RESULT',
+ 'SQLEXCEPTION',
+ 'SQLSTATE',
+ 'SQLWARNING',
+ 'SSL',
+ 'STARTING',
+ 'STRAIGHT_JOIN',
+ 'TABLE',
+ 'TERMINATED',
+ 'THEN',
+ 'TINYBLOB',
+ 'TINYINT',
+ 'TINYTEXT',
+ 'TO',
+ 'TRAILING',
+ 'TRIGGER',
+ 'TRUE',
+ 'UNDO',
+ 'UNION',
+ 'UNIQUE',
+ 'UNLOCK',
+ 'UNSIGNED',
+ 'UPDATE',
+ 'USAGE',
+ 'USE',
+ 'USING',
+ 'UTC_DATE',
+ 'UTC_TIME',
+ 'UTC_TIMESTAMP',
+ 'VALUES',
+ 'VARBINARY',
+ 'VARCHAR',
+ 'VARCHARACTER',
+ 'VARYING',
+ 'WHEN',
+ 'WHERE',
+ 'WHILE',
+ 'WITH',
+ 'WRITE',
+ 'XOR',
+ 'YEAR_MONTH',
+ 'ZEROFILL',
+ );
+ }
+}
View
50 lib/Doctrine/DBAL/Platforms/MySQL57Platform.php
@@ -0,0 +1,50 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace Doctrine\DBAL\Platforms;
+
+use Doctrine\DBAL\Schema\Index;
+
+/**
+ * Provides the behavior, features and SQL dialect of the MySQL 5.7 database platform.
+ *
+ * @author Steve Müller <st.mueller@dzh-online.de>
+ * @link www.doctrine-project.org
+ * @since 2.5
+ */
+class MySQL57Platform extends MySqlPlatform
+{
+ /**
+ * {@inheritdoc}
+ */
+ protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
+ {
+ return array(
+ 'ALTER TABLE ' . $tableName . ' RENAME INDEX ' . $oldIndexName . ' TO ' . $index->getQuotedName($this)
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function getReservedKeywordsClass()
+ {
+ return 'Doctrine\DBAL\Platforms\Keywords\MySQL57Keywords';
+ }
+}
View
8 lib/Doctrine/DBAL/Platforms/OraclePlatform.php
@@ -763,6 +763,14 @@ public function getColumnDeclarationSQL($name, array $field)
}
/**
+ * {@inheritdoc}
+ */
+ protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
+ {
+ return array('ALTER INDEX ' . $oldIndexName . ' RENAME TO ' . $index->getQuotedName($this));
+ }
+
+ /**
* {@inheritDoc}
*/
public function prefersSequences()
View
9 lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
@@ -21,6 +21,7 @@
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
+use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\BinaryType;
use Doctrine\DBAL\Types\BlobType;
@@ -585,6 +586,14 @@ private function isUnchangedBinaryColumn(ColumnDiff $columnDiff)
/**
* {@inheritdoc}
*/
+ protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
+ {
+ return array('ALTER INDEX ' . $oldIndexName . ' RENAME TO ' . $index->getQuotedName($this));
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function getCommentOnColumnSQL($tableName, $columnName, $comment)
{
$comment = $comment === null ? 'NULL' : "'$comment'";
View
10 lib/Doctrine/DBAL/Platforms/SQLAnywherePlatform.php
@@ -1452,6 +1452,16 @@ protected function getCreateIndexSQLFlags(Index $index)
/**
* {@inheritdoc}
*/
+ protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
+ {
+ return array(
+ 'ALTER INDEX ' . $oldIndexName . ' ON ' . $tableName . ' RENAME TO ' . $index->getQuotedName($this)
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
protected function getReservedKeywordsClass()
{
return 'Doctrine\DBAL\Platforms\Keywords\SQLAnywhereKeywords';
View
16 lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
@@ -22,7 +22,6 @@
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\TableDiff;
-use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
@@ -660,6 +659,21 @@ protected function getDropColumnCommentSQL($tableName, $columnName)
}
/**
+ * {@inheritdoc}
+ */
+ protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
+ {
+ return array(
+ sprintf(
+ "EXEC sp_RENAME N'%s.%s', N'%s', N'INDEX'",
+ $tableName,
+ $oldIndexName,
+ $index->getQuotedName($this)
+ )
+ );
+ }
+
+ /**
* Returns the SQL statement for adding an extended property to a database object.
*
* @param string $name The name of the property to add.
View
3  lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
@@ -849,6 +849,7 @@ private function getSimpleAlterTableSQL(TableDiff $diff)
if ( ! empty($diff->renamedColumns) || ! empty($diff->addedForeignKeys) || ! empty($diff->addedIndexes)
|| ! empty($diff->changedColumns) || ! empty($diff->changedForeignKeys) || ! empty($diff->changedIndexes)
|| ! empty($diff->removedColumns) || ! empty($diff->removedForeignKeys) || ! empty($diff->removedIndexes)
+ || ! empty($diff->renamedIndexes)
) {
return false;
}
@@ -969,7 +970,7 @@ private function getIndexesInAlteredTable(TableDiff $diff)
}
}
- foreach (array_merge($diff->changedIndexes, $diff->addedIndexes) as $index) {
+ foreach (array_merge($diff->changedIndexes, $diff->addedIndexes, $diff->renamedIndexes) as $index) {
$indexName = strtolower($index->getName());
if (strlen($indexName)) {
$indexes[$indexName] = $index;
View
5 lib/Doctrine/DBAL/Schema/Comparator.php
@@ -227,6 +227,11 @@ public function diffTable(Table $table1, Table $table2)
foreach ($table2Indexes as $index2Name => $index2Definition) {
foreach ($table1Indexes as $index1Name => $index1Definition) {
if ($this->diffIndex($index1Definition, $index2Definition) === false) {
+ if ( ! $index1Definition->isPrimary() && $index1Name != $index2Name) {
+ $tableDifferences->renamedIndexes[$index1Name] = $index2Definition;
+ $changes++;
+ }
+
unset($table1Indexes[$index1Name]);
unset($table2Indexes[$index2Name]);
} else {
View
46 lib/Doctrine/DBAL/Schema/Table.php
@@ -207,6 +207,52 @@ public function addUniqueIndex(array $columnNames, $indexName = null)
}
/**
+ * Renames an index.
+ *
+ * @param string $oldIndexName The name of the index to rename from.
+ * @param string|null $newIndexName The name of the index to rename to.
+ * If null is given, the index name will be auto-generated.
+ *
+ * @return \Doctrine\DBAL\Schema\Table This table instance.
+ *
+ * @throws SchemaException if no index exists for the given current name
+ * or if an index with the given new name already exists on this table.
+ */
+ public function renameIndex($oldIndexName, $newIndexName = null)
+ {
+ $oldIndexName = strtolower($oldIndexName);
+ $normalizedNewIndexName = strtolower($newIndexName);
+
+ if ($oldIndexName === $normalizedNewIndexName) {
+ return $this;
+ }
+
+ if ( ! $this->hasIndex($oldIndexName)) {
+ throw SchemaException::indexDoesNotExist($oldIndexName, $this->_name);
+ }
+
+ if ($this->hasIndex($normalizedNewIndexName)) {
+ throw SchemaException::indexAlreadyExists($normalizedNewIndexName, $this->_name);
+ }
+
+ $oldIndex = $this->_indexes[$oldIndexName];
+
+ if ($oldIndex->isPrimary()) {
+ $this->dropPrimaryKey();
+
+ return $this->setPrimaryKey($oldIndex->getColumns(), $newIndexName);
+ }
+
+ unset($this->_indexes[$oldIndexName]);
+
+ if ($oldIndex->isUnique()) {
+ return $this->addUniqueIndex($oldIndex->getColumns(), $newIndexName);
+ }
+
+ return $this->addIndex($oldIndex->getColumns(), $newIndexName, $oldIndex->getFlags());
+ }
+
+ /**
* Checks if an index begins in the order of the given columns.
*
* @param array $columnsNames
View
7 lib/Doctrine/DBAL/Schema/TableDiff.php
@@ -88,6 +88,13 @@ class TableDiff
public $removedIndexes = array();
/**
+ * Indexes that are only renamed but are identical otherwise.
+ *
+ * @var \Doctrine\DBAL\Schema\Index[]
+ */
+ public $renamedIndexes = array();
+
+ /**
* All added foreign key definitions
*
* @var \Doctrine\DBAL\Schema\ForeignKeyConstraint[]
View
3  lib/Doctrine/DBAL/Tools/Console/Command/ReservedWordsCommand.php
@@ -33,6 +33,7 @@ class ReservedWordsCommand extends Command
*/
private $keywordListClasses = array(
'mysql' => 'Doctrine\DBAL\Platforms\Keywords\MySQLKeywords',
+ 'mysql57' => 'Doctrine\DBAL\Platforms\Keywords\MySQL57Keywords',
'sqlserver' => 'Doctrine\DBAL\Platforms\Keywords\SQLServerKeywords',
'sqlserver2005' => 'Doctrine\DBAL\Platforms\Keywords\SQLServer2005Keywords',
'sqlserver2008' => 'Doctrine\DBAL\Platforms\Keywords\SQLServer2008Keywords',
@@ -91,6 +92,7 @@ protected function configure()
The following keyword lists are currently shipped with Doctrine:
* mysql
+ * mysql57
* pgsql
* pgsql92
* sqlite
@@ -120,6 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ( ! $keywordLists) {
$keywordLists = array(
'mysql',
+ 'mysql57',
'pgsql',
'pgsql92',
'sqlite',
View
18 tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php
@@ -425,7 +425,21 @@ public function testAlterTableScenario()
$tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
$tableDiff->fromTable = $table;
- $tableDiff->removedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo', 'foreign_key_test'));
+ $tableDiff->renamedIndexes['foo_idx'] = new \Doctrine\DBAL\Schema\Index('bar_idx', array('foo', 'foreign_key_test'));
+
+ $this->_sm->alterTable($tableDiff);
+
+ $table = $this->_sm->listTableDetails('alter_table');
+ $this->assertEquals(2, count($table->getIndexes()));
+ $this->assertTrue($table->hasIndex('bar_idx'));
+ $this->assertFalse($table->hasIndex('foo_idx'));
+ $this->assertEquals(array('foo', 'foreign_key_test'), array_map('strtolower', $table->getIndex('bar_idx')->getColumns()));
+ $this->assertFalse($table->getIndex('bar_idx')->isPrimary());
+ $this->assertFalse($table->getIndex('bar_idx')->isUnique());
+
+ $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
+ $tableDiff->fromTable = $table;
+ $tableDiff->removedIndexes[] = new \Doctrine\DBAL\Schema\Index('bar_idx', array('foo', 'foreign_key_test'));
$fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('foreign_key_test'), 'alter_table_foreign', array('id'));
$tableDiff->addedForeignKeys[] = $fk;
@@ -433,7 +447,7 @@ public function testAlterTableScenario()
$table = $this->_sm->listTableDetails('alter_table');
// dont check for index size here, some platforms automatically add indexes for foreign keys.
- $this->assertFalse($table->hasIndex('foo_idx'));
+ $this->assertFalse($table->hasIndex('bar_idx'));
if ($this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$fks = $table->getForeignKeys();
View
64 tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
@@ -6,6 +6,7 @@
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
+use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\Type;
@@ -601,4 +602,67 @@ public function testReturnsJsonTypeDeclarationSQL()
$this->_platform->getJsonTypeDeclarationSQL($column)
);
}
+
+ /**
+ * @group DBAL-234
+ */
+ public function testAlterTableRenameIndex()
+ {
+ $tableDiff = new TableDiff('mytable');
+ $tableDiff->fromTable = new Table('mytable');
+ $tableDiff->fromTable->addColumn('id', 'integer');
+ $tableDiff->fromTable->setPrimaryKey(array('id'));
+ $tableDiff->renamedIndexes = array(
+ 'idx_foo' => new Index('idx_bar', array('id'))
+ );
+
+ $this->assertSame(
+ $this->getAlterTableRenameIndexSQL(),
+ $this->_platform->getAlterTableSQL($tableDiff)
+ );
+ }
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'DROP INDEX idx_foo',
+ 'CREATE INDEX idx_bar ON mytable (id)',
+ );
+ }
+
+ /**
+ * @group DBAL-234
+ */
+ public function testQuotesAlterTableRenameIndex()
+ {
+ $tableDiff = new TableDiff('table');
+ $tableDiff->fromTable = new Table('table');
+ $tableDiff->fromTable->addColumn('id', 'integer');
+ $tableDiff->fromTable->setPrimaryKey(array('id'));
+ $tableDiff->renamedIndexes = array(
+ 'create' => new Index('select', array('id')),
+ '`foo`' => new Index('`bar`', array('id')),
+ );
+
+ $this->assertSame(
+ $this->getQuotedAlterTableRenameIndexSQL(),
+ $this->_platform->getAlterTableSQL($tableDiff)
+ );
+ }
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getQuotedAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'DROP INDEX "create"',
+ 'CREATE INDEX "select" ON "table" (id)',
+ 'DROP INDEX "foo"',
+ 'CREATE INDEX "bar" ON "table" (id)',
+ );
+ }
}
View
21 tests/Doctrine/Tests/DBAL/Platforms/DB2PlatformTest.php
@@ -384,4 +384,25 @@ public function testReturnsBinaryTypeDeclarationSQL()
$this->assertSame('BINARY(32704)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 32704)));
$this->assertSame('BLOB(1M)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 32705)));
}
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'RENAME INDEX idx_foo TO idx_bar',
+ );
+ }
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getQuotedAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'RENAME INDEX "create" TO "select"',
+ 'RENAME INDEX "foo" TO "bar"',
+ );
+ }
}
View
37 tests/Doctrine/Tests/DBAL/Platforms/MySQL57PlatformTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Doctrine\Tests\DBAL\Platforms;
+
+use Doctrine\DBAL\Platforms\MySQL57Platform;
+
+class MySQL57PlatformTest extends MySqlPlatformTest
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function createPlatform()
+ {
+ return new MySQL57Platform();
+ }
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'ALTER TABLE mytable RENAME INDEX idx_foo TO idx_bar',
+ );
+ }
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getQuotedAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'ALTER TABLE `table` RENAME INDEX `create` TO `select`',
+ 'ALTER TABLE `table` RENAME INDEX `foo` TO `bar`',
+ );
+ }
+}
View
24 tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php
@@ -484,4 +484,28 @@ public function testDoesNotPropagateForeignKeyAlterationForNonSupportingEngines(
$this->_platform->getAlterTableSQL($tableDiff)
);
}
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'DROP INDEX idx_foo ON mytable',
+ 'CREATE INDEX idx_bar ON mytable (id)',
+ );
+ }
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getQuotedAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'DROP INDEX `create` ON `table`',
+ 'CREATE INDEX `select` ON `table` (id)',
+ 'DROP INDEX `foo` ON `table`',
+ 'CREATE INDEX `bar` ON `table` (id)',
+ );
+ }
}
View
21 tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php
@@ -417,4 +417,25 @@ public function dataCreateSequenceWithCache()
array(3, 'CACHE 3')
);
}
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'ALTER INDEX idx_foo RENAME TO idx_bar',
+ );
+ }
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getQuotedAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'ALTER INDEX "create" RENAME TO "select"',
+ 'ALTER INDEX "foo" RENAME TO "bar"',
+ );
+ }
}
View
21 tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php
@@ -506,4 +506,25 @@ public function testDoesNotPropagateUnnecessaryTableAlterationOnBinaryType()
// BLOB -> BLOB
$this->assertEmpty($this->_platform->getAlterTableSQL($comparator->diffTable($table1, $table2)));
}
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'ALTER INDEX idx_foo RENAME TO idx_bar',
+ );
+ }
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getQuotedAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'ALTER INDEX "create" RENAME TO "select"',
+ 'ALTER INDEX "foo" RENAME TO "bar"',
+ );
+ }
}
View
21 tests/Doctrine/Tests/DBAL/Platforms/SQLAnywherePlatformTest.php
@@ -784,4 +784,25 @@ public function testReturnsBinaryTypeDeclarationSQL()
$this->assertSame('BINARY(32767)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 32767)));
$this->assertSame('LONG BINARY', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 32768)));
}
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'ALTER INDEX idx_foo ON mytable RENAME TO idx_bar',
+ );
+ }
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getQuotedAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'ALTER INDEX "create" ON "table" RENAME TO "select"',
+ 'ALTER INDEX "foo" ON "table" RENAME TO "bar"',
+ );
+ }
}
View
21 tests/Doctrine/Tests/DBAL/Platforms/SQLServerPlatformTest.php
@@ -694,4 +694,25 @@ public function testReturnsBinaryTypeDeclarationSQL()
$this->assertSame('BINARY(8000)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 8000)));
$this->assertSame('VARBINARY(MAX)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 8001)));
}
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getAlterTableRenameIndexSQL()
+ {
+ return array(
+ "EXEC sp_RENAME N'mytable.idx_foo', N'idx_bar', N'INDEX'",
+ );
+ }
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getQuotedAlterTableRenameIndexSQL()
+ {
+ return array(
+ "EXEC sp_RENAME N'[table].[create]', N'[select]', N'INDEX'",
+ "EXEC sp_RENAME N'[table].[foo]', N'[bar]', N'INDEX'",
+ );
+ }
}
View
31 tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php
@@ -323,4 +323,35 @@ public function testReturnsBinaryTypeDeclarationSQL()
$this->assertSame('BLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0)));
$this->assertSame('BLOB', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 9999999)));
}
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'CREATE TEMPORARY TABLE __temp__mytable AS SELECT id FROM mytable',
+ 'DROP TABLE mytable',
+ 'CREATE TABLE mytable (id INTEGER NOT NULL, PRIMARY KEY(id))',
+ 'INSERT INTO mytable (id) SELECT id FROM __temp__mytable',
+ 'DROP TABLE __temp__mytable',
+ 'CREATE INDEX idx_bar ON mytable (id)',
+ );
+ }
+
+ /**
+ * @group DBAL-234
+ */
+ protected function getQuotedAlterTableRenameIndexSQL()
+ {
+ return array(
+ 'CREATE TEMPORARY TABLE __temp__table AS SELECT id FROM "table"',
+ 'DROP TABLE "table"',
+ 'CREATE TABLE "table" (id INTEGER NOT NULL, PRIMARY KEY(id))',
+ 'INSERT INTO "table" (id) SELECT id FROM __temp__table',
+ 'DROP TABLE __temp__table',
+ 'CREATE INDEX "select" ON table (id)',
+ 'CREATE INDEX "bar" ON table (id)',
+ );
+ }
}
View
9 tests/Doctrine/Tests/DBAL/Schema/ComparatorTest.php
@@ -617,9 +617,14 @@ public function testCompareIndexBasedOnPropertiesNotName()
$tableB->addIndex(array("id"), "bar_foo_idx");
$c = new Comparator();
- $tableDiff = $c->diffTable($tableA, $tableB);
+ $tableDiff = new TableDiff('foo');
+ $tableDiff->fromTable = $tableA;
+ $tableDiff->renamedIndexes['foo_bar_idx'] = new Index('bar_foo_idx', array('id'));
- $this->assertFalse($tableDiff);
+ $this->assertEquals(
+ $tableDiff,
+ $c->diffTable($tableA, $tableB)
+ );
}
public function testCompareForeignKeyBasedOnPropertiesNotName()
View
100 tests/Doctrine/Tests/DBAL/Schema/TableTest.php
@@ -525,4 +525,104 @@ public function testDropPrimaryKey()
$table->dropPrimaryKey();
$this->assertFalse($table->hasPrimaryKey());
}
+
+ /**
+ * @group DBAL-234
+ */
+ public function testRenameIndex()
+ {
+ $table = new Table("test");
+ $table->addColumn('id', 'integer');
+ $table->addColumn('foo', 'integer');
+ $table->addColumn('bar', 'integer');
+ $table->addColumn('baz', 'integer');
+ $table->setPrimaryKey(array('id'), 'pk');
+ $table->addIndex(array('foo'), 'idx', array('flag'));
+ $table->addUniqueIndex(array('bar', 'baz'), 'uniq');
+
+ // Rename to custom name.
+ $this->assertSame($table, $table->renameIndex('pk', 'pk_new'));
+ $this->assertSame($table, $table->renameIndex('idx', 'idx_new'));
+ $this->assertSame($table, $table->renameIndex('uniq', 'uniq_new'));
+
+ $this->assertTrue($table->hasPrimaryKey());
+ $this->assertTrue($table->hasIndex('pk_new'));
+ $this->assertTrue($table->hasIndex('idx_new'));
+ $this->assertTrue($table->hasIndex('uniq_new'));
+
+ $this->assertFalse($table->hasIndex('pk'));
+ $this->assertFalse($table->hasIndex('idx'));
+ $this->assertFalse($table->hasIndex('uniq'));
+
+ $this->assertEquals(new Index('pk_new', array('id'), true, true), $table->getPrimaryKey());
+ $this->assertEquals(new Index('pk_new', array('id'), true, true), $table->getIndex('pk_new'));
+ $this->assertEquals(
+ new Index('idx_new', array('foo'), false, false, array('flag')),
+ $table->getIndex('idx_new')
+ );
+ $this->assertEquals(new Index('uniq_new', array('bar', 'baz'), true), $table->getIndex('uniq_new'));
+
+ // Rename to auto-generated name.
+ $this->assertSame($table, $table->renameIndex('pk_new', null));
+ $this->assertSame($table, $table->renameIndex('idx_new', null));
+ $this->assertSame($table, $table->renameIndex('uniq_new', null));
+
+ $this->assertTrue($table->hasPrimaryKey());
+ $this->assertTrue($table->hasIndex('primary'));
+ $this->assertTrue($table->hasIndex('IDX_D87F7E0C8C736521'));
+ $this->assertTrue($table->hasIndex('UNIQ_D87F7E0C76FF8CAA78240498'));
+
+ $this->assertFalse($table->hasIndex('pk_new'));
+ $this->assertFalse($table->hasIndex('idx_new'));
+ $this->assertFalse($table->hasIndex('uniq_new'));
+
+ $this->assertEquals(new Index('primary', array('id'), true, true), $table->getPrimaryKey());
+ $this->assertEquals(new Index('primary', array('id'), true, true), $table->getIndex('primary'));
+ $this->assertEquals(
+ new Index('IDX_D87F7E0C8C736521', array('foo'), false, false, array('flag')),
+ $table->getIndex('IDX_D87F7E0C8C736521')
+ );
+ $this->assertEquals(
+ new Index('UNIQ_D87F7E0C76FF8CAA78240498', array('bar', 'baz'), true),
+ $table->getIndex('UNIQ_D87F7E0C76FF8CAA78240498')
+ );
+
+ // Rename to same name (changed case).
+ $this->assertSame($table, $table->renameIndex('primary', 'PRIMARY'));
+ $this->assertSame($table, $table->renameIndex('IDX_D87F7E0C8C736521', 'idx_D87F7E0C8C736521'));
+ $this->assertSame($table, $table->renameIndex('UNIQ_D87F7E0C76FF8CAA78240498', 'uniq_D87F7E0C76FF8CAA78240498'));
+
+ $this->assertTrue($table->hasPrimaryKey());
+ $this->assertTrue($table->hasIndex('primary'));
+ $this->assertTrue($table->hasIndex('IDX_D87F7E0C8C736521'));
+ $this->assertTrue($table->hasIndex('UNIQ_D87F7E0C76FF8CAA78240498'));
+ }
+
+ /**
+ * @group DBAL-234
+ * @expectedException \Doctrine\DBAL\Schema\SchemaException
+ */
+ public function testThrowsExceptionOnRenamingNonExistingIndex()
+ {
+ $table = new Table("test");
+ $table->addColumn('id', 'integer');
+ $table->addIndex(array('id'), 'idx');
+
+ $table->renameIndex('foo', 'bar');
+ }
+
+ /**
+ * @group DBAL-234
+ * @expectedException \Doctrine\DBAL\Schema\SchemaException
+ */
+ public function testThrowsExceptionOnRenamingToAlreadyExistingIndex()
+ {
+ $table = new Table("test");
+ $table->addColumn('id', 'integer');
+ $table->addColumn('foo', 'integer');
+ $table->addIndex(array('id'), 'idx_id');
+ $table->addIndex(array('foo'), 'idx_foo');
+
+ $table->renameIndex('idx_id', 'idx_foo');
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.