|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Feature\V2; |
| 6 | + |
| 7 | +use Illuminate\Database\QueryException; |
| 8 | +use Illuminate\Database\Schema\Blueprint; |
| 9 | +use Illuminate\Support\Facades\DB; |
| 10 | +use Illuminate\Support\Facades\Schema; |
| 11 | +use Illuminate\Support\Str; |
| 12 | +use Tests\TestCase; |
| 13 | +use Workflow\Support\WorkflowMigration; |
| 14 | +use Workflow\V2\Models\WorkflowFailure; |
| 15 | + |
| 16 | +final class V2FailureMessageStorageMigrationTest extends TestCase |
| 17 | +{ |
| 18 | + public function testUpgradeIsAdditiveAndPackageRollbackRetainsCanonicalEvidence(): void |
| 19 | + { |
| 20 | + $legacy = $this->legacyRow(); |
| 21 | + $migration = $this->migration(); |
| 22 | + $migration->up(); |
| 23 | + $connection = Schema::getConnection(); |
| 24 | + $row = $connection->table('workflow_failures')->where('id', $legacy['id'])->first(); |
| 25 | + $this->assertNull($row->message_full); |
| 26 | + unset($row->message_full); |
| 27 | + $this->assertSame($legacy, (array) $row); |
| 28 | + |
| 29 | + $message = str_repeat("\0界🦀", 10_000); |
| 30 | + $failure = WorkflowFailure::query()->findOrFail($legacy['id']); |
| 31 | + $failure->message = $message; |
| 32 | + $failure->save(); |
| 33 | + $this->assertSame($message, $failure->fresh()->message); |
| 34 | + |
| 35 | + $migration->down(); |
| 36 | + $this->assertTrue(Schema::hasColumn('workflow_failures', 'message_full')); |
| 37 | + $this->assertSame($message, $failure->fresh()->message); |
| 38 | + $migration->up(); |
| 39 | + $this->assertSame($message, $failure->fresh()->message); |
| 40 | + $this->assertSame(1, WorkflowFailure::query()->count()); |
| 41 | + } |
| 42 | + |
| 43 | + public function testHeldWriterBoundsMetadataWaitAndRestoresConnectionPolicy(): void |
| 44 | + { |
| 45 | + $connection = Schema::getConnection(); |
| 46 | + $driver = $connection->getDriverName(); |
| 47 | + if (! in_array($driver, ['mysql', 'mariadb', 'pgsql'], true)) { |
| 48 | + $this->markTestSkipped('Network database metadata-lock control.'); |
| 49 | + } |
| 50 | + |
| 51 | + $legacy = $this->legacyRow(); |
| 52 | + $timeoutQuery = $driver === 'pgsql' ? 'SHOW lock_timeout' : 'SELECT @@SESSION.lock_wait_timeout AS value'; |
| 53 | + $previous = (array) $connection->selectOne($timeoutQuery); |
| 54 | + $blockerName = 'failure_message_metadata_blocker'; |
| 55 | + config()->set('database.connections.' . $blockerName, $connection->getConfig()); |
| 56 | + DB::purge($blockerName); |
| 57 | + $blocker = DB::connection($blockerName); |
| 58 | + $blocker->beginTransaction(); |
| 59 | + $blocker->table('workflow_failures')->where('id', $legacy['id'])->lockForUpdate()->first(); |
| 60 | + $migration = $this->migration(); |
| 61 | + $started = microtime(true); |
| 62 | + |
| 63 | + try { |
| 64 | + try { |
| 65 | + $migration->up(); |
| 66 | + $this->fail('The held writer must prevent an unbounded metadata upgrade.'); |
| 67 | + } catch (QueryException) { |
| 68 | + $elapsed = microtime(true) - $started; |
| 69 | + $this->assertGreaterThanOrEqual(0.8, $elapsed); |
| 70 | + $this->assertLessThan(3.0, $elapsed); |
| 71 | + $this->assertSame($previous, (array) $connection->selectOne($timeoutQuery)); |
| 72 | + $this->assertFalse(Schema::hasColumn('workflow_failures', 'message_full')); |
| 73 | + $this->assertSame($legacy, (array) $connection->table('workflow_failures')->first()); |
| 74 | + } |
| 75 | + } finally { |
| 76 | + $blocker->rollBack(); |
| 77 | + DB::purge($blockerName); |
| 78 | + $migration->up(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public function testUnsupportedMySqlLayoutNeverFallsBackToCopyingTheTable(): void |
| 83 | + { |
| 84 | + $connection = Schema::getConnection(); |
| 85 | + if ($connection->getDriverName() !== 'mysql') { |
| 86 | + $this->markTestSkipped('MySQL compressed-layout control.'); |
| 87 | + } |
| 88 | + |
| 89 | + $version = (array) $connection->selectOne('SELECT VERSION() AS version'); |
| 90 | + if (str_contains($version['version'], 'MariaDB')) { |
| 91 | + $this->markTestSkipped('MariaDB has a separate instant-DDL support contract.'); |
| 92 | + } |
| 93 | + |
| 94 | + $legacy = $this->legacyRow(); |
| 95 | + $table = $connection->getQueryGrammar()->wrapTable('workflow_failures'); |
| 96 | + // This is disposable fixture preparation, not a rolling-upgrade operation. |
| 97 | + $connection->statement("ALTER TABLE {$table} ROW_FORMAT=COMPRESSED"); |
| 98 | + $migration = $this->migration(); |
| 99 | + |
| 100 | + try { |
| 101 | + try { |
| 102 | + $migration->up(); |
| 103 | + $this->fail('The unsupported instant operation must fail without a copying fallback.'); |
| 104 | + } catch (QueryException $error) { |
| 105 | + $this->assertContains($error->errorInfo[1], [1845, 1846]); |
| 106 | + $this->assertFalse(Schema::hasColumn('workflow_failures', 'message_full')); |
| 107 | + $this->assertSame($legacy, (array) $connection->table('workflow_failures')->first()); |
| 108 | + } |
| 109 | + } finally { |
| 110 | + $connection->statement("ALTER TABLE {$table} ROW_FORMAT=DYNAMIC"); |
| 111 | + $migration->up(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @return array<string, mixed> |
| 117 | + */ |
| 118 | + private function legacyRow(): array |
| 119 | + { |
| 120 | + Schema::table('workflow_failures', static function (Blueprint $table): void { |
| 121 | + $table->dropColumn('message_full'); |
| 122 | + }); |
| 123 | + $connection = Schema::getConnection(); |
| 124 | + $connection->table('workflow_failures')->insert([ |
| 125 | + 'id' => (string) Str::ulid(), |
| 126 | + 'workflow_run_id' => (string) Str::ulid(), |
| 127 | + 'source_kind' => 'activity_execution', |
| 128 | + 'source_id' => 'original-attempt', |
| 129 | + 'propagation_kind' => 'activity', |
| 130 | + 'failure_category' => null, |
| 131 | + 'non_retryable' => false, |
| 132 | + 'handled' => false, |
| 133 | + 'exception_class' => 'RuntimeException', |
| 134 | + 'message' => 'unchanged legacy message', |
| 135 | + 'file' => '/fixture/activity.php', |
| 136 | + 'line' => 7, |
| 137 | + 'trace_preview' => null, |
| 138 | + 'created_at' => '2026-10-23 23:00:00', |
| 139 | + 'updated_at' => '2026-10-23 23:00:00', |
| 140 | + ]); |
| 141 | + |
| 142 | + return (array) $connection->table('workflow_failures')->first(); |
| 143 | + } |
| 144 | + |
| 145 | + private function migration(): WorkflowMigration |
| 146 | + { |
| 147 | + return require __DIR__ . '/../../../src/migrations/2026_10_23_000100_add_full_failure_message_storage.php'; |
| 148 | + } |
| 149 | +} |
0 commit comments