|
|
@@ -35,17 +35,18 @@ return new class extends Migration
|
|
|
if (!Schema::hasTable('agent_flow_commission_assignments')) {
|
|
|
Schema::create('agent_flow_commission_assignments', function (Blueprint $table) {
|
|
|
$table->id();
|
|
|
- $table->unsignedBigInteger('agent_id')->index();
|
|
|
- $table->unsignedBigInteger('flow_commission_profile_id')->nullable()->index();
|
|
|
+ $table->unsignedBigInteger('agent_id');
|
|
|
+ $table->unsignedBigInteger('flow_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')->index();
|
|
|
+ $table->date('effective_from');
|
|
|
$table->unsignedBigInteger('created_by')->nullable();
|
|
|
$table->timestamps();
|
|
|
- $table->unique(['agent_id', 'effective_from'], 'uniq_agent_flow_profile_effective');
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ $this->ensureAssignmentIndexes();
|
|
|
+
|
|
|
$this->backfillAgents($defaultId);
|
|
|
}
|
|
|
|
|
|
@@ -71,6 +72,43 @@ return new class extends Migration
|
|
|
return $defaultId;
|
|
|
}
|
|
|
|
|
|
+ private function ensureAssignmentIndexes(): void
|
|
|
+ {
|
|
|
+ $indexes = [
|
|
|
+ 'idx_agent_flow_assignment_agent' => ['agent_id', false],
|
|
|
+ 'idx_agent_flow_assignment_profile' => ['flow_commission_profile_id', false],
|
|
|
+ 'idx_agent_flow_assignment_effective' => ['effective_from', false],
|
|
|
+ 'uniq_agent_flow_profile_effective' => [['agent_id', 'effective_from'], true],
|
|
|
+ ];
|
|
|
+ foreach ($indexes as $name => [$columns, $unique]) {
|
|
|
+ $columns = (array)$columns;
|
|
|
+ if ($this->indexExists('agent_flow_commission_assignments', $name, $columns, $unique)) continue;
|
|
|
+ Schema::table('agent_flow_commission_assignments', function (Blueprint $table) use ($columns, $name, $unique) {
|
|
|
+ $unique ? $table->unique($columns, $name) : $table->index($columns, $name);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function indexExists(string $table, string $index, array $columns, bool $unique): bool
|
|
|
+ {
|
|
|
+ $physicalTable = DB::getTablePrefix() . $table;
|
|
|
+ $rows = DB::select(
|
|
|
+ 'SELECT index_name, column_name, seq_in_index, non_unique FROM information_schema.statistics '
|
|
|
+ . 'WHERE table_schema = DATABASE() AND table_name = ? ORDER BY index_name, seq_in_index',
|
|
|
+ [$physicalTable]
|
|
|
+ );
|
|
|
+ $grouped = [];
|
|
|
+ foreach ($rows as $row) {
|
|
|
+ $grouped[$row->index_name]['columns'][] = (string)$row->column_name;
|
|
|
+ $grouped[$row->index_name]['unique'] = (int)$row->non_unique === 0;
|
|
|
+ }
|
|
|
+ foreach ($grouped as $name => $definition) {
|
|
|
+ if ($name === $index) return true;
|
|
|
+ if ($definition['columns'] === $columns && (!$unique || $definition['unique'])) return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
private function backfillAgents(int $defaultId): void
|
|
|
{
|
|
|
if (!Schema::hasTable('agents') || !Schema::hasTable('agent_flow_commission_assignments')) return;
|