Skip to content

Commit b935fe3

Browse files
committed
final fix and add logging
Signed-off-by: dartcafe <github@dartcafe.de>
1 parent 443b600 commit b935fe3

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

lib/Db/V10/IndexManager.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ public function createIndex(string $tableName, string $indexName, array $columns
360360

361361
// Check by name: if a named index already exists, verify its columns
362362
if ($hasName && $table->hasIndex($indexName)) {
363-
if ($this->columnsMatch($table->getIndex($indexName)->getColumns(), $columns)) {
363+
if ($this->columnsMatch($table->getIndex($indexName)->getUnquotedColumns(), $columns)) {
364364
// Named index with same columns already exists, skip creation and return success message
365365
return ucfirst($type) . ' ' . $indexName . ' with correct configuration already exists in ' . $tableName . '. Skip creation.';
366366
}
@@ -371,7 +371,7 @@ public function createIndex(string $tableName, string $indexName, array $columns
371371

372372
// Check by columns: look further for any existing index with same uniqueness and same columns
373373
foreach ($table->getIndexes() as $index) {
374-
if ($index->isUnique() === $unique && $this->columnsMatch($index->getColumns(), $columns)) {
374+
if ($index->isUnique() === $unique && $this->columnsMatch($index->getUnquotedColumns(), $columns)) {
375375
if (!$hasName) {
376376
// If index is unnamed and index with matching configuraton is found, skip creation and return success message
377377
return ucfirst($type) . ' for ' . json_encode($columns) . ' already exists as ' . $index->getName() . ' in ' . $tableName;
@@ -383,7 +383,8 @@ public function createIndex(string $tableName, string $indexName, array $columns
383383
}
384384
}
385385

386-
// now create the new index, either, because it did not exist at all, or because the existing one(s) were dropped due to mismatch
386+
// now create the new index, either, because it did not exist at all, or
387+
// because the existing one(s) were dropped due to mismatch
387388
try {
388389
if ($unique) {
389390
$table->addUniqueIndex($columns, $hasName ? $indexName : null);
@@ -392,7 +393,25 @@ public function createIndex(string $tableName, string $indexName, array $columns
392393
}
393394
} catch (IndexAlreadyExists) {
394395
// Catch Exception and treat the index as existing
395-
// Especially catches pgsql error in situations, where the prior check did not detect the existing index
396+
// Especially catches pgsql error in situations, where the prior
397+
// check did not detect the existing index
398+
399+
// Let's add some details about the existing indices to the log
400+
// collect expected and actual index configurations
401+
$indexDump = [];
402+
foreach ($table->getIndexes() as $idx) {
403+
$indexDump[] = $idx->getName() . '[' . implode(',', $idx->getColumns()) . ']' . ($idx->isUnique() ? ' UNIQUE' : '');
404+
}
405+
406+
// Log the exception with details about the existing indices as a warning log entry
407+
$this->logger->warning(
408+
'IndexAlreadyExists caught for {table}: expected columns {expected}, existing indices: {indices}',
409+
[
410+
'table' => $prefixedTable,
411+
'expected' => json_encode($columns),
412+
'indices' => implode(' | ', $indexDump),
413+
]
414+
);
396415
return ucfirst($type) . ' for ' . json_encode($columns) . ' already exists in ' . $tableName . ' (detected via exception). Skip creation.';
397416
}
398417
$message[] = 'Added ' . $type . ' ' . ($hasName ? $indexName : '(auto)') . ' for ' . json_encode($columns) . ' to ' . $tableName;

0 commit comments

Comments
 (0)