|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Migrations; |
| 4 | + |
| 5 | +use Illuminate\Database\Events\QueryExecuted; |
| 6 | +use Illuminate\Support\Facades\DB; |
| 7 | +use Illuminate\Support\Facades\Schema; |
| 8 | +use Tests\TestCase; |
| 9 | + |
| 10 | +class AddTaskPaginationIndexesTest extends TestCase |
| 11 | +{ |
| 12 | + private const MIGRATION_PATH = 'database/migrations/2026_08_17_000000_add_task_pagination_indexes.php'; |
| 13 | + |
| 14 | + private const COUNT_INDEX = 'process_request_tokens_prt_completed_task_count'; |
| 15 | + |
| 16 | + private const PAGE_INDEX = 'process_request_tokens_prt_task_created'; |
| 17 | + |
| 18 | + protected $connectionsToTransact = []; |
| 19 | + |
| 20 | + public function testUpCreatesBothIndexesAndRefreshesStatistics(): void |
| 21 | + { |
| 22 | + $migration = $this->migration(); |
| 23 | + $migration->down(); |
| 24 | + |
| 25 | + try { |
| 26 | + $queries = $this->captureQueries(fn () => $migration->up()); |
| 27 | + |
| 28 | + $this->assertTrue(Schema::hasIndex('process_request_tokens', self::COUNT_INDEX)); |
| 29 | + $this->assertTrue(Schema::hasIndex('process_request_tokens', self::PAGE_INDEX)); |
| 30 | + $this->assertStatisticsWereRefreshed($queries); |
| 31 | + } finally { |
| 32 | + $migration->up(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public function testUpIsIdempotentAndStillRefreshesStatistics(): void |
| 37 | + { |
| 38 | + $migration = $this->migration(); |
| 39 | + $migration->up(); |
| 40 | + |
| 41 | + $queries = $this->captureQueries(fn () => $migration->up()); |
| 42 | + |
| 43 | + $this->assertTrue(Schema::hasIndex('process_request_tokens', self::COUNT_INDEX)); |
| 44 | + $this->assertTrue(Schema::hasIndex('process_request_tokens', self::PAGE_INDEX)); |
| 45 | + $this->assertStatisticsWereRefreshed($queries); |
| 46 | + $this->assertFalse(collect($queries)->contains( |
| 47 | + fn (string $query) => str_starts_with($query, 'ALTER TABLE `process_request_tokens`') |
| 48 | + )); |
| 49 | + } |
| 50 | + |
| 51 | + public function testDownRemovesBothIndexesAndRefreshesStatistics(): void |
| 52 | + { |
| 53 | + $migration = $this->migration(); |
| 54 | + $migration->up(); |
| 55 | + |
| 56 | + try { |
| 57 | + $queries = $this->captureQueries(fn () => $migration->down()); |
| 58 | + |
| 59 | + $this->assertFalse(Schema::hasIndex('process_request_tokens', self::COUNT_INDEX)); |
| 60 | + $this->assertFalse(Schema::hasIndex('process_request_tokens', self::PAGE_INDEX)); |
| 61 | + $this->assertStatisticsWereRefreshed($queries); |
| 62 | + } finally { |
| 63 | + $migration->up(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private function migration() |
| 68 | + { |
| 69 | + return include base_path(self::MIGRATION_PATH); |
| 70 | + } |
| 71 | + |
| 72 | + private function captureQueries(callable $callback): array |
| 73 | + { |
| 74 | + $queries = []; |
| 75 | + |
| 76 | + DB::listen(function (QueryExecuted $query) use (&$queries) { |
| 77 | + $queries[] = $query->sql; |
| 78 | + }); |
| 79 | + |
| 80 | + $callback(); |
| 81 | + |
| 82 | + return $queries; |
| 83 | + } |
| 84 | + |
| 85 | + private function assertStatisticsWereRefreshed(array $queries): void |
| 86 | + { |
| 87 | + $this->assertContains('ANALYZE TABLE `process_request_tokens`', $queries); |
| 88 | + } |
| 89 | +} |
0 commit comments