Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions src/Migration/Writer/MigrationFix/MigrationFixApplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ public function apply(array &$data, string $connectionId): void
private function getMappings(array $ids, string $connectionId): array
{
$sql = <<<'SQL'
SELECT mapping.entity_id as entityId, fix.id, fix.value, fix.path FROM swag_migration_mapping as mapping
INNER JOIN swag_migration_fix as fix ON fix.main_mapping_id = mapping.id
WHERE mapping.entity_id IN (:ids)
AND mapping.connection_id = :connectionId
SELECT fix.entity_id as entityId, fix.id, fix.value, fix.path FROM swag_migration_fix as fix
WHERE fix.entity_id IN (:ids)
AND fix.connection_id = :connectionId
SQL;

$result = $this->connection->fetchAllAssociative(
Expand All @@ -71,6 +70,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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ protected function createConverter(
MediaFileServiceInterface $mediaFileService,
?array $mappingArray = [],
): ConverterInterface {
$primaryKey = Uuid::randomHex();

/** @var StaticEntityRepository<ShippingMethodCollection> $shippingMethodRepository */
$shippingMethodRepository = new StaticEntityRepository([
new IdSearchResult(
1, // trigger already existing technical name check
[[
'primaryKey' => Uuid::randomHex(),
[$primaryKey => [
'primaryKey' => $primaryKey,
'data' => [],
]],
new Criteria(),
Expand Down
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 @@ -16,7 +16,6 @@
use Shopware\Core\Framework\Test\TestCaseBase\KernelTestBehaviour;
use Shopware\Core\Framework\Uuid\Uuid;
use SwagMigrationAssistant\Migration\Connection\SwagMigrationConnectionCollection;
use SwagMigrationAssistant\Migration\Mapping\MappingService;
use SwagMigrationAssistant\Migration\MigrationContext;
use SwagMigrationAssistant\Migration\MigrationFix\SwagMigrationFixCollection;
use SwagMigrationAssistant\Migration\MigrationFix\SwagMigrationFixEntity;
Expand All @@ -37,28 +36,23 @@ class SwagMigrationEntityTest extends TestCase
*/
private EntityRepository $connectionRepository;

private MappingService $mappingService;

protected function setUp(): void
{
$this->migrationFixRepository = $this->getContainer()->get('swag_migration_fix.repository');
$this->connectionRepository = $this->getContainer()->get('swag_migration_connection.repository');
$this->mappingService = $this->getContainer()->get(MappingService::class);
}

#[DataProvider('valueData')]
public function testGetAndSetValue(mixed $value): void
{
$context = Context::createDefaultContext();
$connectionId = $this->createConnection($context);
$mappingId = $this->createMapping($connectionId);

$fixId = Uuid::randomHex();

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

Expand Down Expand Up @@ -87,23 +81,6 @@ public static function valueData(): array
];
}

private function createMapping(string $connectionId): string
{
$mapping = $this->mappingService->createMapping(
$connectionId,
'any',
'old_id_1',
null,
null,
Uuid::randomHex(),
'value'
);

$this->mappingService->writeMapping();

return $mapping['id'];
}

private function createConnection(Context $context): string
{
$connectionId = Uuid::randomHex();
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