Skip to content

Commit

Permalink
fix(database): fix database indexes and keys
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Nov 7, 2023
1 parent 5e92f23 commit 5ddb09d
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/Database/CreateCarrierMappingTableDatabaseMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function up(): void
->id('carrier_id')
->column('myparcel_carrier', 'VARCHAR(32)')
->timestamps()
->primary(['carrier_id', 'myparcel_carrier']);
->primary(['carrier_id'])
->unique(['myparcel_carrier']);

$this->execute($sql);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Database/CreateOrderShipmentTableDatabaseMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MyParcelNL\PrestaShop\Database;

use MyParcelNL\PrestaShop\Database\Sql\CreateIndexSqlBuilder;
use MyParcelNL\PrestaShop\Database\Sql\CreateTableSqlBuilder;
use MyParcelNL\PrestaShop\Database\Sql\DropTableSqlBuilder;
use MyParcelNL\PrestaShop\Entity\MyparcelnlOrderShipment;
Expand All @@ -25,9 +26,13 @@ public function up(): void
->id('shipment_id')
->column('data', 'TEXT NOT NULL')
->timestamps()
->primary(['order_id', 'shipment_id']);
->primary(['shipment_id']);

$this->execute($sql);

$indexSql = (new CreateIndexSqlBuilder($this->getTable()))->index('order_id', ['order_id']);

$this->execute($indexSql);
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/Database/Sql/CreateIndexSqlBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\PrestaShop\Database\Sql;

final class CreateIndexSqlBuilder extends SqlBuilder
{
/**
* @var array
*/
private $columns = [];

/**
* @var string
*/
private $index;

public function build(): string
{
return sprintf(
'CREATE INDEX `%s` ON `%s` (%s);',
$this->index,
$this->getTable(),
implode(', ', $this->columns)
);
}

public function index(string $name, array $columns): self
{
$this->index = $name;
$this->columns = $columns;

return $this;
}
}
23 changes: 22 additions & 1 deletion src/Database/Sql/CreateTableSqlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ final class CreateTableSqlBuilder extends SqlBuilder
*/
private $primary = [];

/**
* @var array
*/
private $unique = [];

public function build(): string
{
$rows = array_map(static function ($column) {
Expand All @@ -33,8 +38,12 @@ public function build(): string
$string .= sprintf(', PRIMARY KEY (%s)', implode(', ', $this->primary));
}

if (! empty($this->unique)) {
$string .= sprintf(', UNIQUE KEY (%s)', implode(', ', $this->unique));
}

return sprintf(
'CREATE TABLE if NOT EXISTS `%s` (%s) ENGINE=%s DEFAULT CHARSET=utf8;',
'CREATE TABLE IF NOT EXISTS `%s` (%s) ENGINE=%s DEFAULT CHARSET=utf8;',
$this->getTable(),
$string,
$this->getEngine()
Expand Down Expand Up @@ -95,4 +104,16 @@ public function timestamps(): self
->column('date_add', 'DATETIME')
->column('date_upd', 'DATETIME');
}

/**
* @param array $keys
*
* @return $this
*/
public function unique(array $keys): self
{
$this->unique = $keys;

return $this;
}
}
18 changes: 18 additions & 0 deletions tests/Unit/Database/Sql/CreateIndexSqlBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

namespace MyParcelNL\PrestaShop\Database\Sql;

it('creates sql', function () {
$builder = new CreateIndexSqlBuilder('table');

$builder->index('index_123', ['column1', 'column2']);

$result = <<<SQL
CREATE INDEX `index_123` ON `table` (column1, column2);
SQL;

expect($builder->build())->toEqual($result);
});
58 changes: 49 additions & 9 deletions tests/Unit/Database/Sql/CreateTableSqlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,57 @@

use MyParcelNL\PrestaShop\Database\Sql\CreateTableSqlBuilder;

it('builds sql', function () {
it('builds sql', function (callable $callback, string $expected) {
$builder = new CreateTableSqlBuilder('table_name');

$builder->id('id');
$builder->column('name', 'VARCHAR(255)');
$builder->column('nullable', 'VARCHAR(255)', true);
$builder->primary(['id']);
$callback($builder);

$expected = <<<SQL
CREATE TABLE if NOT EXISTS `ps_table_name` (`id` INT(10) unsigned NOT NULL, `name` VARCHAR(255) NOT NULL, `nullable` VARCHAR(255) NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
expect($builder->build())->toBe($expected);
})->with([
'2 varchar columns' => [
'builder' => function () {
return function (CreateTableSqlBuilder $builder) {
$builder->id('id');
$builder->column('name', 'VARCHAR(255)');
$builder->column('nullable', 'VARCHAR(255)', true);
};
},
'result' => function () {
return <<<SQL
CREATE TABLE IF NOT EXISTS `ps_table_name` (`id` INT(10) unsigned NOT NULL, `name` VARCHAR(255) NOT NULL, `nullable` VARCHAR(255) NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SQL;
},
],

expect($builder->build())->toBe($expected);
});
'primary key' => [
'builder' => function () {
return function (CreateTableSqlBuilder $builder) {
$builder->id('id');
$builder->column('name', 'VARCHAR(255)');
$builder->column('deleted', 'TINYINT(1)');
$builder->primary(['id']);
};
},
'result' => function () {
return <<<SQL
CREATE TABLE IF NOT EXISTS `ps_table_name` (`id` INT(10) unsigned NOT NULL, `name` VARCHAR(255) NOT NULL, `deleted` TINYINT(1) NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SQL;
},
],

'unique index' => [
'builder' => function () {
return function (CreateTableSqlBuilder $builder) {
$builder->id('id');
$builder->column('name', 'VARCHAR(255)');
$builder->column('deleted', 'TINYINT(1)');
$builder->unique(['id', 'name']);
};
},
'result' => function () {
return <<<SQL
CREATE TABLE IF NOT EXISTS `ps_table_name` (`id` INT(10) unsigned NOT NULL, `name` VARCHAR(255) NOT NULL, `deleted` TINYINT(1) NOT NULL, UNIQUE KEY (id, name)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SQL;
},
],
]);

0 comments on commit 5ddb09d

Please sign in to comment.