forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateUuidsForTable.php
More file actions
62 lines (53 loc) · 1.72 KB
/
GenerateUuidsForTable.php
File metadata and controls
62 lines (53 loc) · 1.72 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
55
56
57
58
59
60
61
62
<?php
namespace ProcessMaker\Jobs;
use DB;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Str;
class GenerateUuidsForTable implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $table;
private $field;
private $primary;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(String $table, String $field = 'uuid', String $primary = 'id')
{
$this->table = $table;
$this->field = $field;
$this->primary = $primary;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$query = DB::table($this->table)
->select($this->primary, $this->field)
->orderBy($this->primary)->chunk(1000, function ($records) {
// Assign UUIDs if needed
foreach ($records as &$record) {
if (!$record->{$this->field} || trim($record->{$this->field}) === '') {
$record->{$this->field} = (string) Str::orderedUuid();
}
}
// Set UUIDs in transaction for performance reasons
DB::transaction(function () use ($records) {
foreach ($records as &$record) {
DB::table($this->table)
->where($this->primary, $record->{$this->primary})
->update(['uuid' => $record->{$this->field}]);
}
});
});
}
}