diff options
author | Robin Appelman <icewind@owncloud.com> | 2014-01-30 14:21:16 +0100 |
---|---|---|
committer | Robin Appelman <icewind@owncloud.com> | 2014-06-03 11:17:21 +0200 |
commit | 0035147be96537193f0c7119dfd0628fe7363a26 (patch) | |
tree | 4af065ac1c7410603173c48890c6591879f2fcc0 | |
parent | 9c6a93a87c835dee5fb0b580865d4f70836685cf (diff) | |
download | nextcloud-server-0035147be96537193f0c7119dfd0628fe7363a26.tar.gz nextcloud-server-0035147be96537193f0c7119dfd0628fe7363a26.zip |
Create unique names for temporary indexes
-rw-r--r-- | lib/private/db/migrator.php | 26 | ||||
-rw-r--r-- | tests/lib/db/migrator.php | 13 |
2 files changed, 31 insertions, 8 deletions
diff --git a/lib/private/db/migrator.php b/lib/private/db/migrator.php index 91060663f87..a6c61f35424 100644 --- a/lib/private/db/migrator.php +++ b/lib/private/db/migrator.php @@ -9,9 +9,11 @@ namespace OC\DB; use \Doctrine\DBAL\DBALException; +use \Doctrine\DBAL\Schema\Index; use \Doctrine\DBAL\Schema\Table; use \Doctrine\DBAL\Schema\Schema; use \Doctrine\DBAL\Schema\SchemaConfig; +use \Doctrine\DBAL\Schema\Comparator; class Migrator { /** @@ -57,15 +59,16 @@ class Migrator { * Check the migration of a table on a copy so we can detect errors before messing with the real table * * @param \Doctrine\DBAL\Schema\Table $table + * @throws \OC\DB\MigrationException */ protected function checkTableMigrate(Table $table) { $name = $table->getName(); - $tmpName = $name . '_copy_' . uniqid(); + $tmpName = uniqid(); $this->copyTable($name, $tmpName); //create the migration schema for the temporary table - $tmpTable = new Table($tmpName, $table->getColumns(), $table->getIndexes(), $table->getForeignKeys(), $table->_idGeneratorType, $table->getOptions()); + $tmpTable = $this->renameTableSchema($table, $tmpName); $schemaConfig = new SchemaConfig(); $schemaConfig->setName($this->connection->getDatabase()); $schema = new Schema(array($tmpTable), array(), $schemaConfig); @@ -80,6 +83,23 @@ class Migrator { } /** + * @param \Doctrine\DBAL\Schema\Table $table + * @param string $newName + * @return \Doctrine\DBAL\Schema\Table + */ + protected function renameTableSchema(Table $table, $newName) { + $indexes = $table->getIndexes(); + $newIndexes = array(); + foreach ($indexes as $index) { + $indexName = uniqid(); // avoid conflicts in index names + $newIndexes[] = new Index($indexName, $index->getColumns(), $index->isUnique(), $index->isPrimary(), $index->getFlags()); + } + + // foreign keys are not supported so we just set it to an empty array + return new Table($newName, $table->getColumns(), $indexes, array(), 0, $table->getOptions()); + } + + /** * @param \Doctrine\DBAL\Schema\Schema $targetSchema * @param \Doctrine\DBAL\Connection $connection */ @@ -104,7 +124,7 @@ class Migrator { } } - $comparator = new \Doctrine\DBAL\Schema\Comparator(); + $comparator = new Comparator(); $schemaDiff = $comparator->compare($sourceSchema, $targetSchema); foreach ($schemaDiff->changedTables as $tableDiff) { diff --git a/tests/lib/db/migrator.php b/tests/lib/db/migrator.php index ed7c36dbdf4..e08f1cf4547 100644 --- a/tests/lib/db/migrator.php +++ b/tests/lib/db/migrator.php @@ -20,30 +20,33 @@ class Migrator extends \PHPUnit_Framework_TestCase { private $tableName; + private $fullTableName; + public function setUp() { $this->tableName = 'test_' . uniqid(); $this->connection = \OC_DB::getConnection(); - $this->tableName = $this->connection->getDatabase() . '.' . $this->tableName; + $this->fullTableName = $this->connection->getDatabase() . '.' . $this->tableName; } public function tearDown() { - $this->connection->exec('DROP TABLE ' . $this->tableName); + $this->connection->exec('DROP TABLE ' . $this->fullTableName); } private function getInitialSchema() { $schema = new Schema(array(), array(), $this->getSchemaConfig()); - $table = $schema->createTable($this->tableName); + $table = $schema->createTable($this->fullTableName); $table->addColumn('id', 'integer'); $table->addColumn('name', 'string'); + $table->addIndex(array('id'), $this->tableName . '_id'); return $schema; } private function getNewSchema() { $schema = new Schema(array(), array(), $this->getSchemaConfig()); - $table = $schema->createTable($this->tableName); + $table = $schema->createTable($this->fullTableName); $table->addColumn('id', 'integer'); $table->addColumn('name', 'string'); - $table->addUniqueIndex(array('id')); + $table->addUniqueIndex(array('id'), $this->tableName . '_id'); return $schema; } |