Skip to content

Adds a db/repair CLI command #16812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Added the `db/repair` command.
- Fixed an error that could occur when generating an image transform URL via a console request. ([#16793](https://github.com/craftcms/cms/issues/16793))
- Fixed a bug where `_includes/forms/button.twig` was always adding `class="btngroup-btn-first"` to the resulting button HTML.

Expand Down
72 changes: 72 additions & 0 deletions src/console/controllers/DbController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,45 @@ public function options($actionID): array
return $options;
}

/**
* Repairs all tables in the database.
*
* Note that this can cause table locking, which could interfere SQL being executed.
*
* Example:
* ```
* php craft db/repair
* ```
*
* @since 4.15
* @see https://dev.mysql.com/doc/refman/8.4/en/optimize-table.html
* @see https://www.postgresql.org/docs/current/sql-analyze.html
*/
public function actionRepair(): 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 repair all tables from the database?')) {
$this->stdout('Aborted.' . PHP_EOL, Console::FG_YELLOW);
return ExitCode::OK;
}

$this->_backupPrompt();

try {
$this->_repairAllTables();
} 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;
}

/**
* Drops all tables in the database.
*
Expand Down Expand Up @@ -140,6 +179,39 @@ private function _backupPrompt(): void
}
}

/**
* Repairs all tables in the database.
*
* @throws NotSupportedException
* @throws Exception
*/
private function _repairAllTables(): void
{
$db = Craft::$app->getDb();
$tableNames = $db->getSchema()->getTableNames();

$this->stdout('Repairing all database tables ... ' . PHP_EOL);

foreach ($tableNames as $tableName) {
$this->stdout(' - Repairing ');
$this->stdout($tableName, Console::FG_CYAN);
$this->stdout(' ... ');

if ($db->getDriverName() === Connection::DRIVER_MYSQL) {
$sql = 'OPTIMIZE TABLE ' . $db->quoteTableName($tableName) . ';';
} elseif ($db->getDriverName() === Connection::DRIVER_PGSQL) {
$sql = 'ANALYZE VERBOSE ' . $db->quoteTableName($tableName) . ';';
} else {
throw new NotSupportedException('Unknown database driver.');
}

Craft::$app->getDb()->createCommand($sql)->execute();
$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
}

$this->stdout('Finished repairing all database tables.' . PHP_EOL . PHP_EOL, Console::FG_GREEN);
}

/**
* Drops all tables in the database.
*
Expand Down
Loading