Skip to content

Commit 222c0bd

Browse files
authored
refactor: remove main mapping id from migration fix (#83)
1 parent ac67406 commit 222c0bd

File tree

10 files changed

+169
-69
lines changed

10 files changed

+169
-69
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* (c) shopware AG <[email protected]>
4+
* For the full copyright and license information, please view the LICENSE
5+
* file that was distributed with this source code.
6+
*/
7+
8+
namespace SwagMigrationAssistant\Core\Migration;
9+
10+
use Doctrine\DBAL\Connection;
11+
use Shopware\Core\Framework\Log\Package;
12+
use Shopware\Core\Framework\Migration\MigrationStep;
13+
14+
/**
15+
* @internal
16+
*/
17+
#[Package('fundamentals@after-sales')]
18+
class Migration1762436233RemoveMainMappingIdFromMigrationFix extends MigrationStep
19+
{
20+
public const TABLE_NAME = 'swag_migration_fix';
21+
public const COLUMN_NAME = 'main_mapping_id';
22+
public const FOREIGN_KEY_NAME = 'fk.swag_migration_fix.main_mapping_id';
23+
24+
public function getCreationTimestamp(): int
25+
{
26+
return 1762436233;
27+
}
28+
29+
/**
30+
* @throws \Throwable
31+
*/
32+
public function update(Connection $connection): void
33+
{
34+
$this->dropForeignKeyIfExists(
35+
$connection,
36+
self::TABLE_NAME,
37+
self::FOREIGN_KEY_NAME,
38+
);
39+
40+
$this->dropColumnIfExists(
41+
$connection,
42+
self::TABLE_NAME,
43+
self::COLUMN_NAME,
44+
);
45+
}
46+
}

src/Migration/MigrationFix/SwagMigrationFixDefinition.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ protected function defineFields(): FieldCollection
4343
return new FieldCollection([
4444
(new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
4545
(new IdField('connection_id', 'connectionId'))->addFlags(new Required()),
46-
(new IdField('main_mapping_id', 'mainMappingId'))->addFlags(new Required()),
4746
(new AnyJsonField('value', 'value'))->addFlags(new Required()),
4847
(new StringField('path', 'path'))->addFlags(new Required()),
4948
new IdField('entity_id', 'entityId'),

src/Migration/MigrationFix/SwagMigrationFixEntity.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class SwagMigrationFixEntity extends Entity
1818

1919
protected string $connectionId;
2020

21-
protected string $mainMappingId;
22-
2321
protected mixed $value;
2422

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

41-
public function getMainMappingId(): string
42-
{
43-
return $this->mainMappingId;
44-
}
45-
46-
public function setMainMappingId(string $mainMappingId): void
47-
{
48-
$this->mainMappingId = $mainMappingId;
49-
}
50-
5139
public function getValue(): mixed
5240
{
5341
return $this->value;

src/Migration/Writer/MigrationFix/MigrationFixApplier.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ public function apply(array &$data, string $connectionId): void
5151
private function getMappings(array $ids, string $connectionId): array
5252
{
5353
$sql = <<<'SQL'
54-
SELECT mapping.entity_id as entityId, fix.id, fix.value, fix.path FROM swag_migration_mapping as mapping
55-
INNER JOIN swag_migration_fix as fix ON fix.main_mapping_id = mapping.id
56-
WHERE mapping.entity_id IN (:ids)
57-
AND mapping.connection_id = :connectionId
54+
SELECT fix.entity_id as entityId, fix.id, fix.value, fix.path FROM swag_migration_fix as fix
55+
WHERE fix.entity_id IN (:ids)
56+
AND fix.connection_id = :connectionId
5857
SQL;
5958

6059
$result = $this->connection->fetchAllAssociative(
@@ -71,6 +70,7 @@ private function getMappings(array $ids, string $connectionId): array
7170
$return = [];
7271
foreach ($result as $row) {
7372
$entityId = Uuid::fromBytesToHex($row['entityId']);
73+
7474
if (!\array_key_exists($entityId, $return)) {
7575
$return[$entityId] = [];
7676
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* (c) shopware AG <[email protected]>
4+
* For the full copyright and license information, please view the LICENSE
5+
* file that was distributed with this source code.
6+
*/
7+
8+
namespace Core\Migration;
9+
10+
use PHPUnit\Framework\TestCase;
11+
use Shopware\Core\Framework\Log\Package;
12+
use Shopware\Core\Framework\Test\TestCaseBase\KernelLifecycleManager;
13+
use SwagMigrationAssistant\Core\Migration\Migration1762436233RemoveMainMappingIdFromMigrationFix;
14+
use SwagMigrationAssistant\Test\TableHelperTrait;
15+
16+
/**
17+
* @internal
18+
*/
19+
#[Package('fundamentals@after-sales')]
20+
class Migration1762436233RemoveMainMappingIdFromMigrationFixTest extends TestCase
21+
{
22+
use TableHelperTrait;
23+
24+
public function testUpdate(): void
25+
{
26+
$connection = KernelLifecycleManager::getConnection();
27+
28+
$tableName = Migration1762436233RemoveMainMappingIdFromMigrationFix::TABLE_NAME;
29+
$columnName = Migration1762436233RemoveMainMappingIdFromMigrationFix::COLUMN_NAME;
30+
$foreignKeyName = Migration1762436233RemoveMainMappingIdFromMigrationFix::FOREIGN_KEY_NAME;
31+
32+
if (!$this->columnExists($connection, $tableName, $columnName)) {
33+
$this->addColumn(
34+
$connection,
35+
$tableName,
36+
$columnName,
37+
'BINARY(16)',
38+
);
39+
40+
$this->addForeignKey(
41+
$connection,
42+
$tableName,
43+
$foreignKeyName,
44+
$columnName,
45+
'swag_migration_mapping',
46+
'id',
47+
);
48+
}
49+
50+
static::assertTrue($this->columnExists($connection, $tableName, $columnName));
51+
52+
$migration = new Migration1762436233RemoveMainMappingIdFromMigrationFix();
53+
$migration->update($connection);
54+
$migration->update($connection);
55+
56+
static::assertFalse($this->columnExists($connection, $tableName, $columnName));
57+
}
58+
}

tests/Profile/Shopware6/Converter/ShippingMethodConverterTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ protected function createConverter(
3232
MediaFileServiceInterface $mediaFileService,
3333
?array $mappingArray = [],
3434
): ConverterInterface {
35+
$primaryKey = Uuid::randomHex();
36+
3537
/** @var StaticEntityRepository<ShippingMethodCollection> $shippingMethodRepository */
3638
$shippingMethodRepository = new StaticEntityRepository([
3739
new IdSearchResult(
3840
1, // trigger already existing technical name check
39-
[[
40-
'primaryKey' => Uuid::randomHex(),
41+
[$primaryKey => [
42+
'primaryKey' => $primaryKey,
4143
'data' => [],
4244
]],
4345
new Criteria(),

tests/TableHelperTrait.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,54 @@ protected function dropIndex(Connection $connection, string $table, string $inde
5252

5353
$connection->executeStatement($sql);
5454
}
55+
56+
protected function foreignKeyExists(Connection $connection, string $table, string $foreignKeyName): bool
57+
{
58+
$exists = $connection->fetchOne(
59+
'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',
60+
[
61+
'tableName' => $table,
62+
'constraintName' => $foreignKeyName,
63+
]
64+
);
65+
66+
return !empty($exists);
67+
}
68+
69+
protected function addForeignKey(
70+
Connection $connection,
71+
string $table,
72+
string $foreignKeyName,
73+
string $column,
74+
string $referencedTable,
75+
string $referencedColumn,
76+
string $onDelete = 'CASCADE',
77+
): void {
78+
if ($this->foreignKeyExists($connection, $table, $foreignKeyName)) {
79+
return;
80+
}
81+
82+
$sql = \sprintf(
83+
'ALTER TABLE `%s` ADD CONSTRAINT `%s` FOREIGN KEY (`%s`) REFERENCES `%s` (`%s`) ON DELETE %s',
84+
$table,
85+
$foreignKeyName,
86+
$column,
87+
$referencedTable,
88+
$referencedColumn,
89+
$onDelete
90+
);
91+
92+
$connection->executeStatement($sql);
93+
}
94+
95+
protected function dropForeignKey(Connection $connection, string $table, string $foreignKeyName): void
96+
{
97+
if (!$this->foreignKeyExists($connection, $table, $foreignKeyName)) {
98+
return;
99+
}
100+
101+
$sql = \sprintf('ALTER TABLE `%s` DROP FOREIGN KEY `%s`', $table, $foreignKeyName);
102+
103+
$connection->executeStatement($sql);
104+
}
55105
}

tests/integration/Migration/MigrationFix/SwagMigrationEntityTest.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Shopware\Core\Framework\Test\TestCaseBase\KernelTestBehaviour;
1717
use Shopware\Core\Framework\Uuid\Uuid;
1818
use SwagMigrationAssistant\Migration\Connection\SwagMigrationConnectionCollection;
19-
use SwagMigrationAssistant\Migration\Mapping\MappingService;
2019
use SwagMigrationAssistant\Migration\MigrationContext;
2120
use SwagMigrationAssistant\Migration\MigrationFix\SwagMigrationFixCollection;
2221
use SwagMigrationAssistant\Migration\MigrationFix\SwagMigrationFixEntity;
@@ -37,28 +36,23 @@ class SwagMigrationEntityTest extends TestCase
3736
*/
3837
private EntityRepository $connectionRepository;
3938

40-
private MappingService $mappingService;
41-
4239
protected function setUp(): void
4340
{
4441
$this->migrationFixRepository = $this->getContainer()->get('swag_migration_fix.repository');
4542
$this->connectionRepository = $this->getContainer()->get('swag_migration_connection.repository');
46-
$this->mappingService = $this->getContainer()->get(MappingService::class);
4743
}
4844

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

5651
$fixId = Uuid::randomHex();
5752

5853
$migrationFix = new SwagMigrationFixEntity();
5954
$migrationFix->setId($fixId);
6055
$migrationFix->setConnectionId($connectionId);
61-
$migrationFix->setMainMappingId($mappingId);
6256
$migrationFix->setPath('this.is.any.path');
6357
$migrationFix->setValue($value);
6458

@@ -87,23 +81,6 @@ public static function valueData(): array
8781
];
8882
}
8983

90-
private function createMapping(string $connectionId): string
91-
{
92-
$mapping = $this->mappingService->createMapping(
93-
$connectionId,
94-
'any',
95-
'old_id_1',
96-
null,
97-
null,
98-
Uuid::randomHex(),
99-
'value'
100-
);
101-
102-
$this->mappingService->writeMapping();
103-
104-
return $mapping['id'];
105-
}
106-
10784
private function createConnection(Context $context): string
10885
{
10986
$connectionId = Uuid::randomHex();

tests/integration/Migration/Writer/MigrationFix/MigrationFixApplierTest.php

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,6 @@ public function testApply(): void
4949

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

52-
$mappingOne = $this->mappingService->createMapping(
53-
$connectionId,
54-
'any',
55-
'old_id_1',
56-
null,
57-
null,
58-
$idOne,
59-
'value'
60-
);
61-
62-
$mappingTwo = $this->mappingService->createMapping(
63-
$connectionId,
64-
'any_other',
65-
'old_id_2',
66-
null,
67-
null,
68-
$idTwo,
69-
'any other value'
70-
);
71-
7252
// create also mapping without fix
7353
$this->mappingService->createMapping(
7454
$connectionId,
@@ -82,11 +62,11 @@ public function testApply(): void
8262

8363
$this->mappingService->writeMapping();
8464

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

88-
$this->createFix($mappingTwo['id'], $connectionId, 'val3', 'third.path');
89-
$this->createFix($mappingTwo['id'], $connectionId, 'val4', 'fourth.other.path');
68+
$this->createFix($connectionId, $idTwo, 'val3', 'third.path');
69+
$this->createFix($connectionId, $idTwo, 'val4', 'fourth.other.path');
9070

9171
$data = [
9272
[
@@ -130,12 +110,12 @@ public function testApply(): void
130110
static::assertSame($expected, $data);
131111
}
132112

133-
private function createFix(string $mappingId, string $connectionId, mixed $value, string $path): void
113+
private function createFix(string $connectionId, string $entityId, mixed $value, string $path): void
134114
{
135115
$migrationFix = new SwagMigrationFixEntity();
136116
$migrationFix->setId(Uuid::randomHex());
137117
$migrationFix->setConnectionId($connectionId);
138-
$migrationFix->setMainMappingId($mappingId);
118+
$migrationFix->setEntityId($entityId);
139119
$migrationFix->setPath($path);
140120
$migrationFix->setValue($value);
141121

tests/unit/Migration/Writer/MigrationFix/MigrationFixTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public static function dataWithMissingKeys(): array
124124
'data' => [
125125
'id' => 'anyIdentifier',
126126
'connection_id' => 'anyConnectionIdentifier',
127-
'main_mapping_id' => 'anyMappingId',
127+
'entity_id' => 'anyEntityId',
128128
'value' => json_encode('anyValue', \JSON_THROW_ON_ERROR),
129129
],
130130
'expectedMissingKey' => 'path',

0 commit comments

Comments
 (0)