Skip to content

Commit

Permalink
Trigger db migrations manually
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-rueegg committed Dec 15, 2023
1 parent eac7f12 commit e8bb6d6
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ HumHub Changelog

1.16.0 (Unreleased)
-------------------
- Enh #6711: run migrations manually
- Enh #6720: Consolidate `isInstalled()`, `setInstalled()`, and `setDatabaseInstalled`
- Fix #6693: `MigrateController::$migrationPathMap` stored rolling sum of migrations
- Enh #6697: Make state badge customizable
Expand Down
12 changes: 9 additions & 3 deletions protected/humhub/commands/MigrateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use humhub\components\Module;
use humhub\helpers\DatabaseHelper;
use Yii;
use yii\base\InvalidRouteException;
use yii\console\Exception;
use yii\db\MigrationInterface;
use yii\web\Application;
Expand Down Expand Up @@ -202,17 +203,22 @@ protected function getMigrationPaths(): array
/**
* Executes all pending migrations
*
* @param string $action 'up' or 'new'
* @param \yii\base\Module|null $module Module to get the migrations from, or Null for Application
*
* @return string output
* @throws Exception
* @throws InvalidRouteException
*/
public static function webMigrateAll(): string
public static function webMigrateAll(string $action = 'up', ?\yii\base\Module $module = null): string
{
ob_start();
$controller = new self('migrate', Yii::$app);
$controller = new self('migrate', $module ?? Yii::$app);
$controller->db = Yii::$app->db;
$controller->interactive = false;
$controller->includeModuleMigrations = true;
$controller->color = false;
$controller->runAction('up');
$controller->runAction($action);

return ob_get_clean() ?: '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

namespace humhub\modules\admin\controllers;

use humhub\commands\MigrateController;
use humhub\libs\SelfTest;
use humhub\modules\admin\components\Controller;
use humhub\modules\admin\components\DatabaseInfo;
use humhub\modules\admin\libs\HumHubAPI;
use humhub\modules\admin\permissions\SeeAdminInformation;
use humhub\modules\queue\driver\MySQL;
use humhub\modules\queue\helpers\QueueHelper;
use humhub\modules\queue\interfaces\QueueInfoInterface;
Expand All @@ -20,12 +23,15 @@
use Yii;

/**
* Informations
* Information
*
* @since 0.5
*/
class InformationController extends Controller
{
public const DB_ACTION_CHECK = 0;
public const DB_ACTION_RUN = 1;
public const DB_ACTION_PENDING = 2;

/**
* @inheritdoc
Expand All @@ -52,7 +58,7 @@ public function init()
protected function getAccessRules()
{
return [
['permissions' => \humhub\modules\admin\permissions\SeeAdminInformation::class],
['permissions' => SeeAdminInformation::class],
];
}

Expand All @@ -78,11 +84,25 @@ public function actionAbout()

public function actionPrerequisites()
{
return $this->render('prerequisites', ['checks' => \humhub\libs\SelfTest::getResults()]);
return $this->render('prerequisites', ['checks' => SelfTest::getResults()]);
}

public function actionDatabase()
public function actionDatabase(int $migrate = self::DB_ACTION_CHECK)
{
if ($migrate === self::DB_ACTION_RUN) {
$migrationOutput = sprintf(
"%s\n%s",
MigrateController::webMigrateAll(),
SettingController::flushCache()
);
} else {
$migrationOutput = MigrateController::webMigrateAll(MigrateController::MIGRATION_ACTION_NEW);

$migrate = str_contains($migrationOutput, 'No new migrations found.')
? self::DB_ACTION_CHECK
: self::DB_ACTION_PENDING;
}

$databaseInfo = new DatabaseInfo(Yii::$app->db->dsn);

$rebuildSearchJob = new RebuildIndex();
Expand All @@ -95,7 +115,8 @@ public function actionDatabase()
[
'rebuildSearchRunning' => QueueHelper::isQueued($rebuildSearchJob),
'databaseName' => $databaseInfo->getDatabaseName(),
'migrate' => \humhub\commands\MigrateController::webMigrateAll(),
'migrationOutput' => $migrationOutput,
'migrationStatus' => $migrate,
]
);
}
Expand All @@ -105,8 +126,8 @@ public function actionDatabase()
*/
public function actionBackgroundJobs()
{
$lastRunHourly = (int) Yii::$app->settings->getUncached('cronLastHourlyRun');
$lastRunDaily = (int) Yii::$app->settings->getUncached('cronLastDailyRun');
$lastRunHourly = (int)Yii::$app->settings->getUncached('cronLastHourlyRun');
$lastRunDaily = (int)Yii::$app->settings->getUncached('cronLastDailyRun');

$queue = Yii::$app->queue;

Expand All @@ -115,7 +136,10 @@ public function actionBackgroundJobs()
$canClearQueue = true;
if (Yii::$app->request->isPost && Yii::$app->request->get('clearQueue') == 1) {
$queue->clear();
$this->view->setStatusMessage('success', Yii::t('AdminModule.information', 'Queue successfully cleared.'));
$this->view->setStatusMessage(
'success',
Yii::t('AdminModule.information', 'Queue successfully cleared.')
);
}
}

Expand All @@ -137,7 +161,7 @@ public function actionBackgroundJobs()
$reflect = new ReflectionClass($queue);
$driverName = $reflect->getShortName();
} catch (ReflectionException $e) {
Yii::error('Could not determine queue driver: '. $e->getMessage());
Yii::error('Could not determine queue driver: ' . $e->getMessage());
}

return $this->render('background-jobs', [
Expand All @@ -151,5 +175,4 @@ public function actionBackgroundJobs()
'canClearQueue' => $canClearQueue
]);
}

}
38 changes: 26 additions & 12 deletions protected/humhub/modules/admin/controllers/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
*/
class SettingController extends Controller
{

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -125,11 +124,9 @@ public function actionDeleteIconImage()
*/
public function actionCaching()
{
$form = new CacheSettingsForm;
$form = new CacheSettingsForm();
if ($form->load(Yii::$app->request->post()) && $form->validate() && $form->save()) {
Yii::$app->cache->flush();
Yii::$app->assetManager->clear();
Yii::$app->view->theme->activate();
self::flushCache();
$this->view->success(Yii::t('AdminModule.settings', 'Saved and flushed cache'));
return $this->redirect(['/admin/setting/caching']);
}
Expand All @@ -145,7 +142,7 @@ public function actionCaching()
*/
public function actionStatistic()
{
$form = new StatisticSettingsForm;
$form = new StatisticSettingsForm();
if ($form->load(Yii::$app->request->post()) && $form->validate() && $form->save()) {
$this->view->saved();
return $this->redirect([
Expand Down Expand Up @@ -178,7 +175,7 @@ public function actionNotification()
*/
public function actionMailingServer()
{
$form = new MailingSettingsForm;
$form = new MailingSettingsForm();
if ($form->load(Yii::$app->request->post()) && $form->validate() && $form->save()) {
return $this->redirect(['/admin/setting/mailing-server-test']);
}
Expand Down Expand Up @@ -220,7 +217,7 @@ public function actionMailingServerTest()

public function actionDesign()
{
$form = new DesignSettingsForm;
$form = new DesignSettingsForm();
if ($form->load(Yii::$app->request->post()) && $form->validate() && $form->save()) {
$this->view->saved();
return $this->redirect([
Expand All @@ -238,7 +235,7 @@ public function actionDesign()
*/
public function actionFile()
{
$form = new FileSettingsForm;
$form = new FileSettingsForm();
if ($form->load(Yii::$app->request->post()) && $form->validate() && $form->save()) {
$this->view->saved();
return $this->redirect([
Expand Down Expand Up @@ -272,7 +269,7 @@ public function actionFile()
*/
public function actionProxy()
{
$form = new ProxySettingsForm;
$form = new ProxySettingsForm();


if ($form->load(Yii::$app->request->post()) && $form->validate() && $form->save()) {
Expand Down Expand Up @@ -318,7 +315,7 @@ public function actionLogs()
$dating = "the begining of time";
}

$form = new LogsSettingsForm;
$form = new LogsSettingsForm();
$limitAgeOptions = $form->options;
if ($form->load(Yii::$app->request->post()) && $form->validate() && $form->save()) {

Expand All @@ -343,7 +340,7 @@ public function actionLogs()
*/
public function actionOembedEdit()
{
$form = new OEmbedProviderForm;
$form = new OEmbedProviderForm();

$name = Yii::$app->request->get('name');
$providers = UrlOembed::getProviders();
Expand Down Expand Up @@ -399,4 +396,21 @@ public function actionAdvanced()
]);
}

/**
* @since 1.16
* @return string Activity output that can be used for logging
*/
public static function flushCache(): string
{
$output = "Flushing cache ...";
Yii::$app->cache->flush();

$output .= "\nFlushing asset manager ...";
Yii::$app->assetManager->clear();

$output .= "\nFlushing theme cache ...";
Yii::$app->view->theme->activate();

return $output;
}
}
49 changes: 43 additions & 6 deletions protected/humhub/modules/admin/views/information/database.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

use humhub\libs\Html;
use humhub\modules\admin\controllers\InformationController;
use humhub\modules\ui\view\components\View;

/**
* @var $this \humhub\modules\ui\view\components\View
* @var $this View
* @var $databaseName string
* @var $migrate string
* @var $migrationStatus int
* @var $migrationOutput string
* @var $rebuildSearchRunning boolean
*/

?>
<div>
<p>
Expand All @@ -27,10 +32,42 @@
</p>
</div>

<p><?= Yii::t('AdminModule.information', 'Database migration results:'); ?></p>

<div class="well">
<div>
<?php if ($migrationStatus === InformationController::DB_ACTION_PENDING): ?>
<p><?= Yii::t('AdminModule.information', 'Outstanding database migrations:'); ?></p>
<div class="well">
<pre>
<?= $migrate; ?>
<?= $migrationOutput ?>
</pre>
</div>
<p><br>
<?= Html::a(
Yii::t('AdminModule.information', 'Refresh'),
['/admin/information/database'],
[
'id' => 'migrationRun',
'class' => 'btn btn-primary pull-right',
]
); ?>
</p>
<?php elseif ($migrationStatus === InformationController::DB_ACTION_RUN): ?>
<p><?= Yii::t('AdminModule.information', 'Database migration results:'); ?></p>
<div class="well">
<pre>
<?= $migrationOutput ?>
</pre>
</div>
<p><br>
<?= Html::a(
Yii::t('AdminModule.information', 'Update Database'),
['/admin/information/database', 'migrate' => 1],
[
'id' => 'migrationRun',
'class' => 'btn btn-primary pull-right',
]
); ?>
</p>
<?php else: ?>
<p><?= Yii::t('AdminModule.information', 'Your database is <b>up-to-date</b>.'); ?></p>
<?php endif; ?>
</div>

0 comments on commit e8bb6d6

Please sign in to comment.