update db diff handling

This commit is contained in:
Paul Spenke
2023-07-03 11:24:53 +08:00
parent 7b47fdaf14
commit 7d60bc9b45
4 changed files with 65 additions and 5 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+8
View File
@@ -5,6 +5,8 @@ namespace Plugi\Core;
use Exception; use Exception;
use PDO; use PDO;
use Plugi\Extensions; use Plugi\Extensions;
use Plugi\Repositories\SettingRepository;
use Plugi\Setting;
/** /**
* Class DB * Class DB
@@ -170,6 +172,10 @@ class DB
return $tables; return $tables;
} }
public function getDBDefinitionHash() {
return sha1(json_encode($this->getDBDefinition()));
}
public function diffDB() { public function diffDB() {
$tables = $this->getDBDefinition(); $tables = $this->getDBDefinition();
$diff = []; $diff = [];
@@ -209,5 +215,7 @@ class DB
} }
} }
} }
$settingRepository = new SettingRepository;
$settingRepository->updateSettings(['db-definition' => $this->getDBDefinitionHash()]);
} }
} }
@@ -1,3 +1,8 @@
<?php
use Plugi\Setting;
?>
<div class="overflow-x-auto"> <div class="overflow-x-auto">
<table class="table table-zebra"> <table class="table table-zebra">
<tbody> <tbody>
@@ -8,13 +13,15 @@
<tr> <tr>
<th class="w-60">Database</th> <th class="w-60">Database</th>
<td> <td>
<form method="post" action="<?= phpb_url('website_manager', ['route' => 'database', 'action' => 'update', 'tab' => 'info']) ?>"> <form method="post" action="<?= phpb_url('website_manager', ['route' => 'database', 'action' => 'update']) ?>">
<?php <?php
global $phpb_db; global $phpb_db;
$diff = $phpb_db->diffDB(); $diff = $phpb_db->diffDB();
$actionNeeded = false; $actionNeeded = false;
if (Setting::get('db-definition') !== $phpb_db->getDBDefinitionHash()) $actionNeeded = true;
foreach ($diff as $table => $tableDiff) : foreach ($diff as $table => $tableDiff) :
if(!is_array($tableDiff)) $actionNeeded = true; if(!is_array($tableDiff)) $actionNeeded = true;
if(is_array($tableDiff) && empty($tableDiff['columns']) && empty($tableDiff['uniqueKeys'])) continue;
?> ?>
<div> <div>
table <strong><?= phpb_e($table) ?></strong> (<?= phpb_e(is_array($tableDiff) ? (empty($tableDiff['columns']) && empty($tableDiff['uniqueKeys']) ? 'is fine' : 'has diff') : 'is not existent') ?>) table <strong><?= phpb_e($table) ?></strong> (<?= phpb_e(is_array($tableDiff) ? (empty($tableDiff['columns']) && empty($tableDiff['uniqueKeys']) ? 'is fine' : 'has diff') : 'is not existent') ?>)
@@ -49,6 +56,10 @@
?> ?>
<button class="btn" type="submit">Update</button> <button class="btn" type="submit">Update</button>
<?php <?php
else :
?>
<div>Database is up-to-date! <span class="text-gray-400">(<?= $phpb_db->getDBDefinitionHash() ?>)</span></div>
<?php
endif; endif;
?> ?>
+44 -3
View File
@@ -67,9 +67,14 @@ class Plugi
$this->setConfig($config); $this->setConfig($config);
// load extensions
$this->loadExtensions();
// create database connection, if enabled // create database connection, if enabled
if (phpb_config('storage.use_database')) { if (phpb_config('storage.use_database')) {
$this->setDatabaseConnection(phpb_config('storage.database')); $this->setDatabaseConnection(phpb_config('storage.database'));
$this->checkDatabaseDefinition();
$this->hydrateConfig();
} }
// init the default authentication, if enabled // init the default authentication, if enabled
@@ -92,9 +97,6 @@ class Plugi
$this->router = phpb_instance('router'); $this->router = phpb_instance('router');
// load extensions
$this->loadExtensions();
// load translations in the language that is currently active // load translations in the language that is currently active
$this->loadTranslations(phpb_current_language()); $this->loadTranslations(phpb_current_language());
} }
@@ -193,6 +195,45 @@ class Plugi
if ($language) $phpb_config['general']['language'] = $language; if ($language) $phpb_config['general']['language'] = $language;
} }
/**
* Set the Plugi database connection using the given array.
*
* @param array $config
*/
public function hydrateConfig()
{
global $phpb_config;
$theme = Setting::get('selected_theme');
if ($theme) $phpb_config['theme']['active_theme'] = $theme;
$language = Setting::get('admin_language');
if ($language) $phpb_config['general']['language'] = $language;
}
/**
* Set the Plugi database connection using the given array.
*
* @param array $config
*/
public function checkDatabaseDefinition()
{
global $phpb_db;
if (Setting::get('db-definition') !== $phpb_db->getDBDefinitionHash()) {
$module = 'website_manager';
$parameters = [
['tab' => 'info'],
['route' => 'database', 'action' => 'update']
];
if(!phpb_on_url($module, $parameters[0])) {
if(!phpb_on_url($module, $parameters[1])) {
phpb_redirect(phpb_url($module, $parameters[0]), [
'message-type' => 'error',
'message' => phpb_trans('website-manager.check-database')
]);
}
}
}
}
/** /**
* Set a custom auth. * Set a custom auth.
* *