Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types=1);
/*
* (c) shopware AG <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SwagMigrationAssistant\Core\Migration;

use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Migration\MigrationStep;

/**
* @internal
*/
#[Package('fundamentals@after-sales')]
class Migration1762436233RemoveMainMappingIdFromMigrationFix extends MigrationStep
{
public const TABLE_NAME = 'swag_migration_fix';
public const COLUMN_NAME = 'main_mapping_id';
public const FOREIGN_KEY_NAME = 'fk.swag_migration_fix.main_mapping_id';

public function getCreationTimestamp(): int
{
return 1762436233;
}

/**
* @throws \Throwable
*/
public function update(Connection $connection): void
{
$this->dropForeignKeyIfExists(
$connection,
self::TABLE_NAME,
self::FOREIGN_KEY_NAME,
);

$this->dropColumnIfExists(
$connection,
self::TABLE_NAME,
self::COLUMN_NAME,
);
}
}
1 change: 0 additions & 1 deletion src/Migration/MigrationFix/SwagMigrationFixDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ protected function defineFields(): FieldCollection
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
(new IdField('connection_id', 'connectionId'))->addFlags(new Required()),
(new IdField('main_mapping_id', 'mainMappingId'))->addFlags(new Required()),
(new AnyJsonField('value', 'value'))->addFlags(new Required()),
(new StringField('path', 'path'))->addFlags(new Required()),
new IdField('entity_id', 'entityId'),
Expand Down
12 changes: 0 additions & 12 deletions src/Migration/MigrationFix/SwagMigrationFixEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class SwagMigrationFixEntity extends Entity

protected string $connectionId;

protected string $mainMappingId;

protected mixed $value;

protected string $path;
Expand All @@ -38,16 +36,6 @@ public function setConnectionId(string $connectionId): void
$this->connectionId = $connectionId;
}

public function getMainMappingId(): string
{
return $this->mainMappingId;
}

public function setMainMappingId(string $mainMappingId): void
{
$this->mainMappingId = $mainMappingId;
}

public function getValue(): mixed
{
return $this->value;
Expand Down
1 change: 1 addition & 0 deletions src/Migration/Writer/MigrationFix/MigrationFixApplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private function getMappings(array $ids, string $connectionId): array
$return = [];
foreach ($result as $row) {
$entityId = Uuid::fromBytesToHex($row['entityId']);

if (!\array_key_exists($entityId, $return)) {
$return[$entityId] = [];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types=1);
/*
* (c) shopware AG <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core\Migration;

use PHPUnit\Framework\TestCase;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Test\TestCaseBase\KernelLifecycleManager;
use SwagMigrationAssistant\Core\Migration\Migration1762436233RemoveMainMappingIdFromMigrationFix;
use SwagMigrationAssistant\Test\TableHelperTrait;

/**
* @internal
*/
#[Package('fundamentals@after-sales')]
class Migration1762436233RemoveMainMappingIdFromMigrationFixTest extends TestCase
{
use TableHelperTrait;

public function testUpdate(): void
{
$connection = KernelLifecycleManager::getConnection();

$tableName = Migration1762436233RemoveMainMappingIdFromMigrationFix::TABLE_NAME;
$columnName = Migration1762436233RemoveMainMappingIdFromMigrationFix::COLUMN_NAME;
$foreignKeyName = Migration1762436233RemoveMainMappingIdFromMigrationFix::FOREIGN_KEY_NAME;

if (!$this->columnExists($connection, $tableName, $columnName)) {
$this->addColumn(
$connection,
$tableName,
$columnName,
'BINARY(16)',
);

$this->addForeignKey(
$connection,
$tableName,
$foreignKeyName,
$columnName,
'swag_migration_mapping',
'id',
);
}

static::assertTrue($this->columnExists($connection, $tableName, $columnName));

$migration = new Migration1762436233RemoveMainMappingIdFromMigrationFix();
$migration->update($connection);
$migration->update($connection);

static::assertFalse($this->columnExists($connection, $tableName, $columnName));
}
}
50 changes: 50 additions & 0 deletions tests/TableHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,54 @@ protected function dropIndex(Connection $connection, string $table, string $inde

$connection->executeStatement($sql);
}

protected function foreignKeyExists(Connection $connection, string $table, string $foreignKeyName): bool
{
$exists = $connection->fetchOne(
'SELECT CONSTRAINT_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = :tableName AND CONSTRAINT_NAME = :constraintName AND REFERENCED_TABLE_NAME IS NOT NULL',
[
'tableName' => $table,
'constraintName' => $foreignKeyName,
]
);

return !empty($exists);
}

protected function addForeignKey(
Connection $connection,
string $table,
string $foreignKeyName,
string $column,
string $referencedTable,
string $referencedColumn,
string $onDelete = 'CASCADE',
): void {
if ($this->foreignKeyExists($connection, $table, $foreignKeyName)) {
return;
}

$sql = \sprintf(
'ALTER TABLE `%s` ADD CONSTRAINT `%s` FOREIGN KEY (`%s`) REFERENCES `%s` (`%s`) ON DELETE %s',
$table,
$foreignKeyName,
$column,
$referencedTable,
$referencedColumn,
$onDelete
);

$connection->executeStatement($sql);
}

protected function dropForeignKey(Connection $connection, string $table, string $foreignKeyName): void
{
if (!$this->foreignKeyExists($connection, $table, $foreignKeyName)) {
return;
}

$sql = \sprintf('ALTER TABLE `%s` DROP FOREIGN KEY `%s`', $table, $foreignKeyName);

$connection->executeStatement($sql);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@
{
$context = Context::createDefaultContext();
$connectionId = $this->createConnection($context);
$mappingId = $this->createMapping($connectionId);

$entityId = $this->createMapping($connectionId);
static::assertNotNull($entityId);

$fixId = Uuid::randomHex();

$migrationFix = new SwagMigrationFixEntity();
$migrationFix->setId($fixId);
$migrationFix->setConnectionId($connectionId);
$migrationFix->setMainMappingId($mappingId);
$migrationFix->setEntityId($entityId);
$migrationFix->setPath('this.is.any.path');
$migrationFix->setValue($value);

Expand Down Expand Up @@ -87,7 +89,7 @@
];
}

private function createMapping(string $connectionId): string
private function createMapping(string $connectionId): ?string
{
$mapping = $this->mappingService->createMapping(
$connectionId,
Expand All @@ -101,7 +103,7 @@

$this->mappingService->writeMapping();

return $mapping['id'];
return $mapping['entityUuid'];

Check failure on line 106 in tests/integration/Migration/MigrationFix/SwagMigrationEntityTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Offset 'entityUuid' does not exist on array{id: string, connectionId: string, oldIdentifier: string|null, entityId: string|null, entityValue: string|null, checksum: string|null, additionalData: array<mixed>|null}.
}

private function createConnection(Context $context): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,6 @@ public function testApply(): void

$fixApplier = new MigrationFixApplier($this->getContainer()->get(Connection::class));

$mappingOne = $this->mappingService->createMapping(
$connectionId,
'any',
'old_id_1',
null,
null,
$idOne,
'value'
);

$mappingTwo = $this->mappingService->createMapping(
$connectionId,
'any_other',
'old_id_2',
null,
null,
$idTwo,
'any other value'
);

// create also mapping without fix
$this->mappingService->createMapping(
$connectionId,
Expand All @@ -82,11 +62,11 @@ public function testApply(): void

$this->mappingService->writeMapping();

$this->createFix($mappingOne['id'], $connectionId, 'val1', 'first.path');
$this->createFix($mappingOne['id'], $connectionId, ['nested' => ['array' => ['value' => 'nested array value']]], 'second.other.path');
$this->createFix($connectionId, $idOne, 'val1', 'first.path');
$this->createFix($connectionId, $idOne, ['nested' => ['array' => ['value' => 'nested array value']]], 'second.other.path');

$this->createFix($mappingTwo['id'], $connectionId, 'val3', 'third.path');
$this->createFix($mappingTwo['id'], $connectionId, 'val4', 'fourth.other.path');
$this->createFix($connectionId, $idTwo, 'val3', 'third.path');
$this->createFix($connectionId, $idTwo, 'val4', 'fourth.other.path');

$data = [
[
Expand Down Expand Up @@ -130,12 +110,12 @@ public function testApply(): void
static::assertSame($expected, $data);
}

private function createFix(string $mappingId, string $connectionId, mixed $value, string $path): void
private function createFix(string $connectionId, string $entityId, mixed $value, string $path): void
{
$migrationFix = new SwagMigrationFixEntity();
$migrationFix->setId(Uuid::randomHex());
$migrationFix->setConnectionId($connectionId);
$migrationFix->setMainMappingId($mappingId);
$migrationFix->setEntityId($entityId);
$migrationFix->setPath($path);
$migrationFix->setValue($value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static function dataWithMissingKeys(): array
'data' => [
'id' => 'anyIdentifier',
'connection_id' => 'anyConnectionIdentifier',
'main_mapping_id' => 'anyMappingId',
'entity_id' => 'anyEntityId',
'value' => json_encode('anyValue', \JSON_THROW_ON_ERROR),
],
'expectedMissingKey' => 'path',
Expand Down
Loading