-
Notifications
You must be signed in to change notification settings - Fork 611
/
DbController.php
500 lines (425 loc) · 15.9 KB
/
DbController.php
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\console\controllers;
use Craft;
use craft\console\Controller;
use craft\db\Connection;
use craft\db\Table;
use craft\helpers\Console;
use craft\helpers\Db;
use craft\helpers\FileHelper;
use craft\helpers\StringHelper;
use Throwable;
use yii\base\NotSupportedException;
use yii\console\ExitCode;
use yii\db\Exception;
use ZipArchive;
/**
* Performs database operations.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.6.0
*/
class DbController extends Controller
{
/**
* @var bool Whether the backup should be saved as a zip file.
*/
public bool $zip = false;
/**
* @var bool Whether to overwrite an existing backup file, if a specific file path is given.
*/
public bool $overwrite = false;
/**
* @var bool Whether to drop all preexisting tables in the database prior to restoring the backup.
* @since 4.1.0
*/
public bool $dropAllTables = false;
/**
* @inheritdoc
*/
public function options($actionID): array
{
$options = parent::options($actionID);
switch ($actionID) {
case 'backup':
$options[] = 'zip';
$options[] = 'overwrite';
break;
case 'restore':
$options[] = 'dropAllTables';
break;
}
return $options;
}
/**
* Drops all tables in the database.
*
* Example:
* ```
* php craft db/drop-all-tables
* ```
*
* @throws \yii\base\NotSupportedException
* @throws \yii\db\Exception
* @since 4.1.0
*/
public function actionDropAllTables(): int
{
if (!$this->_tablesExist()) {
$this->stdout('No existing database tables found.' . PHP_EOL, Console::FG_YELLOW);
return ExitCode::OK;
}
if ($this->interactive && !$this->confirm('Are you sure you want to drop all tables from the database?')) {
$this->stdout('Aborted.' . PHP_EOL, Console::FG_YELLOW);
return ExitCode::OK;
}
$this->_backupPrompt();
try {
$this->_dropAllTables();
} catch (Throwable $e) {
Craft::$app->getErrorHandler()->logException($e);
$this->stderr('error: ' . $e->getMessage() . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}
return ExitCode::OK;
}
/**
* Returns whether any database tables exist currently.
*
* @return bool
*/
private function _tablesExist(): bool
{
return !empty(Craft::$app->getDb()->getSchema()->getTableNames());
}
/**
* Prompts for whether the database should be backed up.
*/
private function _backupPrompt(): void
{
if ($this->interactive && $this->confirm('Backup your database?')) {
$this->runAction('backup');
$this->stdout(PHP_EOL);
}
}
/**
* Drops all tables in the database.
*
* @throws NotSupportedException
* @throws Exception
*/
private function _dropAllTables(): void
{
$tableNames = Craft::$app->getDb()->getSchema()->getTableNames();
$this->stdout('Dropping all database tables ... ' . PHP_EOL);
foreach ($tableNames as $tableName) {
$this->stdout(' - Dropping ');
$this->stdout($tableName, Console::FG_CYAN);
$this->stdout(' ... ');
Db::dropAllForeignKeysToTable($tableName);
Craft::$app->getDb()->createCommand()
->dropTable($tableName)
->execute();
$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
}
$this->stdout('Finished dropping all database tables.' . PHP_EOL . PHP_EOL, Console::FG_GREEN);
}
/**
* Creates a new database backup.
*
* Example:
* ```
* php craft db/backup ./my-backups/
* ```
*
* @param string|null $path The path the database backup should be created at.
* Can be any of the following:
*
* - A full file path
* - A folder path (backup will be saved in there with a dynamically-generated name)
* - A filename (backup will be saved in the working directory with the given name)
* - Blank (backup will be saved to the `storage/backups/` folder with a dynamically-generated name)
*
* @return int
*/
public function actionBackup(?string $path = null): int
{
$this->stdout('Backing up the database ... ');
$db = Craft::$app->getDb();
if ($path !== null) {
// Prefix with the working directory if a relative path or no path is given
if (str_starts_with($path, '.') || !str_contains(FileHelper::normalizePath($path, '/'), '/')) {
$path = getcwd() . DIRECTORY_SEPARATOR . $path;
}
$path = FileHelper::normalizePath($path);
if (is_dir($path)) {
$path .= DIRECTORY_SEPARATOR . basename($db->getBackupFilePath());
} elseif ($this->zip) {
$path = preg_replace('/\.zip$/', '', $path);
}
} else {
$path = $db->getBackupFilePath();
}
$checkPaths = [$path];
if ($this->zip) {
$checkPaths[] = "$path.zip";
}
foreach ($checkPaths as $checkPath) {
if (is_file($checkPath)) {
if (!$this->overwrite) {
if (!$this->interactive) {
$this->stderr("$checkPath already exists. Retry with the --overwrite flag to overwrite it." . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}
if (!$this->confirm("$checkPath already exists. Overwrite?")) {
$this->stdout('Aborting' . PHP_EOL);
return ExitCode::OK;
}
}
unlink($checkPath);
}
}
try {
$db->backupTo($path);
if ($this->zip) {
$zipPath = FileHelper::zip($path);
FileHelper::unlink($path);
$path = $zipPath;
}
} catch (Throwable $e) {
Craft::$app->getErrorHandler()->logException($e);
$this->stderr('error: ' . $e->getMessage() . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}
$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
$size = Craft::$app->getFormatter()->asShortSize(filesize($path));
$this->stdout("Backup file: $path ($size)" . PHP_EOL);
return ExitCode::OK;
}
/**
* Restores a database backup.
*
* Example:
* ```
* php craft db/restore ./my-backup.sql
* ```
*
* @param string|null $path The path to the database backup file.
* @return int
*/
public function actionRestore(?string $path = null): int
{
if (!is_readable($path)) {
if (!is_dir($path) && Craft::$app->getConfig()->getGeneral()->backupCommandFormat === 'directory') {
$this->stderr("Backup directory doesn't exist: $path" . PHP_EOL);
} else {
$this->stderr("Backup file doesn't exist: $path" . PHP_EOL);
}
return ExitCode::UNSPECIFIED_ERROR;
}
if (strtolower(pathinfo($path, PATHINFO_EXTENSION)) === 'zip') {
$zip = new ZipArchive();
if ($zip->open($path) !== true) {
$this->stderr("Unable to open the zip file at $path." . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}
$tempDir = Craft::$app->getPath()->getTempPath() . DIRECTORY_SEPARATOR . StringHelper::randomString(10);
FileHelper::createDirectory($tempDir);
$this->stdout("Extracting zip to a temp directory ... ");
$zip->extractTo($tempDir);
$zip->close();
$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
// Find the first file in there
$files = FileHelper::findFiles($tempDir);
if (empty($files)) {
$this->stderr("No files unzipped from $path." . PHP_EOL, Console::FG_RED);
}
$path = reset($files);
}
if ($this->_tablesExist()) {
$this->_backupPrompt();
if (
$this->dropAllTables ||
($this->interactive && $this->confirm('Drop all tables from the database first?'))
) {
try {
$this->_dropAllTables();
} catch (Throwable $e) {
Craft::$app->getErrorHandler()->logException($e);
$this->stderr('error: ' . $e->getMessage() . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}
}
}
$this->stdout('Restoring database backup ... ');
try {
Craft::$app->getDb()->restore($path);
} catch (Throwable $e) {
Craft::$app->getErrorHandler()->logException($e);
$this->stderr('error: ' . $e->getMessage() . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}
$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
if (isset($tempDir)) {
$this->stdout('Deleting the temp directory ... ');
FileHelper::removeDirectory($tempDir);
$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
}
if ($this->interactive && $this->confirm('Clear data caches?', true)) {
$this->run('clear-caches/data');
}
return ExitCode::OK;
}
/**
* Converts tables’ character sets and collations. (MySQL only)
*
* Example:
* ```
* php craft db/convert-charset utf8 utf8_unicode_ci
* ```
*
* @param string|null $charset The target character set, which honors `DbConfig::$charset`
* or defaults to `utf8`.
* @param string|null $collation The target collation, which honors `DbConfig::$collation`
* or defaults to `utf8_unicode_ci`.
* @return int
*/
public function actionConvertCharset(?string $charset = null, ?string $collation = null): int
{
$db = Craft::$app->getDb();
if (!$db->getIsMysql()) {
$this->stderr('This command is only available when using MySQL.' . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}
$schema = $db->getSchema();
$tableNames = $schema->getTableNames();
if (empty($tableNames)) {
$this->stderr('Could not find any database tables.' . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}
$dbConfig = Craft::$app->getConfig()->getDb();
if ($charset === null) {
$charset = $this->prompt('Which character set should be used?', [
'default' => $dbConfig->getCharset(),
]);
}
if ($collation === null) {
$collation = $this->prompt('Which collation should be used?', [
'default' => $dbConfig->collation ?? Db::defaultCollation($db),
]);
}
// Disable FK checks
$queryBuilder = $db->getSchema()->getQueryBuilder();
$db->createCommand($queryBuilder->checkIntegrity(false))->execute();
foreach ($tableNames as $tableName) {
$tableName = $schema->getRawTableName($tableName);
$this->stdout('Converting ');
$this->stdout($tableName, Console::FG_CYAN);
$this->stdout(' ... ');
$db->createCommand("ALTER TABLE `$tableName` CONVERT TO CHARACTER SET $charset COLLATE $collation")->execute();
$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
}
$db->createCommand()->checkIntegrity(true)->execute();
$this->stdout("Finished converting tables to $charset/$collation." . PHP_EOL, Console::FG_GREEN);
return ExitCode::OK;
}
/**
* Drops the database table prefix from all tables.
*
* @param string|null $prefix The current table prefix. If omitted, the prefix will be detected automatically.
* @return int
* @since 4.5.10
*/
public function actionDropTablePrefix(?string $prefix = null): int
{
$db = Craft::$app->getDb();
$prefix ??= $this->detectPrefix($db);
if ($prefix === false) {
return ExitCode::UNSPECIFIED_ERROR;
}
$prefix = StringHelper::ensureRight($prefix, '_');
$this->warning(<<<MD
All database tables named `$prefix*` will be renamed to drop the table prefix.
**This should NOT be run on production!**
MD);
if (!$this->confirm('Are you sure you want to proceed?')) {
return ExitCode::OK;
}
$tablePrefixBak = $db->tablePrefix;
$db->tablePrefix = $prefix;
$schema = $db->getSchema();
$quotedPrefix = preg_quote($prefix, '/');
foreach ($schema->getTableNames() as $oldName) {
if (!str_starts_with($oldName, $prefix)) {
continue;
}
$newName = preg_replace("/^$quotedPrefix/", '', $oldName);
$this->do(
$this->markdownToAnsi("Renaming `$oldName` to `$newName`"),
function() use ($oldName, $newName, $db) {
Db::renameTable($oldName, $newName, $db);
},
);
}
$db->tablePrefix = $tablePrefixBak;
$this->success('Database tables renamed.');
if (Craft::$app->getConfig()->getDb()->tablePrefix) {
$this->tip('Don’t forget to clear out your `tablePrefix` database setting.');
}
return ExitCode::OK;
}
private function detectPrefix(Connection $db): string|false
{
$this->stdout("Detecting the current table prefix …\n");
$patterns = array_map(
function(string $name) {
// based on Schema::getRawTableName()
$name = preg_replace('/\\{\\{(.*?)}}/', '\1', $name);
$name = preg_replace_callback('/[^%]+/', fn($match) => preg_quote($match[0], '/'), $name);
return sprintf('/^%s$/', str_replace('%', '(\w+)_', $name));
},
[Table::ELEMENTS, Table::ENTRIES, Table::INFO, Table::SECTIONS],
);
$foundPrefixes = [];
foreach ($db->getSchema()->getTableNames() as $name) {
foreach ($patterns as $pattern) {
if (preg_match($pattern, $name, $match)) {
if (!isset($foundPrefixes[$match[1]])) {
$foundPrefixes[$match[1]] = 1;
} else {
$foundPrefixes[$match[1]]++;
}
break;
}
}
}
$possiblePrefixes = [];
foreach ($foundPrefixes as $prefix => $count) {
if ($count === count($patterns)) {
$possiblePrefixes[] = $prefix;
}
}
if (empty($possiblePrefixes)) {
$this->stdout("No current table prefix appears to be in use.\n", Console::FG_RED);
return false;
}
if (count($possiblePrefixes) === 1) {
$prefix = reset($possiblePrefixes);
$this->stdout($this->markdownToAnsi("`$prefix` detected.") . PHP_EOL);
return $prefix;
}
if (!$this->interactive) {
$this->stdout("Multiple table prefixes were detected. Run the command again with the current prefix specified.\n", Console::FG_RED);
return false;
}
return $this->select(
'Multiple table prefixes were detected. Which one should be removed?',
array_combine($possiblePrefixes, $possiblePrefixes),
);
}
}