Skip to content

Commit

Permalink
add upgrade and autocreate db if wrong upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
PrestaSafe committed Oct 17, 2024
1 parent 778a51d commit 2191bd6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
7 changes: 6 additions & 1 deletion prettyblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct()
{
$this->name = 'prettyblocks';
$this->tab = 'administration';
$this->version = '3.1.1';
$this->version = '3.1.2';
$this->author = 'PrestaSafe';
$this->need_instance = 1;
$this->js_path = $this->_path . 'views/js/';
Expand Down Expand Up @@ -215,6 +215,11 @@ private function createBlockDb()
return $isOk;
}

/**
* Create table to store editors of the page
*
* @return bool
*/
public function makeConnectedEmployee()
{
// Creating a table to store editors of the page
Expand Down
2 changes: 2 additions & 0 deletions src/Controller/AdminThemeManagerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,8 @@ public function getConnectedEmployeesAction(Request $request)

$connectedEmployees = ConnectedEmployeeDataProvider::get();
if (null === $connectedEmployees) {
// autocreate table if not exists
ConnectedEmployeeDataPersister::tableExists();
return (new JsonResponse())->setData([
'success' => false,
]);
Expand Down
16 changes: 16 additions & 0 deletions src/DataPersister/ConnectedEmployeeDataPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace PrestaSafe\PrettyBlocks\DataPersister;

use Module;
use PrettyBlocksMigrate;

final class ConnectedEmployeeDataPersister
{
/**
Expand Down Expand Up @@ -29,6 +32,7 @@ public static function insert(int $id_user, string $session_id): bool
ON DUPLICATE KEY UPDATE last_update = "' . date('Y-m-d H:i:s') . '"'
);
} catch (\Exception $e) {
new \PrestaShopException('Error while inserting connected employee', 0, $e);
return false;
}
}
Expand All @@ -51,6 +55,8 @@ public static function update(string $session_id): bool
'session_id = \'' . pSQL($session_id) . '\''
);
} catch (\Exception $e) {

new \PrestaShopException('Error while updating connected employee', 0, $e);
return false;
}
}
Expand All @@ -73,7 +79,17 @@ public static function cleanData(): bool

return $database->execute($sql);
} catch (\Exception $e) {

new \PrestaShopException('Error while cleaning connected employee', 0, $e);
return false;
}
}

public static function tableExists(): void
{
if (!PrettyBlocksMigrate::tableExists('prettyblocks_connected_employee')) {
dump('table not exists');
Module::getInstanceByName('prettyblocks')->makeConnectedEmployee();
}
}
}
38 changes: 38 additions & 0 deletions upgrade/upgrade-3.1.2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright (c) Since 2020 PrestaSafe and contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaSafe <[email protected]>
* @copyright Since 2020 PrestaSafe and contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaSafe
*/
if (!defined('_PS_VERSION_')) {
exit;
}

/**
* @param prettyblocks $module
*
* @return bool|string
*
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
function upgrade_module_3_1_2($module)
{
$module->registerHook($module->hooks);
$module->makeConnectedEmployee();

return true;
}

0 comments on commit 2191bd6

Please sign in to comment.