Skip to content

Commit dd414a5

Browse files
committed
ENH Allow disabling/dropping indexes via config
1 parent 83920d6 commit dd414a5

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

src/ORM/Connect/DBSchemaManager.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public function transAlterField($table, $field, $schema)
314314
*
315315
* @param string $table Name of the table to hold this index
316316
* @param string $index Name of the index to update
317-
* @param array $schema Already parsed index specification
317+
* @param array|false $schema Already parsed index specification, or false to drop the index.
318318
*/
319319
public function transAlterIndex($table, $index, $schema)
320320
{
@@ -431,7 +431,11 @@ public function requireTable(
431431
// Create custom indexes
432432
if ($indexSchema) {
433433
foreach ($indexSchema as $indexName => $indexSpec) {
434-
$this->requireIndex($table, $indexName, $indexSpec);
434+
if ($indexSpec === false) {
435+
$this->dontRequireIndex($table, $indexName);
436+
} else {
437+
$this->requireIndex($table, $indexName, $indexSpec);
438+
}
435439
}
436440
}
437441

@@ -529,6 +533,28 @@ public function requireIndex($table, $index, $spec)
529533
}
530534
}
531535

536+
public function dontRequireIndex(string $table, string $index): void
537+
{
538+
// Skip if this is a new table, since it just won't have that index
539+
$newTable = !isset($this->tableList[strtolower($table)]);
540+
if ($newTable) {
541+
return;
542+
}
543+
544+
$indexKey = $this->indexKey($table, $index, []);
545+
$indexList = $this->indexList($table);
546+
// Drop the index if it exists
547+
if (isset($indexList[$indexKey])) {
548+
$oldSpec = $indexList[$indexKey];
549+
$oldSpecString = $this->convertIndexSpec($oldSpec);
550+
$this->transAlterIndex($table, $index, false);
551+
$this->alterationMessage(
552+
"Index $table.$index: dropped <i class=\"build-info-before\">(from $oldSpecString)</i>",
553+
"deleted"
554+
);
555+
}
556+
}
557+
532558
/**
533559
* Splits a spec string safely, considering quoted columns, whitespace,
534560
* and cleaning brackets

src/ORM/Connect/MySQLSchemaManager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ public function alterTable(
9393
if ($alteredIndexes) {
9494
foreach ($alteredIndexes as $k => $v) {
9595
$alterList[] = "DROP INDEX \"$k\"";
96-
$alterList[] = "ADD " . $this->getIndexSqlDefinition($k, $v);
96+
if ($v !== false) {
97+
$alterList[] = "ADD " . $this->getIndexSqlDefinition($k, $v);
98+
}
9799
}
98100
}
99101

src/ORM/DataObjectSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ protected function buildCustomDatabaseIndexes($class)
629629
var_export($indexSpec['columns'], true)
630630
));
631631
}
632-
} else {
632+
} elseif ($indexSpec !== false) {
633633
$indexSpec = [
634634
'type' => 'index',
635635
'columns' => [$indexName],

0 commit comments

Comments
 (0)