forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMigrationHelper.php
More file actions
48 lines (43 loc) · 1.37 KB
/
MigrationHelper.php
File metadata and controls
48 lines (43 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
<?php
namespace ProcessMaker\ImportExport;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
class MigrationHelper
{
public function addUuidsToTables($tables)
{
foreach ($tables as $table) {
if (!Schema::hasColumn($table, 'uuid')) {
Schema::table($table, function (Blueprint $table) {
$table->uuid('uuid')->after('id')->unique()->nullable();
});
}
}
}
public function removeUuidsFromTables($tables)
{
foreach ($tables as $table) {
if (Schema::hasColumn($table, 'uuid')) {
Schema::table($table, function (Blueprint $table) {
$table->dropColumn('uuid');
});
}
}
}
public function populateUuids($tables)
{
foreach ($tables as $table) {
\DB::table($table)
->select('id')
->where('uuid', '=', null)
->orderBy('id')->chunkById(1000, function ($rows) use ($table) {
$count = count($rows);
foreach ($rows as $row) {
$uuid = (string) Str::orderedUuid();
\DB::statement("update `$table` set `uuid` = ? where `id` = ?", [$uuid, $row->id]);
}
});
}
}
}