From 2ac42058c8c733ec149a76e70299429df7b3406a Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Wed, 14 Oct 2026 11:17:37 -0400 Subject: [PATCH 1/4] feat: enhance saved search index migration by adding a method to check for covering indexes --- .../2026_07_27_000000_add_saved_search_indexes.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/database/migrations/2026_07_27_000000_add_saved_search_indexes.php b/database/migrations/2026_07_27_000000_add_saved_search_indexes.php index 765c2693a1..8eabaf0a30 100644 --- a/database/migrations/2026_07_27_000000_add_saved_search_indexes.php +++ b/database/migrations/2026_07_27_000000_add_saved_search_indexes.php @@ -77,7 +77,7 @@ public function down(): void private function addIndex(string $table, string $name, array $columns): void { - if (Schema::hasIndex($table, $name)) { + if (Schema::hasIndex($table, $name) || $this->hasCoveringIndex($table, $columns)) { return; } @@ -86,6 +86,13 @@ private function addIndex(string $table, string $name, array $columns): void }); } + private function hasCoveringIndex(string $table, array $columns): bool + { + return collect(Schema::getIndexes($table))->contains(function (array $index) use ($columns) { + return array_slice($index['columns'], 0, count($columns)) === $columns; + }); + } + private function dropIndex(string $table, string $name): void { if (!Schema::hasIndex($table, $name)) { From 3941ea9f188817a522244f4a5e3dd8cb3282a532 Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Wed, 14 Oct 2026 12:06:08 -0400 Subject: [PATCH 2/4] fix: add foreign key index checks and rollback index management --- ..._07_27_000000_add_saved_search_indexes.php | 77 +++++++++++++++++-- 1 file changed, 71 insertions(+), 6 deletions(-) diff --git a/database/migrations/2026_07_27_000000_add_saved_search_indexes.php b/database/migrations/2026_07_27_000000_add_saved_search_indexes.php index 8eabaf0a30..3733a420e7 100644 --- a/database/migrations/2026_07_27_000000_add_saved_search_indexes.php +++ b/database/migrations/2026_07_27_000000_add_saved_search_indexes.php @@ -77,19 +77,26 @@ public function down(): void private function addIndex(string $table, string $name, array $columns): void { - if (Schema::hasIndex($table, $name) || $this->hasCoveringIndex($table, $columns)) { + if (Schema::hasIndex($table, $name)) { + $this->dropRollbackIndexes($table, $columns); + return; + } + + if ($this->hasCoveringIndex($table, $columns)) { return; } Schema::table($table, function (Blueprint $table) use ($name, $columns) { $table->index($columns, $name); }); + + $this->dropRollbackIndexes($table, $columns); } private function hasCoveringIndex(string $table, array $columns): bool { return collect(Schema::getIndexes($table))->contains(function (array $index) use ($columns) { - return array_slice($index['columns'], 0, count($columns)) === $columns; + return $this->columnsStartWith($index['columns'], $columns); }); } @@ -99,10 +106,68 @@ private function dropIndex(string $table, string $name): void return; } - try { - DB::statement("ALTER TABLE `{$table}` DROP INDEX `{$name}`"); - } catch (Exception $e) { - // Ignore so rollback continues (e.g. index required by a foreign key). + $this->ensureForeignKeysRemainIndexed($table, $name); + + DB::statement("ALTER TABLE `{$table}` DROP INDEX `{$name}`"); + } + + private function ensureForeignKeysRemainIndexed(string $table, string $name): void + { + $indexes = collect(Schema::getIndexes($table)); + $index = $indexes->firstWhere('name', strtolower($name)); + + if (!$index) { + return; + } + + foreach (Schema::getForeignKeys($table) as $foreignKey) { + if (!$this->columnsStartWith($index['columns'], $foreignKey['columns'])) { + continue; + } + + $hasReplacement = $indexes->contains(function (array $candidate) use ($index, $foreignKey) { + return $candidate['name'] !== $index['name'] + && $this->columnsStartWith($candidate['columns'], $foreignKey['columns']); + }); + + if ($hasReplacement) { + continue; + } + + $replacementName = $this->rollbackIndexName($table, $foreignKey['columns']); + + Schema::table($table, function (Blueprint $table) use ($foreignKey, $replacementName) { + $table->index($foreignKey['columns'], $replacementName); + }); + + $indexes = collect(Schema::getIndexes($table)); + } + } + + private function dropRollbackIndexes(string $table, array $columns): void + { + foreach (Schema::getForeignKeys($table) as $foreignKey) { + if (!$this->columnsStartWith($columns, $foreignKey['columns'])) { + continue; + } + + $name = $this->rollbackIndexName($table, $foreignKey['columns']); + + if (Schema::hasIndex($table, $name)) { + DB::statement("ALTER TABLE `{$table}` DROP INDEX `{$name}`"); + } } } + + private function rollbackIndexName(string $table, array $columns): string + { + $suffix = '_four27824_rb_' . substr(sha1(implode(',', $columns)), 0, 8); + + return substr($table, 0, 64 - strlen($suffix)) . $suffix; + } + + private function columnsStartWith(array $indexColumns, array $columns): bool + { + return array_slice($indexColumns, 0, count($columns)) === $columns; + } }; From 9169aab5e4144c3b5d7f325d99c13967fed04519 Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Wed, 14 Oct 2026 12:10:54 -0400 Subject: [PATCH 3/4] test: add migration tests to ensure index creation and rollback functionality --- .../Migrations/AddSavedSearchIndexesTest.php | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 tests/Feature/Migrations/AddSavedSearchIndexesTest.php diff --git a/tests/Feature/Migrations/AddSavedSearchIndexesTest.php b/tests/Feature/Migrations/AddSavedSearchIndexesTest.php new file mode 100644 index 0000000000..a502f441d5 --- /dev/null +++ b/tests/Feature/Migrations/AddSavedSearchIndexesTest.php @@ -0,0 +1,163 @@ + 'category_assignments', + 'name' => 'category_assignments_assignabletyid_categorytyid', + 'columns' => ['assignable_type', 'assignable_id', 'category_type', 'category_id'], + ], + [ + 'table' => 'process_categories', + 'name' => 'process_categories_issystem_id', + 'columns' => ['is_system'], + ], + [ + 'table' => 'process_versions', + 'name' => 'process_versions_processid_draft', + 'columns' => ['process_id', 'draft'], + ], + ]; + + private const MANUAL_INDEXES = [ + [ + 'table' => 'category_assignments', + 'name' => 'ca_assignable_type_id_cat_type_id_idx', + 'columns' => ['assignable_type', 'assignable_id', 'category_type', 'category_id'], + ], + [ + 'table' => 'process_categories', + 'name' => 'pc_is_system_id_idx', + 'columns' => ['is_system', 'id'], + ], + [ + 'table' => 'process_versions', + 'name' => 'pv_process_id_draft_idx', + 'columns' => ['process_id', 'draft'], + ], + ]; + + protected $connectionsToTransact = []; + + public function testUpCreatesTheSavedSearchIndexes(): void + { + $migration = $this->migration(); + $migration->down(); + + try { + $migration->up(); + + foreach (self::TARGET_INDEXES as $index) { + $this->assertIndexColumns($index['table'], $index['name'], $index['columns']); + } + } finally { + $migration->up(); + } + } + + public function testUpIsIdempotent(): void + { + $migration = $this->migration(); + $migration->up(); + $migration->up(); + + foreach (self::TARGET_INDEXES as $index) { + $matches = collect(Schema::getIndexes($index['table'])) + ->where('name', $index['name']); + + $this->assertCount(1, $matches); + $this->assertSame($index['columns'], $matches->first()['columns']); + } + } + + public function testUpUsesEquivalentManualIndexesAndDownPreservesThem(): void + { + $migration = $this->migration(); + $migration->down(); + $this->dropManualIndexes(); + $this->addManualIndexes(); + + try { + $migration->up(); + + foreach (self::TARGET_INDEXES as $index) { + $this->assertFalse( + Schema::hasIndex($index['table'], $index['name']), + "Index {$index['name']} still exists on {$index['table']}." + ); + } + + $migration->down(); + + foreach (self::MANUAL_INDEXES as $index) { + $this->assertTrue(Schema::hasIndex($index['table'], $index['name'])); + } + } finally { + $this->dropManualIndexes(); + $migration->up(); + } + } + + public function testDownRemovesTheCanonicalIndexes(): void + { + $migration = $this->migration(); + $migration->up(); + + try { + $migration->down(); + + foreach (self::TARGET_INDEXES as $index) { + $this->assertFalse( + Schema::hasIndex($index['table'], $index['name']), + "Index {$index['name']} still exists on {$index['table']}." + ); + } + } finally { + $migration->up(); + } + } + + private function migration() + { + return include base_path(self::MIGRATION_PATH); + } + + private function assertIndexColumns(string $table, string $name, array $columns): void + { + $index = collect(Schema::getIndexes($table))->firstWhere('name', $name); + + $this->assertNotNull($index); + $this->assertSame($columns, $index['columns']); + } + + private function addManualIndexes(): void + { + foreach (self::MANUAL_INDEXES as $index) { + Schema::table($index['table'], function (Blueprint $table) use ($index) { + $table->index($index['columns'], $index['name']); + }); + } + } + + private function dropManualIndexes(): void + { + foreach (self::MANUAL_INDEXES as $index) { + if (!Schema::hasIndex($index['table'], $index['name'])) { + continue; + } + + Schema::table($index['table'], function (Blueprint $table) use ($index) { + $table->dropIndex($index['name']); + }); + } + } +} From bc02ca35571a81f30d77ff6410c9b9b5d25080ed Mon Sep 17 00:00:00 2001 From: Miguel Angel Date: Wed, 14 Oct 2026 12:26:41 -0400 Subject: [PATCH 4/4] fix: clean rollback indexes for covering indexes --- ..._07_27_000000_add_saved_search_indexes.php | 1 + .../Migrations/AddSavedSearchIndexesTest.php | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/database/migrations/2026_07_27_000000_add_saved_search_indexes.php b/database/migrations/2026_07_27_000000_add_saved_search_indexes.php index 3733a420e7..66d4c9656c 100644 --- a/database/migrations/2026_07_27_000000_add_saved_search_indexes.php +++ b/database/migrations/2026_07_27_000000_add_saved_search_indexes.php @@ -83,6 +83,7 @@ private function addIndex(string $table, string $name, array $columns): void } if ($this->hasCoveringIndex($table, $columns)) { + $this->dropRollbackIndexes($table, $columns); return; } diff --git a/tests/Feature/Migrations/AddSavedSearchIndexesTest.php b/tests/Feature/Migrations/AddSavedSearchIndexesTest.php index a502f441d5..3dcaaca92c 100644 --- a/tests/Feature/Migrations/AddSavedSearchIndexesTest.php +++ b/tests/Feature/Migrations/AddSavedSearchIndexesTest.php @@ -85,6 +85,7 @@ public function testUpUsesEquivalentManualIndexesAndDownPreservesThem(): void $migration->down(); $this->dropManualIndexes(); $this->addManualIndexes(); + $rollbackIndex = $this->addProcessVersionsRollbackIndex(); try { $migration->up(); @@ -96,12 +97,18 @@ public function testUpUsesEquivalentManualIndexesAndDownPreservesThem(): void ); } + $this->assertFalse( + Schema::hasIndex('process_versions', $rollbackIndex), + "Rollback index {$rollbackIndex} still exists on process_versions." + ); + $migration->down(); foreach (self::MANUAL_INDEXES as $index) { $this->assertTrue(Schema::hasIndex($index['table'], $index['name'])); } } finally { + $this->addProcessVersionsRollbackIndex(); $this->dropManualIndexes(); $migration->up(); } @@ -160,4 +167,26 @@ private function dropManualIndexes(): void }); } } + + private function addProcessVersionsRollbackIndex(): string + { + $table = 'process_versions'; + $columns = ['process_id']; + $name = $this->rollbackIndexName($table, $columns); + + if (!Schema::hasIndex($table, $name)) { + Schema::table($table, function (Blueprint $table) use ($columns, $name) { + $table->index($columns, $name); + }); + } + + return $name; + } + + private function rollbackIndexName(string $table, array $columns): string + { + $suffix = '_four27824_rb_' . substr(sha1(implode(',', $columns)), 0, 8); + + return substr($table, 0, 64 - strlen($suffix)) . $suffix; + } }