AgentProfitAssignmentMigrationTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Tests\Integration;
  3. use Illuminate\Database\Capsule\Manager;
  4. use Illuminate\Database\Migrations\Migration;
  5. use Illuminate\Database\QueryException;
  6. use Illuminate\Database\Schema\Blueprint;
  7. use Illuminate\Foundation\Application;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Facade;
  10. use Illuminate\Support\Facades\Schema;
  11. use PDO;
  12. use PHPUnit\Framework\TestCase;
  13. // Run against a disposable MySQL server via AGENT_MIGRATION_TEST_SOCKET.
  14. // Each test creates its own database; the project .env is never loaded.
  15. class AgentProfitAssignmentMigrationTest extends TestCase
  16. {
  17. private PDO $admin;
  18. private string $database;
  19. private Migration $migration;
  20. protected function setUp(): void
  21. {
  22. $socket = getenv('AGENT_MIGRATION_TEST_SOCKET');
  23. if (!$socket) $this->markTestSkipped('Set AGENT_MIGRATION_TEST_SOCKET to a disposable MySQL server.');
  24. $this->admin = new PDO('mysql:unix_socket=' . $socket, 'root', '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
  25. $database = 'profit_migration_test_' . bin2hex(random_bytes(8));
  26. $this->admin->exec('CREATE DATABASE `' . $database . '`');
  27. $this->database = $database;
  28. Facade::clearResolvedInstances();
  29. $app = new Application(dirname(__DIR__, 2));
  30. Facade::setFacadeApplication($app);
  31. $db = new Manager($app);
  32. $db->addConnection([
  33. 'driver' => 'mysql', 'unix_socket' => $socket, 'database' => $database,
  34. 'username' => 'root', 'password' => '', 'prefix' => 'bot_', 'prefix_indexes' => true,
  35. 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci',
  36. ]);
  37. $app->instance('db', $db->getDatabaseManager());
  38. $app->bind('db.schema', fn () => $db->getConnection()->getSchemaBuilder());
  39. $this->migration = require dirname(__DIR__, 2)
  40. . '/database/migrations/2026_08_28_120000_create_agent_profit_profiles_and_rebate_records.php';
  41. }
  42. protected function tearDown(): void
  43. {
  44. if (isset($this->database)) {
  45. DB::disconnect();
  46. $this->admin->exec('DROP DATABASE `' . $this->database . '`');
  47. Facade::clearResolvedInstances();
  48. }
  49. parent::tearDown();
  50. }
  51. public function test_fresh_migration_can_be_retried_and_rolled_back(): void
  52. {
  53. $this->migration->up();
  54. $indexes = $this->assertAssignmentIndexes();
  55. $this->migration->up();
  56. $this->assertSame($indexes, $this->assertAssignmentIndexes());
  57. $this->assertSame(1, DB::table('agent_profit_commission_profiles')->count());
  58. $this->migration->down();
  59. foreach (['agent_profit_commission_assignments', 'agent_profit_commission_profiles',
  60. 'agent_daily_game_stats', 'agent_rebate_records'] as $table) {
  61. $this->assertFalse(Schema::hasTable($table));
  62. }
  63. }
  64. public function test_original_1059_failure_can_be_resumed_without_losing_rows(): void
  65. {
  66. try {
  67. // Reproduce the original fluent indexes, including the overlong name.
  68. Schema::create('agent_profit_commission_assignments', function (Blueprint $table) {
  69. $table->id();
  70. $table->unsignedBigInteger('agent_id')->index();
  71. $table->unsignedBigInteger('profit_commission_profile_id')->nullable()->index();
  72. $table->date('effective_from')->index();
  73. $table->unique(['agent_id', 'effective_from'], 'uniq_agent_profit_assignment_date');
  74. });
  75. $this->fail('Expected MySQL error 1059 from the original index name.');
  76. } catch (QueryException $exception) {
  77. $this->assertSame(1059, $exception->errorInfo[1]);
  78. }
  79. $this->assertTrue(Schema::hasTable('agent_profit_commission_assignments'));
  80. DB::table('agent_profit_commission_assignments')->insert(['agent_id' => 7, 'effective_from' => '2026-09-15']);
  81. $before = (array) DB::table('agent_profit_commission_assignments')->first();
  82. $this->migration->up();
  83. $this->assertAssignmentIndexes();
  84. $this->migration->up();
  85. $this->assertAssignmentIndexes();
  86. $this->assertSame($before, (array) DB::table('agent_profit_commission_assignments')->first());
  87. }
  88. public function test_equivalent_indexes_are_reused_and_unique_constraint_is_restored(): void
  89. {
  90. Schema::create('agent_profit_commission_assignments', function (Blueprint $table) {
  91. $table->id();
  92. $table->unsignedBigInteger('agent_id')->index('legacy_agent');
  93. $table->unsignedBigInteger('profit_commission_profile_id')->nullable()->index('legacy_profile');
  94. $table->date('effective_from')->index('legacy_effective');
  95. $table->index(['agent_id', 'effective_from'], 'legacy_lookup');
  96. });
  97. $this->migration->up();
  98. $this->migration->up();
  99. $indexes = $this->assertAssignmentIndexes(6);
  100. foreach (['legacy_agent', 'legacy_profile', 'legacy_effective', 'legacy_lookup'] as $index) {
  101. $this->assertArrayHasKey($index, $indexes);
  102. }
  103. $row = ['agent_id' => 7, 'effective_from' => '2026-09-15'];
  104. DB::table('agent_profit_commission_assignments')->insert($row);
  105. try {
  106. DB::table('agent_profit_commission_assignments')->insert($row);
  107. $this->fail('Expected the agent/date unique constraint to reject a duplicate.');
  108. } catch (QueryException $exception) {
  109. $this->assertSame(1062, $exception->errorInfo[1]);
  110. }
  111. }
  112. private function assertAssignmentIndexes(int $count = 5): array
  113. {
  114. $indexes = [];
  115. foreach (DB::select('SHOW INDEX FROM bot_agent_profit_commission_assignments') as $row) {
  116. $this->assertLessThanOrEqual(64, strlen($row->Key_name));
  117. $indexes[$row->Key_name]['columns'][] = $row->Column_name;
  118. $indexes[$row->Key_name]['unique'] = (int) $row->Non_unique === 0;
  119. }
  120. $this->assertCount($count, $indexes);
  121. foreach ([['agent_id'], ['profit_commission_profile_id'], ['effective_from']] as $columns) {
  122. $this->assertContains(['columns' => $columns, 'unique' => false], $indexes);
  123. }
  124. $this->assertContains(['columns' => ['agent_id', 'effective_from'], 'unique' => true], $indexes);
  125. ksort($indexes);
  126. return $indexes;
  127. }
  128. }