From e16b9ca488c2813e573b362bf99580f1693432ea Mon Sep 17 00:00:00 2001 From: Michael Lipinski Date: Tue, 14 Jul 2015 18:40:16 +0300 Subject: [PATCH] [!] #BUG-1963 Do not drop not dropped indexes --- classes/XLite/Model/Repo/ARepo.php | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git classes/XLite/Model/Repo/ARepo.php classes/XLite/Model/Repo/ARepo.php index e74031e..daddf51 100644 --- classes/XLite/Model/Repo/ARepo.php +++ classes/XLite/Model/Repo/ARepo.php @@ -1044,6 +1044,56 @@ abstract class ARepo extends \Doctrine\ORM\EntityRepository } /** + * Check if index exists + * + * @param string $tableName Table name + * @param string $indexName Index name + * + * @return boolean + */ + protected function isIndexExists($tableName, $indexName) + { + $indexesOfTable = \XLite\Core\Database::getEM()->getConnection()->getSchemaManager() + ->listTableIndexes($tableName); + return array_reduce( + $indexesOfTable, + function ($carry, $index) use ($indexName){ + return $carry ?: $indexName == $index->getName(); + }, + false + ); + } + + /** + * Get not existed indexes list + * + * @param array $schema Schema + * + * @return array Schema + */ + protected function getNotExistedIndexes(array $schema) + { + $pattern = '/DROP INDEX `?IDX_(.*?)`? ON `?([a-zA-Z0-9-_]+)`?/Ss'; + return array_reduce( + $schema, + function ($carry, $schemaLine) use ($pattern) { + if ( preg_match($pattern, $schemaLine, $matches) ) { + $indexName = $matches[1]; + $tableName = $matches[2]; + + $indexExists = $this->isIndexExists($tableName, 'IDX_' . $indexName); + + if (!$indexExists) { + $carry[] = $indexName; + } + } + return $carry; + }, + array() + ); + } + + /** * Process DB schema * * @param array $schema Schema @@ -1195,6 +1247,14 @@ abstract class ARepo extends \Doctrine\ORM\EntityRepository } } + // Do not drop index if it is not exist in database + $notExistedIndexes = $this->getNotExistedIndexes($schema); + $schema = preg_grep( + '/DROP INDEX `?IDX_(?:' . implode('|', $notExistedIndexes) . ')`? ON `?[a-zA-Z0-9-_]+`?/Ss', + $schema, + PREG_GREP_INVERT + ); + // Assign columns' character sets foreach ($this->columnsCharSets as $column => $charset) { $schema = preg_replace( -- 2.1.1