Skip to content

Commit 9750e4a

Browse files
committed
refactor: cleanup
1 parent 2ddfbfc commit 9750e4a

File tree

24 files changed

+721
-452
lines changed

24 files changed

+721
-452
lines changed

UPGRADE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@
6767
- Added methods `setProfile()`, `getGateway()`, `setGateway()` and `setConnection()` to `SwagMigrationAssistant\Migration\MigrationContext`
6868
- Added null checks to methods `getProfile()` and `getGateway()` in `SwagMigrationAssistant\Migration\MigrationContext` to ensure that a profile and gateway is set before usage
6969

70+
- [BREAKING] [#57](https://github.com/shopware/SwagMigrationAssistant/pull/57) feat!: checksum and reset via mq
71+
- [BREAKING] Renamed method `cleanupMappingChecksums()` to `startCleanupMappingChecksums()` in `SwagMigrationAssistant\Migration\Run\RunServiceInterface` and implementation `SwagMigrationAssistant\Migration\Run\RunService`
72+
- [BREAKING] Renamed method `cleanupMigrationData()` to `startTruncateMigrationData()` in `SwagMigrationAssistant\Migration\Run\RunServiceInterface` and implementation `SwagMigrationAssistant\Migration\Run\RunService`
73+
7074
# 14.0.0
7175
- [BREAKING] MIG-1053 - Removed ability to set the `verify` flag for the guzzle API client. This is now always true by default.
7276
- [BREAKING] MIG-1053 - Refactored both Shopware 5 and Shopware 6 EnvironmentReader classes to provide more information about exceptions.

src/Controller/StatusController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,18 +354,18 @@ public function resetChecksums(Request $request, Context $context): Response
354354
)]
355355
public function cleanupMigrationData(Context $context): Response
356356
{
357-
$this->runService->startCleanupMigrationData($context);
357+
$this->runService->startTruncateMigrationData($context);
358358

359359
return new Response();
360360
}
361361

362362
#[Route(
363-
path: '/api/_action/migration/get-reset-status',
364-
name: 'api.admin.migration.get-reset-status',
363+
path: '/api/_action/migration/is-truncating-migration-data',
364+
name: 'api.admin.migration.is-truncating-migration-data',
365365
defaults: ['_acl' => ['admin']],
366366
methods: [Request::METHOD_GET]
367367
)]
368-
public function getResetStatus(Context $context): JsonResponse
368+
public function isTruncatingMigrationData(Context $context): JsonResponse
369369
{
370370
$settings = $this->generalSettingRepo->search(new Criteria(), $context)->getEntities()->first();
371371

src/Core/Migration/Migration1759000000AddIsResettingChecksumsToSetting.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,13 @@ public function getCreationTimestamp(): int
2525

2626
public function update(Connection $connection): void
2727
{
28-
$schemaManager = $connection->createSchemaManager();
29-
$columns = $schemaManager->listTableColumns(self::TABLE);
30-
31-
if (isset($columns[self::COLUMN])) {
32-
return;
33-
}
34-
35-
$connection->executeStatement(\sprintf(
36-
'ALTER TABLE %s ADD COLUMN %s TINYINT(1) NOT NULL DEFAULT 0',
37-
self::TABLE,
38-
self::COLUMN
39-
));
28+
$this->addColumn(
29+
connection: $connection,
30+
table: self::TABLE,
31+
column: self::COLUMN,
32+
type: 'TINYINT(1)',
33+
nullable: false,
34+
default: '0'
35+
);
4036
}
4137
}

src/DependencyInjection/migration.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@
311311
<tag name="messenger.message_handler"/>
312312
</service>
313313

314-
<service id="SwagMigrationAssistant\Migration\MessageQueue\Handler\CleanupMigrationHandler">
314+
<service id="SwagMigrationAssistant\Migration\MessageQueue\Handler\TruncateMigrationHandler">
315315
<argument type="service" id="Doctrine\DBAL\Connection"/>
316316
<argument type="service" id="messenger.default_bus"/>
317317
<tag name="messenger.message_handler"/>

src/Exception/MigrationException.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class MigrationException extends HttpException
3232

3333
public const MIGRATION_IS_ALREADY_RUNNING = 'SWAG_MIGRATION__MIGRATION_IS_ALREADY_RUNNING';
3434

35+
public const MIGRATION_IS_RESETTING_CHECKSUMS = 'SWAG_MIGRATION__MIGRATION_IS_RESETTING_CHECKSUMS';
36+
37+
public const MIGRATION_IS_TRUNCATING_DATA = 'SWAG_MIGRATION__MIGRATION_IS_TRUNCATING_DATA';
38+
3539
public const NO_CONNECTION_IS_SELECTED = 'SWAG_MIGRATION__NO_CONNECTION_IS_SELECTED';
3640

3741
public const NO_CONNECTION_FOUND = 'SWAG_MIGRATION__NO_CONNECTION_FOUND';
@@ -294,6 +298,24 @@ public static function migrationIsAlreadyRunning(): self
294298
);
295299
}
296300

301+
public static function checksumResetRunning(): self
302+
{
303+
return new MigrationIsAlreadyRunningException(
304+
Response::HTTP_BAD_REQUEST,
305+
self::MIGRATION_IS_RESETTING_CHECKSUMS,
306+
'Checksum reset is running.',
307+
);
308+
}
309+
310+
public static function truncatingDataRunning(): self
311+
{
312+
return new MigrationIsAlreadyRunningException(
313+
Response::HTTP_BAD_REQUEST,
314+
self::MIGRATION_IS_TRUNCATING_DATA,
315+
'Data truncation is running.',
316+
);
317+
}
318+
297319
public static function noConnectionIsSelected(): self
298320
{
299321
return new self(

src/Migration/MessageQueue/Handler/Processor/AbortingProcessor.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public function process(
5858
$this->bus->dispatch(new ResetChecksumMessage(
5959
$migrationContext->getConnection()->getId(),
6060
$context,
61-
true,
6261
$run->getId(),
6362
$progress->getCurrentEntity(),
6463
null,

src/Migration/MessageQueue/Handler/Processor/CleanUpProcessor.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace SwagMigrationAssistant\Migration\MessageQueue\Handler\Processor;
99

10-
use Doctrine\DBAL\ArrayParameterType;
1110
use Doctrine\DBAL\Connection;
1211
use Shopware\Core\Framework\Context;
1312
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
@@ -94,23 +93,10 @@ public function process(
9493

9594
private function removeMigrationData(): int
9695
{
97-
$ids = $this->connection->createQueryBuilder()
98-
->select('id')
99-
->from('swag_migration_data')
100-
->setMaxResults(self::BATCH_SIZE)
101-
->executeQuery()
102-
->fetchFirstColumn();
103-
104-
if (empty($ids)) {
105-
return 0;
106-
}
107-
108-
$query = $this->connection->createQueryBuilder()
96+
return (int) $this->connection->createQueryBuilder()
10997
->delete('swag_migration_data')
110-
->where('id IN (:ids)')
111-
->setParameter('ids', $ids, ArrayParameterType::BINARY);
112-
113-
return (int) $query->executeStatement();
98+
->setMaxResults(self::BATCH_SIZE)
99+
->executeStatement();
114100
}
115101

116102
private function getMigrationDataTotal(): int

src/Migration/MessageQueue/Handler/ResetChecksumHandler.php

Lines changed: 35 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
namespace SwagMigrationAssistant\Migration\MessageQueue\Handler;
99

10-
use Doctrine\DBAL\ArrayParameterType;
1110
use Doctrine\DBAL\Connection;
11+
use Doctrine\DBAL\ParameterType;
1212
use Shopware\Core\Framework\Context;
1313
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
1414
use Shopware\Core\Framework\Log\Package;
@@ -51,52 +51,27 @@ public function __invoke(ResetChecksumMessage $message): void
5151
$progress = null;
5252

5353
if ($totalMappings === null) {
54-
$totalMappings = $this->getTotalMappingsCount($connectionId, $message->isResettingAll());
54+
$totalMappings = $this->getTotalMappingsCount($connectionId);
5555

5656
if ($message->getRunId() !== null && $totalMappings > 0) {
57-
$progress = $this->initializeProgress(
57+
$progress = $this->updateProgress(
5858
$message,
59+
0,
5960
$totalMappings,
6061
$message->getContext()
6162
);
6263
}
6364
}
6465

65-
$query = $this->connection->createQueryBuilder()
66-
->select('m.id')
67-
->from('swag_migration_mapping', 'm')
68-
->where('m.checksum IS NOT NULL')
69-
->andWhere('m.connection_id = :connectionId')
70-
->setParameter('connectionId', Uuid::fromHexToBytes($connectionId))
71-
->setMaxResults(self::BATCH_SIZE);
72-
73-
if (!$message->isResettingAll()) {
74-
$query->innerJoin(
75-
'm',
76-
'swag_migration_data',
77-
'd',
78-
'd.mapping_uuid = m.id AND d.written = 0'
79-
);
80-
}
81-
82-
$ids = $query->executeQuery()->fetchFirstColumn();
83-
$batchSize = \count($ids);
66+
$affectedRows = $this->resetChecksums($connectionId);
8467

85-
if ($batchSize === 0) {
86-
$this->clearResettingChecksumsFlag();
68+
if ($affectedRows === 0) {
8769
$this->handleCompletion($message, $progress);
8870

8971
return;
9072
}
9173

92-
$this->connection->createQueryBuilder()
93-
->update('swag_migration_mapping')
94-
->set('checksum', 'NULL')
95-
->where('id IN (:ids)')
96-
->setParameter('ids', $ids, ArrayParameterType::BINARY)
97-
->executeStatement();
98-
99-
$newProcessedCount = $message->getProcessedMappings() + $batchSize;
74+
$newProcessedCount = $message->getProcessedMappings() + $affectedRows;
10075

10176
if ($message->getRunId() !== null) {
10277
$progress = $this->updateProgress(
@@ -107,8 +82,7 @@ public function __invoke(ResetChecksumMessage $message): void
10782
);
10883
}
10984

110-
if ($batchSize < self::BATCH_SIZE) {
111-
$this->clearResettingChecksumsFlag();
85+
if ($affectedRows < self::BATCH_SIZE) {
11286
$this->handleCompletion($message, $progress);
11387

11488
return;
@@ -117,7 +91,6 @@ public function __invoke(ResetChecksumMessage $message): void
11791
$this->messageBus->dispatch(new ResetChecksumMessage(
11892
$message->getConnectionId(),
11993
$message->getContext(),
120-
$message->isResettingAll(),
12194
$message->getRunId(),
12295
$message->getEntity(),
12396
$totalMappings,
@@ -128,6 +101,8 @@ public function __invoke(ResetChecksumMessage $message): void
128101

129102
private function handleCompletion(ResetChecksumMessage $message, ?MigrationProgress $progress): void
130103
{
104+
$this->clearResettingChecksumsFlag();
105+
131106
if (!$message->isPartOfAbort() || $message->getRunId() === null) {
132107
return;
133108
}
@@ -152,7 +127,7 @@ private function handleCompletion(ResetChecksumMessage $message, ?MigrationProgr
152127
$this->migrationRunRepo->upsert([
153128
[
154129
'id' => $runId,
155-
'progress' => $finalProgress,
130+
'progress' => $finalProgress->jsonSerialize(),
156131
],
157132
], $context);
158133

@@ -162,43 +137,35 @@ private function handleCompletion(ResetChecksumMessage $message, ?MigrationProgr
162137
));
163138
}
164139

165-
private function getTotalMappingsCount(string $connectionId, bool $resetAll): int
140+
private function resetChecksums(string $connectionId): int
166141
{
167-
$query = $this->connection->createQueryBuilder()
142+
return (int) $this->connection->executeStatement(
143+
'UPDATE swag_migration_mapping
144+
SET checksum = NULL
145+
WHERE checksum IS NOT NULL
146+
AND connection_id = :connectionId
147+
LIMIT :limit',
148+
[
149+
'connectionId' => Uuid::fromHexToBytes($connectionId),
150+
'limit' => self::BATCH_SIZE,
151+
],
152+
[
153+
'connectionId' => ParameterType::BINARY,
154+
'limit' => ParameterType::INTEGER,
155+
]
156+
);
157+
}
158+
159+
private function getTotalMappingsCount(string $connectionId): int
160+
{
161+
return (int) $this->connection->createQueryBuilder()
168162
->select('COUNT(m.id)')
169163
->from('swag_migration_mapping', 'm')
170164
->where('m.checksum IS NOT NULL')
171165
->andWhere('m.connection_id = :connectionId')
172-
->setParameter('connectionId', Uuid::fromHexToBytes($connectionId));
173-
174-
if (!$resetAll) {
175-
$query->innerJoin(
176-
'm',
177-
'swag_migration_data',
178-
'd',
179-
'd.mapping_uuid = m.id AND d.written = 0'
180-
);
181-
}
182-
183-
return (int) $query->executeQuery()->fetchOne();
184-
}
185-
186-
private function initializeProgress(ResetChecksumMessage $message, int $total, Context $context): MigrationProgress
187-
{
188-
$progress = new MigrationProgress(
189-
0,
190-
$total,
191-
new ProgressDataSetCollection(),
192-
$message->getEntity() ?? DefaultEntities::RULE,
193-
0
194-
);
195-
196-
$this->migrationRunRepo->update([[
197-
'id' => $message->getRunId(),
198-
'progress' => $progress->jsonSerialize(),
199-
]], $context);
200-
201-
return $progress;
166+
->setParameter('connectionId', Uuid::fromHexToBytes($connectionId))
167+
->executeQuery()
168+
->fetchOne();
202169
}
203170

204171
private function updateProgress(ResetChecksumMessage $message, int $processed, int $total, Context $context): MigrationProgress

src/Migration/MessageQueue/Handler/CleanupMigrationHandler.php renamed to src/Migration/MessageQueue/Handler/TruncateMigrationHandler.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use Doctrine\DBAL\Connection;
1111
use Shopware\Core\Framework\Log\Package;
12-
use SwagMigrationAssistant\Migration\MessageQueue\Message\CleanupMigrationMessage;
12+
use SwagMigrationAssistant\Migration\MessageQueue\Message\TruncateMigrationMessage;
1313
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
1414
use Symfony\Component\Messenger\MessageBusInterface;
1515

@@ -18,15 +18,17 @@
1818
/**
1919
* @internal
2020
*/
21-
final class CleanupMigrationHandler
21+
final class TruncateMigrationHandler
2222
{
23+
private const BATCH_SIZE = 250;
24+
2325
public function __construct(
2426
private readonly Connection $connection,
2527
private readonly MessageBusInterface $bus,
2628
) {
2729
}
2830

29-
public function __invoke(CleanupMigrationMessage $message): void
31+
public function __invoke(TruncateMigrationMessage $message): void
3032
{
3133
$currentStep = 0;
3234
$tablesToReset = [
@@ -48,15 +50,30 @@ public function __invoke(CleanupMigrationMessage $message): void
4850
$currentStep = $step;
4951
}
5052

53+
$affectedRows = (int) $this->connection->executeStatement(
54+
'DELETE FROM ' . $tablesToReset[$currentStep] . ' LIMIT ' . self::BATCH_SIZE
55+
);
56+
57+
if ($affectedRows >= self::BATCH_SIZE) {
58+
$this->bus->dispatch(new TruncateMigrationMessage(
59+
$tablesToReset[$currentStep]
60+
));
61+
62+
return;
63+
}
64+
5165
$nextStep = $currentStep + 1;
5266

5367
if (isset($tablesToReset[$nextStep])) {
54-
$nextMessage = new CleanupMigrationMessage($tablesToReset[$nextStep]);
55-
$this->bus->dispatch($nextMessage);
68+
$this->bus->dispatch(new TruncateMigrationMessage(
69+
$tablesToReset[$nextStep]
70+
));
71+
72+
return;
5673
}
5774

5875
$this->connection->executeStatement(
59-
'DELETE FROM ' . $tablesToReset[$currentStep] . ';'
76+
'UPDATE swag_migration_general_setting SET `is_reset` = 0;'
6077
);
6178
}
6279
}

0 commit comments

Comments
 (0)