Skip to content

Commit d2af880

Browse files
J7FJ7F
authored andcommitted
PES-2788: uninstall may not delete tables
1 parent 94d0cf6 commit d2af880

File tree

6 files changed

+38
-14
lines changed

6 files changed

+38
-14
lines changed

packetery/libs/ApiCarrier/ApiCarrierRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function save(array $carriers, Packetery $module)
152152

153153
public function getCreateTableSql()
154154
{
155-
return 'CREATE TABLE `' . $this->getPrefixedTableName() . '` (
155+
return 'CREATE TABLE IF NOT EXISTS `' . $this->getPrefixedTableName() . '` (
156156
`id` varchar(255) NOT NULL,
157157
`name` varchar(255) NOT NULL,
158158
`is_pickup_points` boolean NOT NULL,

packetery/libs/Log/LogRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function getDropTableSql()
120120
*/
121121
public function getCreateTableSql()
122122
{
123-
return 'CREATE TABLE ' . $this->getPrefixedTableName() . ' (
123+
return 'CREATE TABLE IF NOT EXISTS ' . $this->getPrefixedTableName() . ' (
124124
`id` int(11) NOT NULL AUTO_INCREMENT,
125125
`order_id` int(10) NULL,
126126
`params` text NOT NULL,

packetery/libs/Module/Installer.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,13 @@ private function createMultiLangField($translationKey)
140140
private function installDatabase()
141141
{
142142
$sql = [];
143+
// DB migrations do not work with PACKETERY_REMOVE_ALL_DATA set to false
144+
$dropTables = !defined('PACKETERY_REMOVE_ALL_DATA') || PACKETERY_REMOVE_ALL_DATA;
143145

144-
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'packetery_order`';
145-
$sql[] = 'CREATE TABLE `' . _DB_PREFIX_ . 'packetery_order` (
146+
if ($dropTables) {
147+
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'packetery_order`';
148+
}
149+
$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'packetery_order` (
146150
`id_order` int,
147151
`id_cart` int,
148152
`id_branch` int NULL,
@@ -173,14 +177,18 @@ private function installDatabase()
173177
UNIQUE(`id_cart`)
174178
) ENGINE=InnoDB DEFAULT CHARSET=utf8;';
175179

176-
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'packetery_payment`';
177-
$sql[] = 'CREATE TABLE `' . _DB_PREFIX_ . 'packetery_payment` (
180+
if ($dropTables) {
181+
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'packetery_payment`';
182+
}
183+
$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'packetery_payment` (
178184
`module_name` varchar(255) NOT NULL PRIMARY KEY,
179185
`is_cod` tinyint(1) NOT NULL DEFAULT 0
180186
) ENGINE=InnoDB DEFAULT charset=utf8;';
181187

182-
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'packetery_address_delivery`';
183-
$sql[] = 'CREATE TABLE `' . _DB_PREFIX_ . 'packetery_address_delivery` (
188+
if ($dropTables) {
189+
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'packetery_address_delivery`';
190+
}
191+
$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'packetery_address_delivery` (
184192
`id_carrier` int NOT NULL PRIMARY KEY,
185193
`id_branch` varchar(255) NOT NULL,
186194
`name_branch` varchar(255) NULL,
@@ -192,19 +200,27 @@ private function installDatabase()
192200
) ENGINE=InnoDB DEFAULT CHARSET=utf8;';
193201

194202
$productAttributeRepository = $this->module->diContainer->get(ProductAttributeRepository::class);
195-
$sql[] = $productAttributeRepository->getDropTableSql();
203+
if ($dropTables) {
204+
$sql[] = $productAttributeRepository->getDropTableSql();
205+
}
196206
$sql[] = $productAttributeRepository->getCreateTableSql();
197207

198208
$apiCarrierRepository = $this->module->diContainer->get(ApiCarrierRepository::class);
199-
$sql[] = $apiCarrierRepository->getDropTableSql();
209+
if ($dropTables) {
210+
$sql[] = $apiCarrierRepository->getDropTableSql();
211+
}
200212
$sql[] = $apiCarrierRepository->getCreateTableSql();
201213

202214
$logRepository = $this->module->diContainer->get(LogRepository::class);
203-
$sql[] = $logRepository->getDropTableSql();
215+
if ($dropTables) {
216+
$sql[] = $logRepository->getDropTableSql();
217+
}
204218
$sql[] = $logRepository->getCreateTableSql();
205219

206220
$packetTrackingRepository = $this->module->diContainer->get(PacketTrackingRepository::class);
207-
$sql[] = $packetTrackingRepository->getDropTableSql();
221+
if ($dropTables) {
222+
$sql[] = $packetTrackingRepository->getDropTableSql();
223+
}
208224
$sql[] = $packetTrackingRepository->getCreateTableSql();
209225

210226
if (!$this->dbTools->executeQueries($sql, $this->getExceptionRaisedText(), true)) {

packetery/libs/Module/Uninstaller.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public function deleteTab($className)
8787
*/
8888
private function uninstallDatabase()
8989
{
90+
if (defined('PACKETERY_REMOVE_ALL_DATA') && !PACKETERY_REMOVE_ALL_DATA) {
91+
return true;
92+
}
93+
9094
$sql = [];
9195
// remove tables with payment and carrier settings, and with carriers
9296
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'packetery_payment`';
@@ -140,6 +144,10 @@ private function unregisterHooks()
140144
*/
141145
private function deleteConfiguration()
142146
{
147+
if (defined('PACKETERY_REMOVE_ALL_DATA') && !PACKETERY_REMOVE_ALL_DATA) {
148+
return true;
149+
}
150+
143151
return (
144152
Configuration::deleteByName('PACKETERY_APIPASS') &&
145153
Configuration::deleteByName('PACKETERY_ESHOP_ID') &&

packetery/libs/PacketTracking/PacketTrackingRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function getDropTableSql()
8484
*/
8585
public function getCreateTableSql()
8686
{
87-
return 'CREATE TABLE `' . $this->getPrefixedTableName() . '` (
87+
return 'CREATE TABLE IF NOT EXISTS `' . $this->getPrefixedTableName() . '` (
8888
`id` int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
8989
`id_order` int unsigned NOT NULL,
9090
`packet_id` varchar(15) NOT NULL,

packetery/libs/Product/ProductAttributeRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function getDropTableSql()
118118
*/
119119
public function getCreateTableSql()
120120
{
121-
return 'CREATE TABLE `' . $this->getPrefixedTableName() . '` (
121+
return 'CREATE TABLE IF NOT EXISTS `' . $this->getPrefixedTableName() . '` (
122122
`id_product` int(11) NOT NULL PRIMARY KEY,
123123
`is_adult` tinyint(1) NOT NULL DEFAULT 0
124124
) ENGINE=InnoDB DEFAULT CHARSET=utf8;';

0 commit comments

Comments
 (0)