Ver código fonte

索引名过长

doge 23 horas atrás
pai
commit
4ccca61115

+ 45 - 12
database/migrations/2026_08_28_120000_create_agent_profit_profiles_and_rebate_records.php

@@ -69,18 +69,51 @@ return new class extends Migration {
 
     private function createProfitAssignments(): void
     {
-        if (Schema::hasTable('agent_profit_commission_assignments')) return;
-        Schema::create('agent_profit_commission_assignments', function (Blueprint $table) {
-            $table->id();
-            $table->unsignedBigInteger('agent_id')->index();
-            $table->unsignedBigInteger('profit_commission_profile_id')->nullable()->index();
-            $table->string('profile_name', 128)->default('');
-            foreach (self::RATE_FIELDS as $field) $table->decimal($field, 8, 4)->default(0);
-            $table->date('effective_from')->index();
-            $table->unsignedBigInteger('created_by')->nullable();
-            $table->timestamps();
-            $table->unique(['agent_id', 'effective_from'], 'uniq_agent_profit_assignment_date');
-        });
+        if (!Schema::hasTable('agent_profit_commission_assignments')) {
+            Schema::create('agent_profit_commission_assignments', function (Blueprint $table) {
+                $table->id();
+                $table->unsignedBigInteger('agent_id');
+                $table->unsignedBigInteger('profit_commission_profile_id')->nullable();
+                $table->string('profile_name', 128)->default('');
+                foreach (self::RATE_FIELDS as $field) $table->decimal($field, 8, 4)->default(0);
+                $table->date('effective_from');
+                $table->unsignedBigInteger('created_by')->nullable();
+                $table->timestamps();
+            });
+        }
+
+        // MySQL can leave the table and some indexes behind when a later index fails.
+        $this->ensureAssignmentIndexes();
+    }
+
+    private function ensureAssignmentIndexes(): void
+    {
+        $rows = DB::select(
+            'SELECT INDEX_NAME AS index_name, COLUMN_NAME AS column_name, NON_UNIQUE AS non_unique '
+            . 'FROM information_schema.statistics '
+            . 'WHERE table_schema = DATABASE() AND table_name = ? ORDER BY index_name, seq_in_index',
+            [DB::getTablePrefix() . 'agent_profit_commission_assignments']
+        );
+        $existing = [];
+        foreach ($rows as $row) {
+            $existing[$row->index_name]['columns'][] = (string) $row->column_name;
+            $existing[$row->index_name]['unique'] = (int) $row->non_unique === 0;
+        }
+
+        $indexes = [
+            'idx_agent_profit_assignment_agent' => [['agent_id'], false],
+            'idx_agent_profit_assignment_profile' => [['profit_commission_profile_id'], false],
+            'idx_agent_profit_assignment_effective' => [['effective_from'], false],
+            'uniq_agent_profit_assignment_date' => [['agent_id', 'effective_from'], true],
+        ];
+        foreach ($indexes as $name => [$columns, $unique]) {
+            foreach ($existing as $definition) {
+                if ($definition['columns'] === $columns && (!$unique || $definition['unique'])) continue 2;
+            }
+            Schema::table('agent_profit_commission_assignments', function (Blueprint $table) use ($columns, $name, $unique) {
+                $unique ? $table->unique($columns, $name) : $table->index($columns, $name);
+            });
+        }
     }
 
     private function migrateLegacyAgentRates(int $defaultId): void