|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Uccello\Core\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Uccello\Core\Models\Entity; |
|
7
|
|
|
use Uccello\Core\Models\Module; |
|
8
|
|
|
|
|
9
|
|
|
class GenerateUuidCacheCommand extends Command |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The name and signature of the console command. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $signature = 'uccello:uuid'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The console command description. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $description = 'Purge uuids linked to deleted records and generate cache for uuid'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Create a new command instance. |
|
27
|
|
|
* |
|
28
|
|
|
* @return void |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct() |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Execute the console command. |
|
37
|
|
|
* |
|
38
|
|
|
* @return mixed |
|
39
|
|
|
*/ |
|
40
|
|
|
public function handle() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->purgeDeletedRecords(); |
|
43
|
|
|
$this->generateCache(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function purgeDeletedRecords() |
|
47
|
|
|
{ |
|
48
|
|
|
$modules = Module::whereNotNull('model_class')->get(); |
|
49
|
|
|
foreach ($modules as $module) { |
|
50
|
|
|
$this->info('Purging '.$module->name); |
|
51
|
|
|
|
|
52
|
|
|
$modelClass = $module->model_class; |
|
53
|
|
|
|
|
54
|
|
|
$query = Entity::where('module_id', $module->id) |
|
55
|
|
|
->whereNotIn('record_id', function ($query) use ($modelClass) { |
|
56
|
|
|
$query->select('id') |
|
57
|
|
|
->from((new $modelClass)->getTable()) |
|
58
|
|
|
->get(); |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
$this->comment('Total to delete: '.$query->count()); |
|
62
|
|
|
|
|
63
|
|
|
$query->delete(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function generateCache() |
|
68
|
|
|
{ |
|
69
|
|
|
$modules = Module::whereNotNull('model_class')->get(); |
|
70
|
|
|
foreach ($modules as $module) { |
|
71
|
|
|
$this->info('Generating cache for '.$module->name); |
|
72
|
|
|
|
|
73
|
|
|
$modelClass = $module->model_class; |
|
74
|
|
|
$count = $modelClass::count(); |
|
75
|
|
|
$cpt = 0; |
|
76
|
|
|
$modelClass::chunkById(300, function ($records) use (&$cpt, $count) { |
|
77
|
|
|
$this->comment($cpt.'/'.$count); |
|
78
|
|
|
foreach ($records as $record) { |
|
79
|
|
|
$record->uuid; // Automaticaly generate cache (see \Uccello\Core\Support\Traits\UccelloModule getUuidAttribute()) |
|
80
|
|
|
} |
|
81
|
|
|
$cpt += 300; |
|
82
|
|
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|