|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Doctrine\DBAL\Schema\Index; |
|
4
|
|
|
use Illuminate\Database\Schema\Blueprint; |
|
5
|
|
|
use Illuminate\Support\Facades\Schema; |
|
6
|
|
|
|
|
7
|
|
|
/* |
|
8
|
|
|
* Create an index if it doesn't exist yet |
|
9
|
|
|
*/ |
|
10
|
|
|
Blueprint::macro('createIndexIfNotExists', function ($columns, $name = null, $connection = null) { |
|
11
|
|
|
return $this->hasIndex($columns, $connection) |
|
|
|
|
|
|
12
|
|
|
? $this |
|
13
|
|
|
: $this->index($columns, $name ?: $this->createIndexNameByColumns($columns)); |
|
14
|
|
|
}); |
|
15
|
|
|
|
|
16
|
|
|
/* |
|
17
|
|
|
* Check if an index exists |
|
18
|
|
|
*/ |
|
19
|
|
|
Blueprint::macro('hasIndex', function ($columns) { |
|
20
|
|
|
return $this->getIndexNameByColumns($columns) !== null; |
|
|
|
|
|
|
21
|
|
|
}); |
|
22
|
|
|
|
|
23
|
|
|
/* |
|
24
|
|
|
* Get an index name by columns |
|
25
|
|
|
*/ |
|
26
|
|
|
Blueprint::macro('getIndexNameByColumns', function ($columns) { |
|
27
|
|
|
if (is_string($columns)) { |
|
28
|
|
|
$columns = [$columns]; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$schemaManager = Schema::getConnection()->getDoctrineSchemaManager(); |
|
32
|
|
|
$indices = $schemaManager->listTableIndexes($this->getTable()); |
|
|
|
|
|
|
33
|
|
|
$filteredIndices = collect($indices)->filter(function (Index $index) use ($columns) { |
|
34
|
|
|
return compare_array($index->getColumns(), $columns); |
|
35
|
|
|
}); |
|
36
|
|
|
|
|
37
|
|
|
if ($filteredIndices->isNotEmpty()) { |
|
38
|
|
|
return $filteredIndices->keys()->first(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return null; |
|
42
|
|
|
}); |
|
43
|
|
|
|
|
44
|
|
|
/* |
|
45
|
|
|
* Create an index name based on columns |
|
46
|
|
|
*/ |
|
47
|
|
|
Blueprint::macro('createIndexNameByColumns', function ($columns) { |
|
48
|
|
|
if (is_string($columns)) { |
|
49
|
|
|
$columns = [$columns]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$index = $this->createIndexName('index', $columns); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
if (strlen($index) > 64) { |
|
55
|
|
|
$index = implode('_', $columns); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $index; |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
/* |
|
62
|
|
|
* Drop an index if it exists |
|
63
|
|
|
*/ |
|
64
|
|
|
Blueprint::macro('dropIndexIfExists', function ($columns) { |
|
65
|
|
|
if ($this->hasIndex($columns)) { |
|
|
|
|
|
|
66
|
|
|
return $this->dropIndex($this->getIndexNameByColumns($columns)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
}); |
|
71
|
|
|
|
|
72
|
|
|
if (!function_exists('compare_array')) { |
|
73
|
|
|
/** |
|
74
|
|
|
* @param $arrayA |
|
75
|
|
|
* @param $arrayB |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
function compare_array($arrayA, $arrayB): bool |
|
79
|
|
|
{ |
|
80
|
|
|
return ( |
|
81
|
|
|
is_array($arrayA) |
|
82
|
|
|
&& is_array($arrayB) |
|
83
|
|
|
&& count($arrayA) == count($arrayB) |
|
84
|
|
|
&& array_diff($arrayA, $arrayB) === array_diff($arrayB, $arrayA) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |