-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathJsonColumnIndex.php
More file actions
54 lines (42 loc) · 1.37 KB
/
JsonColumnIndex.php
File metadata and controls
54 lines (42 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace ProcessMaker;
use DB;
use Illuminate\Support\Facades\Log;
class JsonColumnIndex
{
public function add(string $table, string $column, string $path)
{
$indexName = $column . '_' . $path;
if ($this->indexExists($table, $indexName)) {
return false;
}
$sql = <<<SQL
ALTER TABLE `{$table}` ADD INDEX `{$indexName}` ((
LEFT({$column}->>"{$path}", 255) COLLATE utf8mb4_bin
)) USING BTREE;
SQL;
if (!config('database.enable_index_json_columns')) {
Log::warning('Indexing JSON columns is disabled. The following index was not created: ' . $sql);
return false;
}
return DB::statement($sql);
}
public function indexExists(string $table, string $name)
{
return $this->listIndexes($table)->contains(function ($index) use ($name) {
return $index->Key_name === $name;
});
}
public function listIndexes(string $table)
{
return collect(DB::select("SHOW INDEXES FROM {$table}"));
}
public function remove(string $table, string $column, string $path)
{
$indexName = $column . '_' . $path;
if (!$this->indexExists($table, $indexName)) {
return;
}
return DB::statement("ALTER TABLE `{$table}` DROP INDEX `{$indexName}`");
}
}