63 lines
1.8 KiB
PHP
63 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Database\Schema;
|
|
|
|
use Illuminate\Database\Schema\Blueprint as BaseBlueprint;
|
|
|
|
class Blueprint extends BaseBlueprint
|
|
{
|
|
private const MAX_INDEX_NAME_LENGTH = 64;
|
|
private const MAX_FRAGMENT_INDEX_LEGNTH = 4;
|
|
private const MIN_FRAGMENT_INDEX_LEGNTH = 3;
|
|
|
|
/**
|
|
* Create a default index name for the table.
|
|
*
|
|
* @param string $type
|
|
* @param array $columns
|
|
* @return string
|
|
*/
|
|
protected function createIndexName($type, array $columns)
|
|
{
|
|
$index = $this->getReducedIndexName($type, $columns, self::MAX_FRAGMENT_INDEX_LEGNTH);
|
|
|
|
return str_replace(['-', '.'], '_', $index);
|
|
}
|
|
|
|
private function getReducedIndexName($type, array $columns, int $fragmentLength)
|
|
{
|
|
$table = null;
|
|
|
|
foreach($columns as $key => $value) {
|
|
$column = null;
|
|
|
|
foreach(explode('_', $value) as $explode) {
|
|
$column .= substr($explode, 0, $fragmentLength) . '_';
|
|
}
|
|
|
|
$columns[$key] = rtrim($column, '_');
|
|
}
|
|
|
|
foreach(explode('_', $this->table) as $explode) {
|
|
$table .= substr($explode, 0, $fragmentLength) . '_';
|
|
}
|
|
|
|
$index = strtolower($this->prefix.rtrim($table, '_').'_'.implode('_', $columns).'_'.$type);
|
|
|
|
if (strlen($index) > self::MAX_INDEX_NAME_LENGTH) {
|
|
|
|
$newFragmentLength = $fragmentLength - 1;
|
|
|
|
if ($newFragmentLength < self::MIN_FRAGMENT_INDEX_LEGNTH) {
|
|
|
|
throw new \Exception("An error has occurred while reducing the length of the index. "
|
|
. "Please, name it manually.\nThe last try result in: {$index}.");
|
|
}
|
|
|
|
return $this->getReducedIndexName($type, $columns, $newFragmentLength);
|
|
}
|
|
|
|
return $index;
|
|
}
|
|
}
|