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..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 @@ -78,12 +78,27 @@ public function down(): void private function addIndex(string $table, string $name, array $columns): void { if (Schema::hasIndex($table, $name)) { + $this->dropRollbackIndexes($table, $columns); + return; + } + + if ($this->hasCoveringIndex($table, $columns)) { + $this->dropRollbackIndexes($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 $this->columnsStartWith($index['columns'], $columns); + }); } private function dropIndex(string $table, string $name): void @@ -92,10 +107,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; + } }; diff --git a/tests/Feature/Migrations/AddSavedSearchIndexesTest.php b/tests/Feature/Migrations/AddSavedSearchIndexesTest.php new file mode 100644 index 0000000000..3dcaaca92c --- /dev/null +++ b/tests/Feature/Migrations/AddSavedSearchIndexesTest.php @@ -0,0 +1,192 @@ + '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(); + $rollbackIndex = $this->addProcessVersionsRollbackIndex(); + + 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']}." + ); + } + + $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(); + } + } + + 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']); + }); + } + } + + 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; + } +}