Skip to content

Commit

Permalink
fix(migrations): improve migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Sep 22, 2023
1 parent 2df8009 commit 148fceb
Show file tree
Hide file tree
Showing 33 changed files with 1,764 additions and 1,147 deletions.
24 changes: 0 additions & 24 deletions src/Database/DatabaseMigrations.php

This file was deleted.

58 changes: 0 additions & 58 deletions src/Migration/AbstractLegacyPsMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace MyParcelNL\PrestaShop\Migration;

use Db;
use DbQuery;
use MyParcelNL\Pdk\Base\Support\Collection;
use MyParcelNL\PrestaShop\Database\Table;

abstract class AbstractLegacyPsMigration extends AbstractPsMigration
Expand All @@ -21,35 +18,6 @@ public function down(): void
// do nothing
}

protected function deleteWhere(string $table, string $column, array $values): void
{
$valuesString = implode("', '", $values);
$query = "DELETE FROM `$table` WHERE `$column` IN ('$valuesString')";

$this->db->execute($query);
}

/**
* @param string $from
* @param callable|null $callback
*
* @return \MyParcelNL\Pdk\Base\Support\Collection
* @throws \PrestaShopDatabaseException
*/
protected function getAll(string $from, callable $callback = null): Collection
{
$query = new DbQuery();
$query
->select('*')
->from($from);

if ($callback) {
$callback($query);
}

return $this->getRows($query);
}

final protected function getCarrierConfigurationTable(): string
{
return Table::withPrefix(self::LEGACY_TABLE_CARRIER_CONFIGURATION);
Expand All @@ -69,30 +37,4 @@ final protected function getProductConfigurationTable(): string
{
return Table::withPrefix(self::LEGACY_TABLE_PRODUCT_CONFIGURATION);
}

/**
* @param string|DbQuery $query
*
* @return \MyParcelNL\Pdk\Base\Support\Collection
* @throws \PrestaShopDatabaseException
*/
protected function getRows($query): Collection
{
return new Collection($this->db->executeS($query));
}

/**
* @param string $table
* @param array $records
* @param bool $useReplace
*
* @return void
* @throws \PrestaShopDatabaseException
*/
protected function insert(string $table, array $records, bool $useReplace = true): void
{
$this->db->insert($table, $records, false, false, $useReplace ? Db::REPLACE : Db::INSERT);
}

protected function insertRecords(): void {}
}
79 changes: 79 additions & 0 deletions src/Migration/AbstractPsMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace MyParcelNL\PrestaShop\Migration;

use Db;
use DbQuery;
use MyParcelNL\Pdk\App\Installer\Contract\MigrationInterface;
use MyParcelNL\Pdk\Base\Support\Collection;

abstract class AbstractPsMigration implements MigrationInterface
{
Expand All @@ -18,4 +20,81 @@ public function __construct()
{
$this->db = Db::getInstance();
}

/**
* @param string $table
* @param string $column
* @param array $values
*
* @return void
*/
protected function deleteWhere(string $table, string $column, array $values): void
{
$valuesString = implode("', '", $values);
$query = "DELETE FROM `$table` WHERE `$column` IN ('$valuesString')";

$this->db->execute($query);
}

/**
* @param string $from
* @param callable|null $callback
*
* @return \MyParcelNL\Pdk\Base\Support\Collection
* @throws \PrestaShopDatabaseException
*/
protected function getAllRows(string $from, callable $callback = null): Collection
{
$query = new DbQuery();
$query
->select('*')
->from($from);

if ($callback) {
$callback($query);
}

return $this->getRows($query);
}

/**
* @param string $table
* @param string $column
* @param string $where
*
* @return mixed
*/
protected function getDbValue(string $table, string $column, string $where)
{
$query = new DbQuery();
$query->select($column);
$query->from($table);
$query->where($where);

return $this->db->getValue($query) ?: null;
}

/**
* @param string|DbQuery $query
*
* @return \MyParcelNL\Pdk\Base\Support\Collection
* @throws \PrestaShopDatabaseException
*/
protected function getRows($query): Collection
{
return new Collection($this->db->executeS($query));
}

/**
* @param string $table
* @param array $records
* @param bool $useReplace
*
* @return void
* @throws \PrestaShopDatabaseException
*/
protected function insertRows(string $table, array $records, bool $useReplace = true): void
{
$this->db->insert($table, $records, false, false, $useReplace ? Db::REPLACE : Db::INSERT);
}
}
2 changes: 1 addition & 1 deletion src/Migration/Migration1_1_2.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function up(): void
];
}

$this->insert($table, $newRecords);
$this->insertRows($table, $newRecords);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Migration/Migration1_4_0.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private function migrateCarrierTitlesToCheckoutConfiguration(): void

$prestashopCarrierId = CarrierService::getPrestaShopCarrierId($defaultCarrier);

$carrierConfigurationRows = $this->getAll(AbstractLegacyPsMigration::LEGACY_TABLE_CARRIER_CONFIGURATION);
$carrierConfigurationRows = $this->getAllRows(AbstractLegacyPsMigration::LEGACY_TABLE_CARRIER_CONFIGURATION);

$anyCarrierNonEmpty = $carrierConfigurationRows
->whereIn('name', array_keys(self::CARRIER_CHECKOUT_SETTINGS_MAP))
Expand All @@ -110,7 +110,7 @@ private function migrateCarrierTitlesToCheckoutConfiguration(): void
$newConfigurations = [];
$oldCarrierConfigurationNames = [];

$existingConfigurations = $this->getAll('configuration');
$existingConfigurations = $this->getAllRows('configuration');

foreach (self::CARRIER_CHECKOUT_SETTINGS_MAP as $oldKey => $newKey) {
$valueFromDefaultCarrier = $this->getFirstValue($defaultCarrierNonEmpty, $oldKey);
Expand All @@ -131,7 +131,7 @@ private function migrateCarrierTitlesToCheckoutConfiguration(): void
$oldCarrierConfigurationNames[] = $oldKey;
}

$this->insert('configuration', $newConfigurations);
$this->insertRows('configuration', $newConfigurations);
$this->deleteWhere($table, 'name', $oldCarrierConfigurationNames);
}
}
6 changes: 3 additions & 3 deletions src/Migration/Migration1_6_0.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private function addInsuranceBelgiumForCarriers(): void
];
}

$this->insert($table, $newRecords);
$this->insertRows($table, $newRecords);
}

/**
Expand All @@ -60,7 +60,7 @@ private function addInsuranceBelgiumForCarriers(): void
private function addInsuranceOptionsForCarriers(): void
{
$table = $this->getCarrierConfigurationTable();
$records = $this->getAll($table, function (DbQuery $query) {
$records = $this->getAllRows($table, function (DbQuery $query) {
$query->where('name like %MYPARCELNL_INSURANCE_');
});

Expand Down Expand Up @@ -94,6 +94,6 @@ private function addInsuranceOptionsForCarriers(): void
];
}

$this->insert($table, $newRecords);
$this->insertRows($table, $newRecords);
}
}
4 changes: 2 additions & 2 deletions src/Migration/Migration1_8_0.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function up(): void
private function addInsuranceOptionsEuForCarriers(): void
{
$table = $this->getCarrierConfigurationTable();
$records = $this->getAll($table, function (DbQuery $query) {
$records = $this->getAllRows($table, function (DbQuery $query) {
$query->where("`name` LIKE '%MYPARCELBE_INSURANCE'");
});

Expand All @@ -49,6 +49,6 @@ private function addInsuranceOptionsEuForCarriers(): void
];
}

$this->insert($table, $newRecords);
$this->insertRows($table, $newRecords);
}
}
Loading

0 comments on commit 148fceb

Please sign in to comment.