Skip to content

Commit

Permalink
コアのアップデートの仕組みを改善
Browse files Browse the repository at this point in the history
composer での処理と、migration の処理を分けた
composer の処理は、一時フォルダ内で実行し、migrations 時に vendorにコピーする仕様とした
  • Loading branch information
ryuring committed Jun 16, 2024
1 parent 8771414 commit e423b09
Show file tree
Hide file tree
Showing 16 changed files with 445 additions and 201 deletions.
1 change: 0 additions & 1 deletion plugins/baser-core/src/BaserCorePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,6 @@ public function console(CommandCollection $commands): CommandCollection
{
$commands->add('setup test', SetupTestCommand::class);
$commands->add('composer', ComposerCommand::class);
$commands->add('update', UpdateCommand::class);
$commands->add('create release', CreateReleaseCommand::class);
$commands->add('setup install', SetupInstallCommand::class);
return $commands;
Expand Down
75 changes: 0 additions & 75 deletions plugins/baser-core/src/Command/UpdateCommand.php

This file was deleted.

66 changes: 47 additions & 19 deletions plugins/baser-core/src/Controller/Admin/PluginsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function install(PluginsAdminServiceInterface $service, string $name)

/**
* アップデート実行
* @param PluginsService $service
* @param PluginsAdminServiceInterface|PluginsAdminService $service
* @param string $name
* @return void|Response
* @checked
Expand All @@ -116,7 +116,6 @@ public function install(PluginsAdminServiceInterface $service, string $name)
*/
public function update(PluginsAdminServiceInterface $service, $name = '')
{
BcUtil::clearAllCache();
$plugin = $this->Plugins->getPluginConfig($name);
$this->set($service->getViewVarsForUpdate($plugin));

Expand All @@ -138,31 +137,60 @@ public function update(PluginsAdminServiceInterface $service, $name = '')

if (!$this->request->is(['put', 'post'])) return;
try {
if($plugin->name === 'BaserCore') {
$request = $this->getRequest();
$service->updateCore(
$request->getData('currentVersion'),
$request->getData('targetVersion'),
$request->getData('php'),
$request->getData('connection') ?? 'default'
);
$this->BcMessage->setInfo(__d('baser_core', '全てのアップデート処理が完了しました。 {0} にログを出力しています。', LOGS . 'update.log'));
return $this->redirect(['action' => 'update']);
} else {
$service->update($plugin->name, $this->request->getData('connection') ?? 'default');
if($service->update($plugin->name, $this->request->getData('connection') ?? 'default')) {
$this->BcMessage->setInfo(__d('baser_core', 'アップデート処理が完了しました。画面下部のアップデートログを確認してください。'));
return $this->redirect(['action' => 'update', $name]);
} else {
$this->BcMessage->setError(__d('baser_core', 'アップデート処理に失敗しました。画面下部のアップデートログを確認してください。'));
}
} catch (\Throwable $e) {
$this->BcMessage->setError($e->getMessage());
$this->BcMessage->setError(__d('baser_core', 'アップデート処理に失敗しました。画面下部のアップデートログを確認してください。') . $e->getMessage());
if($plugin->name === 'BaserCore') {
return $this->redirect(['action' => 'update']);
} else {
return $this->redirect(['action' => 'update', $name]);
$request = $this->getRequest();
try {
$service->rollbackCore(
$request->getData('currentVersion'),
$request->getData('php'),
);
$this->BcMessage->setError(__d('baser_core', 'コアファイルを元に戻しました。'));
} catch (\Throwable $e) {
$this->BcMessage->setError($e->getMessage());
}
}
}
if($plugin->name === 'BaserCore') {
return $this->redirect(['action' => 'update']);
} else {
return $this->redirect(['action' => 'update', $name]);
}
}

/**
* コアアップデートを取得する
* @param PluginsAdminServiceInterface $service
* @return Response|null
* @checked
* @noTodo
* @unitTest
*/
public function get_core_update(PluginsAdminServiceInterface $service)
{
if (!$this->request->is(['put', 'post'])) {
$this->BcMessage->setError(__d('baser_core', '無効な処理です。'));
return $this->redirect(['action' => 'update']);
}
$request = $this->getRequest();
try {
$service->getCoreUpdate(
$request->getData('targetVersion')?? '',
$request->getData('php')?? 'php',
);
} catch (\Throwable $e) {
$this->BcMessage->setError($e->getMessage());
}
$this->BcMessage->setSuccess(__d('baser_core', '最新版のダウンロードが完了しました。アップデートを実行してください。'));
return $this->redirect(['action' => 'update']);
}

/**
* 無効化
*
Expand Down
24 changes: 24 additions & 0 deletions plugins/baser-core/src/Controller/Api/Admin/PluginsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,28 @@ public function get_available_core_version_info(PluginsServiceInterface $service
$this->viewBuilder()->setOption('serialize', ['availableCoreVersionInfo']);
}

/**
* コアファイルの最新版を反映する
*
* @param PluginsServiceInterface $service
* @checked
* @noTodo
* @unitTest
*/
public function update_core_files(PluginsServiceInterface $service)
{
$this->request->allowMethod(['post', 'put']);
try {
$service->updateCoreFiles();
$message = __d('baser_core', 'コアファイルの最新版への更新が完了しました。');
} catch (\Throwable $e) {
$message = __d('baser_core', 'コアファイルの最新版への更新中にエラーが発生しました。' . $e->getMessage());
$this->setResponse($this->response->withStatus(500));
}
$this->set([
'message' => $message
]);
$this->viewBuilder()->setOption('serialize', ['message']);
}

}
31 changes: 17 additions & 14 deletions plugins/baser-core/src/Service/Admin/PluginsAdminService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use BaserCore\Service\PluginsService;
use BaserCore\Utility\BcUtil;
use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Core\Plugin as CakePlugin;
use Cake\Datasource\EntityInterface;
Expand Down Expand Up @@ -60,15 +61,15 @@ public function getViewVarsForUpdate(EntityInterface $entity): array
$dbVersion = BcUtil::getDbVersion($entity->name);
BcUtil::includePluginClass($entity->name);
$plugin = CakePlugin::getCollection()->create($entity->name);
$scriptNum = count($plugin->getUpdaters());
$scriptMessages = $plugin->getUpdateScriptMessages();
$scriptNum = count($plugin->getUpdaters('', true));
$scriptMessages = $plugin->getUpdateScriptMessages('', true);

if ($entity->name === 'BaserCore') {
$availableVersion = $this->getAvailableCoreVersion();
$corePlugins = Configure::read('BcApp.corePlugins');
foreach($corePlugins as $corePlugin) {
$scriptNum += count($plugin->getUpdaters($corePlugin));
$scriptMessages += $plugin->getUpdateScriptMessages($corePlugin);
$scriptNum += count($plugin->getUpdaters($corePlugin, true));
$scriptMessages += $plugin->getUpdateScriptMessages($corePlugin, true);
}
} else {
$availableVersion = null;
Expand All @@ -94,9 +95,9 @@ public function getViewVarsForUpdate(EntityInterface $entity): array
'requireUpdate' => $this->isRequireUpdate(
$programVersion,
$dbVersion,
$availableVersion,
$scriptNum
$availableVersion
),
'coreDownloaded' => Cache::read('coreDownloaded', '_bc_update_'),
'php' => $this->whichPhp(),
'isWritableVendor' => $isWritableVendor,
'isWritableComposerJson' => $isWritableComposerJson,
Expand All @@ -120,7 +121,7 @@ public function whichPhp()

/**
* アップデートが必要がどうか
*
* DBのバージョンと利用可能なバージョンが違う場合に必要とする
* @param string $programVersion
* @param string $dbVersion
* @param string $availableVersion
Expand All @@ -129,7 +130,7 @@ public function whichPhp()
* @checked
* @noTodo
*/
public function isRequireUpdate(string $programVersion, ?string $dbVersion, ?string $availableVersion, $scriptNum)
public function isRequireUpdate(string $programVersion, ?string $dbVersion, ?string $availableVersion)
{
$programVerPoint = BcUtil::verpoint($programVersion);
$dbVerPoint = BcUtil::verpoint($dbVersion);
Expand All @@ -140,14 +141,16 @@ public function isRequireUpdate(string $programVersion, ?string $dbVersion, ?str
if ($programVerPoint === false || $dbVerPoint === false || $availableVerPoint === false) {
return false;
}
if ($availableVerPoint !== true) {
if ($availableVersion !== $programVersion) return true;
}
if ($programVersion !== $dbVersion || $scriptNum) {
return true;

if(is_null($availableVersion)) {
// プラグインの場合 プログラムのバージョンを利用可能なバージョンとする
$availableVersion = $programVersion;
} else {
return false;
// コアの場合は、プログラムのバージョンとDBのバージョンが違う場合はアップデート不可
if ($programVersion !== $dbVersion) return false;
}
if ($availableVersion !== $dbVersion) return true;
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public function getViewVarsForInstall(EntityInterface $plugin): array;
* @param EntityInterface $plugin
* @return array
*/
public function getViewVarsForUpdate(EntityInterface $plugin): array;
public function getViewVarsForUpdate(EntityInterface $entity): array;

}
Loading

0 comments on commit e423b09

Please sign in to comment.