Files
mtdb_movie/common/Database/Traits/AddsIndexToExistingTable.php
maher 703f50a09d
Some checks failed
Build / run (push) Has been cancelled
first commit
2025-10-29 11:42:25 +01:00

21 lines
615 B
PHP
Executable File

<?php
namespace Common\Database\Traits;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
trait AddsIndexToExistingTable
{
protected function addIndexIfDoesNotExist(Blueprint $table, string $column) {
$prefix = Schema::getConnection()->getTablePrefix();
$sm = Schema::getConnection()->getDoctrineSchemaManager();
$tableName = "{$prefix}{$table->getTable()}";
$indexesFound = $sm->listTableIndexes($tableName);
if (!array_key_exists("{$tableName}_{$column}_index", $indexesFound)) {
$table->index($column);
}
}
}