From d081a1a5adf007b383b0140c3ac469249d79878a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Thu, 20 Apr 2017 15:55:30 +0200 Subject: [PATCH] quote index columns on oracle, handle all index changes, minor phpdoc cleanup --- lib/private/DB/OracleMigrator.php | 112 +++++++++++++++++++++++------- 1 file changed, 85 insertions(+), 27 deletions(-) diff --git a/lib/private/DB/OracleMigrator.php b/lib/private/DB/OracleMigrator.php index 2735529b5e..b01ec78684 100644 --- a/lib/private/DB/OracleMigrator.php +++ b/lib/private/DB/OracleMigrator.php @@ -32,6 +32,55 @@ use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Table; class OracleMigrator extends NoCheckMigrator { + + /** + * Quote a column's name but changing the name requires recreating + * the column instance and copying over all properties. + * + * @param Column $column old column + * @return Column new column instance with new name + */ + protected function quoteColumn(Column $column) { + $newColumn = new Column( + $this->connection->quoteIdentifier($column->getName()), + $column->getType() + ); + $newColumn->setAutoincrement($column->getAutoincrement()); + $newColumn->setColumnDefinition($column->getColumnDefinition()); + $newColumn->setComment($column->getComment()); + $newColumn->setDefault($column->getDefault()); + $newColumn->setFixed($column->getFixed()); + $newColumn->setLength($column->getLength()); + $newColumn->setNotnull($column->getNotnull()); + $newColumn->setPrecision($column->getPrecision()); + $newColumn->setScale($column->getScale()); + $newColumn->setUnsigned($column->getUnsigned()); + $newColumn->setPlatformOptions($column->getPlatformOptions()); + $newColumn->setCustomSchemaOptions($column->getPlatformOptions()); + return $newColumn; + } + + /** + * Quote an index's name but changing the name requires recreating + * the index instance and copying over all properties. + * + * @param Index $index old index + * @return Index new index instance with new name + */ + protected function quoteIndex($index) { + return new Index( + //TODO migrate existing uppercase indexes, then $this->connection->quoteIdentifier($index->getName()), + $index->getName(), + array_map(function($columnName) { + return $this->connection->quoteIdentifier($columnName); + }, $index->getColumns()), + $index->isUnique(), + $index->isPrimary(), + $index->getFlags(), + $index->getOptions() + ); + } + /** * @param Schema $targetSchema * @param \Doctrine\DBAL\Connection $connection @@ -46,35 +95,10 @@ class OracleMigrator extends NoCheckMigrator { return new Table( $this->connection->quoteIdentifier($table->getName()), array_map(function(Column $column) { - $newColumn = new Column( - $this->connection->quoteIdentifier($column->getName()), - $column->getType() - ); - $newColumn->setAutoincrement($column->getAutoincrement()); - $newColumn->setColumnDefinition($column->getColumnDefinition()); - $newColumn->setComment($column->getComment()); - $newColumn->setDefault($column->getDefault()); - $newColumn->setFixed($column->getFixed()); - $newColumn->setLength($column->getLength()); - $newColumn->setNotnull($column->getNotnull()); - $newColumn->setPrecision($column->getPrecision()); - $newColumn->setScale($column->getScale()); - $newColumn->setUnsigned($column->getUnsigned()); - $newColumn->setPlatformOptions($column->getPlatformOptions()); - $newColumn->setCustomSchemaOptions($column->getPlatformOptions()); - return $newColumn; + return $this->quoteColumn($column); }, $table->getColumns()), array_map(function(Index $index) { - return new Index( - $this->connection->quoteIdentifier($index->getName()), - array_map(function($columnName) { - return $this->connection->quoteIdentifier($columnName); - }, $index->getColumns()), - $index->isUnique(), - $index->isPrimary(), - $index->getFlags(), - $index->getOptions() - ); + return $this->quoteIndex($index); }, $table->getIndexes()), $table->getForeignKeys(), 0, @@ -95,14 +119,48 @@ class OracleMigrator extends NoCheckMigrator { foreach ($schemaDiff->changedTables as $tableDiff) { $tableDiff->name = $this->connection->quoteIdentifier($tableDiff->name); + + $tableDiff->addedColumns = array_map(function(Column $column) { + return $this->quoteColumn($column); + }, $tableDiff->addedColumns); + foreach ($tableDiff->changedColumns as $column) { $column->oldColumnName = $this->connection->quoteIdentifier($column->oldColumnName); // auto increment is not relevant for oracle and can anyhow not be applied on change $column->changedProperties = array_diff($column->changedProperties, ['autoincrement', 'unsigned']); } + // remove columns that no longer have changed (because autoincrement and unsigned are not supported) $tableDiff->changedColumns = array_filter($tableDiff->changedColumns, function (ColumnDiff $column) { return count($column->changedProperties) > 0; }); + + $tableDiff->removedColumns = array_map(function(Column $column) { + return $this->quoteColumn($column); + }, $tableDiff->removedColumns); + + $tableDiff->renamedColumns = array_map(function(Column $column) { + return $this->quoteColumn($column); + }, $tableDiff->renamedColumns); + + $tableDiff->addedIndexes = array_map(function(Index $index) { + return $this->quoteIndex($index); + }, $tableDiff->addedIndexes); + + $tableDiff->changedIndexes = array_map(function(Index $index) { + return $this->quoteIndex($index); + }, $tableDiff->changedIndexes); + + $tableDiff->removedIndexes = array_map(function(Index $index) { + return $this->quoteIndex($index); + }, $tableDiff->removedIndexes); + + $tableDiff->renamedIndexes = array_map(function(Index $index) { + return $this->quoteIndex($index); + }, $tableDiff->renamedIndexes); + + // TODO handle $tableDiff->addedForeignKeys + // TODO handle $tableDiff->changedForeignKeys + // TODO handle $tableDiff->removedForeignKeys } return $schemaDiff;