1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sausin\DBSetAutoIncrement\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Facades\Config; |
8
|
|
|
use Sausin\DBSetAutoIncrement\DatabaseInfo; |
9
|
|
|
use Sausin\DBSetAutoIncrement\GetAttribute; |
10
|
|
|
use Sausin\DBSetAutoIncrement\UpdateAttribute; |
11
|
|
|
|
12
|
|
|
class SetAutoIncrementCommand extends Command |
13
|
|
|
{ |
14
|
|
|
use DatabaseInfo; |
15
|
|
|
use GetAttribute; |
16
|
|
|
use UpdateAttribute; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The name and signature of the console command. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $signature = 'db:set-auto-increment |
24
|
|
|
{--tables=* : The table(s) for which auto increment should be set} |
25
|
|
|
{--value= : The auto increment value to be set}'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Set auto increment for database table(s)'; |
33
|
|
|
|
34
|
|
|
/** @var array */ |
35
|
|
|
protected $skipTables; |
36
|
|
|
|
37
|
|
|
/** @var array */ |
38
|
|
|
protected $onlyTables; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
protected $mode; |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
protected $driver; |
45
|
|
|
|
46
|
|
|
/** @var int */ |
47
|
|
|
protected $autoIncrement; |
48
|
|
|
|
49
|
|
|
/** @var array */ |
50
|
|
|
protected $supportedDrivers = ['mysql', 'sqlite']; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The name of the queue the job should be sent to. |
54
|
|
|
* |
55
|
|
|
* @var string|null |
56
|
|
|
*/ |
57
|
|
|
public $queue = 'monitoring'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create the event listener. |
61
|
|
|
* |
62
|
|
|
* @return void |
|
|
|
|
63
|
|
|
*/ |
64
|
|
|
public function __construct() |
65
|
|
|
{ |
66
|
|
|
parent::__construct(); |
67
|
|
|
|
68
|
|
|
$this->mode = Config::get('auto-increment.mode', 'skip'); |
69
|
|
|
$this->skipTables = Config::get('auto-increment.skipTables', ['migrations']); |
|
|
|
|
70
|
|
|
$this->onlyTables = Config::get('auto-increment.onlyTables', []); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Execute the console command. |
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
public function handle() |
79
|
|
|
{ |
80
|
|
|
$this->autoIncrement = $this->option('value') ?? Config::get('auto-increment.autoIncrement', 100001); |
81
|
|
|
|
82
|
|
|
if (! $this->isDatabaseCompatible()) { |
83
|
|
|
$this->info("Database {$this->driver} not supported"); |
84
|
|
|
|
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$driver = ucfirst($this->driver); |
89
|
|
|
|
90
|
|
|
if ($this->option('tables')) { |
91
|
|
|
$this->{"update{$driver}Tables"}(collect($this->option('tables'))); |
92
|
|
|
|
93
|
|
|
$this->info('Specified tables have been updated'); |
94
|
|
|
|
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$tables = collect([]); |
99
|
|
|
|
100
|
|
|
if ($this->mode === 'only') { |
101
|
|
|
$tables = collect($this->onlyTables); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($this->mode === 'skip') { |
105
|
|
|
$tables = collect($this->getTableList())->reject(function ($value) { |
106
|
|
|
return in_array($value, $this->skipTables, true); |
107
|
|
|
}); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->{"update{$driver}Tables"}($tables); |
111
|
|
|
$this->info('Tables have been updated as per config'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Update AUTO INCREMENT value in mysql tables. |
116
|
|
|
* |
117
|
|
|
* @param Collection $tables |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
protected function updateMysqlTables(Collection $tables): void |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$tables->filter(function ($table) { |
123
|
|
|
return $this->getAutoIncrement('Mysql', $table) < $this->autoIncrement; |
124
|
|
|
})->map(function ($table) { |
125
|
|
|
$this->updateAutoIncrement('Mysql', $table); |
126
|
|
|
}); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Update AUTO INCREMENT value in sqlite tables. |
131
|
|
|
* |
132
|
|
|
* @param Collection $tables |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
protected function updateSqliteTables(Collection $tables): void |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
// the auto increment value is reduced by 1 as SQLITE uses it in this way |
138
|
|
|
$this->autoIncrement--; |
139
|
|
|
|
140
|
|
|
$tables->filter(function ($table) { |
141
|
|
|
return $this->getAutoIncrement('Sqlite', $table) < $this->autoIncrement; |
142
|
|
|
})->map(function ($table) { |
143
|
|
|
$this->updateAutoIncrement('Sqlite', $table); |
144
|
|
|
}); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.