Skip to content

Commit b3ac25f

Browse files
committed
fix: convert migration/logging logs usage
1 parent 295d9d5 commit b3ac25f

37 files changed

+452
-282
lines changed

src/Core/Migration/Migration1754897550AddRequiredFieldsToMigrationLogs.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
use Doctrine\DBAL\Platforms\MySQLPlatform;
1313
use Doctrine\DBAL\Schema\AbstractSchemaManager;
1414
use Shopware\Core\Framework\Log\Package;
15+
use Shopware\Core\Framework\Migration\MigrationStep;
1516

1617
#[Package('fundamentals@after-sales')]
17-
class Migration1754897550AddRequiredFieldsToMigrationLogs extends MigrationStepshopware
18+
class Migration1754897550AddRequiredFieldsToMigrationLogs extends MigrationStep
1819
{
1920
public const MIGRATION_LOGGING_TABLE = 'swag_migration_logging';
2021

@@ -118,15 +119,40 @@ private function ensureRelations(Connection $connection, AbstractSchemaManager $
118119
$indexes = $schemaManager->listTableIndexes(self::MIGRATION_LOGGING_TABLE);
119120

120121
if (isset($indexes['primary'])) {
121-
$connection->executeStatement(\sprintf('ALTER TABLE `%s` DROP PRIMARY KEY;', self::MIGRATION_LOGGING_TABLE));
122+
$connection->executeStatement(
123+
\sprintf(
124+
'ALTER TABLE `%s` DROP PRIMARY KEY;',
125+
self::MIGRATION_LOGGING_TABLE
126+
)
127+
);
122128
}
123129

124-
$connection->executeStatement(\sprintf('ALTER TABLE `%s` ADD PRIMARY KEY (`id`);', self::MIGRATION_LOGGING_TABLE));
125-
$this->dropIndexIfExists($connection, self::MIGRATION_LOGGING_TABLE, 'idx.run_id');
126-
$connection->executeStatement(\sprintf('ALTER TABLE `%s` ADD INDEX `idx.run_id` (`run_id`);', self::MIGRATION_LOGGING_TABLE));
130+
$connection->executeStatement(
131+
\sprintf(
132+
'ALTER TABLE `%s` ADD PRIMARY KEY (`id`);',
133+
self::MIGRATION_LOGGING_TABLE
134+
)
135+
);
136+
137+
$this->dropIndexIfExists(
138+
$connection,
139+
self::MIGRATION_LOGGING_TABLE,
140+
'idx.run_id'
141+
);
142+
$connection->executeStatement(
143+
\sprintf(
144+
'ALTER TABLE `%s` ADD INDEX `idx.run_id` (`run_id`);',
145+
self::MIGRATION_LOGGING_TABLE
146+
)
147+
);
127148

128149
// ensure foreign key constraint
129-
$connection->executeStatement(\sprintf('ALTER TABLE `%s` ADD CONSTRAINT `fk.swag_migration_logging.run_id` FOREIGN KEY (`run_id`) REFERENCES `swag_migration_run` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE;', self::MIGRATION_LOGGING_TABLE));
150+
$connection->executeStatement(
151+
\sprintf(
152+
'ALTER TABLE `%s` ADD CONSTRAINT `fk.swag_migration_logging.run_id` FOREIGN KEY (`run_id`) REFERENCES `swag_migration_run` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE;',
153+
self::MIGRATION_LOGGING_TABLE
154+
)
155+
);
130156
}
131157

132158
private function dropConstraintIfExists(Connection $connection, string $constraintName): void

src/Migration/Media/Processor/HttpDownloadServiceBase.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ abstract public function supports(MigrationContextInterface $migrationContext):
6262
*/
6363
public function process(MigrationContextInterface $migrationContext, Context $context, array $workload): array
6464
{
65+
$connection = $migrationContext->getConnection();
66+
6567
// Map workload with uuids as keys
6668
$mappedWorkload = [];
6769
foreach ($workload as $work) {
@@ -76,15 +78,16 @@ public function process(MigrationContextInterface $migrationContext, Context $co
7678
if ($client === null) {
7779
$this->loggingService->addLogEntry(new ExceptionRunLog(
7880
$migrationContext->getRunUuid(),
79-
$this->getMediaEntity(),
81+
$connection->getProfileName(),
82+
$connection->getGatewayName(),
8083
new \Exception('Http download client can not be constructed.')
8184
));
8285
$this->loggingService->saveLogging($context);
8386

8487
return $workload;
8588
}
8689
// Do download requests and store the promises
87-
$promises = $this->doMediaDownloadRequests($media, $mappedWorkload, $client);
90+
$promises = $this->doMediaDownloadRequests($migrationContext, $media, $mappedWorkload, $client);
8891

8992
// Wait for the requests to complete, even if some of them fail
9093
/** @var array<string, array{'state': string, 'value': ResponseInterface, 'reason': ?RequestException}> $results */
@@ -119,8 +122,8 @@ function (MediaProcessWorkloadStruct $work) use ($uuid) {
119122
$work->setState(MediaProcessWorkloadStruct::ERROR_STATE);
120123
$this->loggingService->addLogEntry(new CannotGetFileRunLog(
121124
$work->getRunId(),
122-
$this->getMediaEntity(),
123-
$work->getMediaId(),
125+
$connection->getProfileName(),
126+
$connection->getGatewayName(),
124127
$work->getAdditionalData()['uri'],
125128
$result['reason'] ?? null
126129
));
@@ -140,8 +143,8 @@ function (MediaProcessWorkloadStruct $work) use ($uuid) {
140143
$work->setState(MediaProcessWorkloadStruct::ERROR_STATE);
141144
$this->loggingService->addLogEntry(new TemporaryFileErrorLog(
142145
$work->getRunId(),
143-
$this->getMediaEntity(),
144-
$uuid
146+
$connection->getProfileName(),
147+
$connection->getGatewayName(),
145148
));
146149

147150
continue;
@@ -181,9 +184,9 @@ function (MediaProcessWorkloadStruct $work) use ($uuid) {
181184
$work->setState(MediaProcessWorkloadStruct::ERROR_STATE);
182185
$this->loggingService->addLogEntry(new ExceptionRunLog(
183186
$work->getRunId(),
184-
$this->getMediaEntity(),
187+
$connection->getProfileName(),
188+
$connection->getGatewayName(),
185189
$e,
186-
$uuid
187190
));
188191
} finally {
189192
// clear up temp data
@@ -246,7 +249,7 @@ final protected function getDataSetEntity(MigrationContextInterface $migrationCo
246249
*
247250
* @return array<string, PromiseInterface>
248251
*/
249-
private function doMediaDownloadRequests(array $media, array &$mappedWorkload, HttpClientInterface $client): array
252+
private function doMediaDownloadRequests(MigrationContextInterface $migrationContext, array $media, array &$mappedWorkload, HttpClientInterface $client): array
250253
{
251254
$promises = [];
252255
foreach ($media as $mediaFile) {
@@ -256,7 +259,7 @@ private function doMediaDownloadRequests(array $media, array &$mappedWorkload, H
256259
$additionalData['uri'] = $mediaFile['uri'];
257260
$mappedWorkload[$uuid]->setAdditionalData($additionalData);
258261

259-
$promise = $this->doNormalDownloadRequest($mappedWorkload[$uuid], $client);
262+
$promise = $this->doNormalDownloadRequest($migrationContext, $mappedWorkload[$uuid], $client);
260263

261264
if ($promise !== null) {
262265
$promises[$uuid] = $promise;
@@ -266,7 +269,7 @@ private function doMediaDownloadRequests(array $media, array &$mappedWorkload, H
266269
return $promises;
267270
}
268271

269-
private function doNormalDownloadRequest(MediaProcessWorkloadStruct $workload, HttpClientInterface $client): ?PromiseInterface
272+
private function doNormalDownloadRequest(MigrationContextInterface $migrationContext, MediaProcessWorkloadStruct $workload, HttpClientInterface $client): ?PromiseInterface
270273
{
271274
$additionalData = $workload->getAdditionalData();
272275

@@ -276,12 +279,14 @@ private function doNormalDownloadRequest(MediaProcessWorkloadStruct $workload, H
276279
$workload->setCurrentOffset((int) $additionalData['file_size']);
277280
$workload->setState(MediaProcessWorkloadStruct::FINISH_STATE);
278281
} catch (\Throwable $exception) {
282+
$connection = $migrationContext->getConnection();
283+
279284
// this should never happen because of Promises, but just in case something is wrong with request construction
280285
$this->loggingService->addLogEntry(new ExceptionRunLog(
281286
$workload->getRunId(),
282-
$this->getMediaEntity(),
287+
$connection->getProfileName(),
288+
$connection->getGatewayName(),
283289
$exception,
284-
$workload->getMediaId()
285290
));
286291

287292
$promise = null;
@@ -296,16 +301,20 @@ private function persistFileToMedia(string $filePath, string $uuid, string $name
296301
// determine correct info about the temporary file, except for the $fileExtension (which can be overridden)
297302
$fileSize = \filesize($filePath);
298303
$mimeType = \mime_content_type($filePath);
304+
299305
if ($fileSize === false || $fileSize === 0 || $mimeType === false) {
306+
$connection = $migrationContext->getConnection();
307+
300308
$this->loggingService->addLogEntry(new ExceptionRunLog(
301309
$migrationContext->getRunUuid(),
302-
$this->getMediaEntity(),
310+
$connection->getProfileName(),
311+
$connection->getGatewayName(),
303312
new \Exception('Downloaded file is empty or could not determine mime type.'),
304-
$uuid
305313
));
306314

307315
return;
308316
}
317+
309318
$fileHash = \hash_file('md5', $filePath);
310319
$mediaFile = new MediaFile(
311320
$filePath,
@@ -331,11 +340,13 @@ private function persistFileToMedia(string $filePath, string $uuid, string $name
331340
} elseif (\in_array($mediaException->getErrorCode(), [MediaException::MEDIA_ILLEGAL_FILE_NAME, MediaException::MEDIA_EMPTY_FILE_NAME], true)) {
332341
$this->fileSaver->persistFileToMedia($mediaFile, Uuid::randomHex(), $uuid, $context);
333342
} else {
343+
$connection = $migrationContext->getConnection();
344+
334345
$this->loggingService->addLogEntry(new ExceptionRunLog(
335346
$migrationContext->getRunUuid(),
336-
$this->getMediaEntity(),
347+
$connection->getProfileName(),
348+
$connection->getGatewayName(),
337349
$mediaException,
338-
$uuid
339350
));
340351
}
341352
}

src/Migration/MessageQueue/Handler/ProcessMediaHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,9 @@ public function __invoke(ProcessMediaMessage $message): void
8383
$processor = $this->mediaFileProcessorRegistry->getProcessor($migrationContext);
8484
$workload = $processor->process($migrationContext, $context, $workload);
8585
$this->processFailures($context, $migrationContext, $processor, $workload);
86-
} catch (NoConnectionFoundException $e) {
86+
} catch (NoConnectionFoundException) {
8787
$this->loggingService->addLogEntry(new ProcessorNotFoundLog(
8888
$message->getRunId(),
89-
$message->getEntityName(),
9089
$connection->getProfileName(),
9190
$connection->getGatewayName()
9291
));
@@ -95,7 +94,8 @@ public function __invoke(ProcessMediaMessage $message): void
9594
} catch (\Exception $e) {
9695
$this->loggingService->addLogEntry(new ExceptionRunLog(
9796
$message->getRunId(),
98-
$message->getEntityName(),
97+
$connection->getProfileName(),
98+
$connection->getGatewayName(),
9999
$e
100100
));
101101

src/Migration/MigrationContext.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MigrationContext extends Struct implements MigrationContextInterface
2121

2222
private ProfileInterface $profile;
2323

24-
private ?SwagMigrationConnectionEntity $connection;
24+
private SwagMigrationConnectionEntity $connection;
2525

2626
private string $runUuid;
2727

@@ -54,8 +54,10 @@ public function getProfile(): ProfileInterface
5454
return $this->profile;
5555
}
5656

57-
public function getConnection(): ?SwagMigrationConnectionEntity
57+
public function getConnection(): SwagMigrationConnectionEntity
5858
{
59+
\assert($this->connection instanceof SwagMigrationConnectionEntity);
60+
5961
return $this->connection;
6062
}
6163

src/Migration/MigrationContextInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface MigrationContextInterface
1818
{
1919
public function getProfile(): ProfileInterface;
2020

21-
public function getConnection(): ?SwagMigrationConnectionEntity;
21+
public function getConnection(): SwagMigrationConnectionEntity;
2222

2323
public function getRunUuid(): string;
2424

src/Migration/Run/RunService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,11 @@ public function assignThemeToSalesChannel(string $runUuid, Context $context): vo
254254
foreach ($salesChannels as $salesChannel) {
255255
try {
256256
$this->themeService->assignTheme($defaultTheme, $salesChannel, $context);
257-
} catch (\Throwable $exception) {
257+
} catch (\Throwable) {
258258
$this->loggingService->addLogEntry(new ThemeCompilingErrorRunLog(
259259
$runUuid,
260-
$defaultTheme
260+
$connection->getProfileName(),
261+
$connection->getGatewayName(),
261262
));
262263
}
263264
}

src/Migration/Run/SwagMigrationRunEntity.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class SwagMigrationRunEntity extends Entity
2525

2626
protected ?string $connectionId = null;
2727

28-
protected ?SwagMigrationConnectionEntity $connection = null;
28+
protected SwagMigrationConnectionEntity $connection;
2929

3030
protected ?array $totals = null;
3131

@@ -49,8 +49,10 @@ public function setConnectionId(string $connectionId): void
4949
$this->connectionId = $connectionId;
5050
}
5151

52-
public function getConnection(): ?SwagMigrationConnectionEntity
52+
public function getConnection(): SwagMigrationConnectionEntity
5353
{
54+
\assert($this->connection instanceof SwagMigrationConnectionEntity);
55+
5456
return $this->connection;
5557
}
5658

src/Migration/Service/MediaFileProcessorService.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function processMediaFiles(MigrationContextInterface $migrationContext, C
4545
try {
4646
$currentDataSet = $this->dataSetRegistry->getDataSet($migrationContext, $mediaFile['entity']);
4747
} catch (DataSetNotFoundException $e) {
48-
$this->logDataSetNotFoundException($migrationContext, $mediaFile);
48+
$this->logDataSetNotFoundException($migrationContext);
4949

5050
continue;
5151
}
@@ -59,7 +59,7 @@ public function processMediaFiles(MigrationContextInterface $migrationContext, C
5959
$currentCount = 0;
6060
$currentDataSet = $this->dataSetRegistry->getDataSet($migrationContext, $mediaFile['entity']);
6161
} catch (DataSetNotFoundException $e) {
62-
$this->logDataSetNotFoundException($migrationContext, $mediaFile);
62+
$this->logDataSetNotFoundException($migrationContext);
6363

6464
continue;
6565
}
@@ -131,22 +131,15 @@ private function addMessageToBus(string $runUuid, Context $context, DataSet $dat
131131
/**
132132
* @param array<string, mixed> $mediaFile
133133
*/
134-
private function logDataSetNotFoundException(
135-
MigrationContextInterface $migrationContext,
136-
array $mediaFile,
137-
): void {
134+
private function logDataSetNotFoundException(MigrationContextInterface $migrationContext): void
135+
{
138136
$connection = $migrationContext->getConnection();
139137

140-
if ($connection === null) {
141-
return;
142-
}
143-
144138
$this->loggingService->addLogEntry(
145139
new DataSetNotFoundLog(
146140
$migrationContext->getRunUuid(),
147-
$mediaFile['entity'],
148-
$mediaFile['id'],
149-
$connection->getProfileName()
141+
$connection->getProfileName(),
142+
$connection->getGatewayName(),
150143
)
151144
);
152145
}

src/Migration/Service/MigrationDataConverter.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,15 @@ public function convert(array $data, MigrationContextInterface $migrationContext
6767
$this->mediaFileService->writeMediaFile($context);
6868
}
6969
} catch (\Throwable $exception) {
70+
$connection = $migrationContext->getConnection();
71+
7072
$this->loggingService->addLogEntry(new ExceptionRunLog(
7173
$migrationContext->getRunUuid(),
72-
$dataSet::getEntity(),
74+
$connection->getProfileName(),
75+
$connection->getGatewayName(),
7376
$exception
7477
));
78+
7579
$this->loggingService->saveLogging($context);
7680
}
7781
}
@@ -101,11 +105,13 @@ private function convertData(
101105
'convertFailure' => $convertFailureFlag,
102106
];
103107
} catch (\Throwable $exception) {
108+
$connection = $migrationContext->getConnection();
109+
104110
$this->loggingService->addLogEntry(new ExceptionRunLog(
105111
$runUuid,
106-
$dataSet::getEntity(),
107-
$exception,
108-
$item['id'] ?? null
112+
$connection->getProfileName(),
113+
$connection->getGatewayName(),
114+
$exception
109115
));
110116

111117
$createData[] = [

src/Migration/Service/MigrationDataFetcher.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ public function fetchData(MigrationContextInterface $migrationContext, Context $
3434
try {
3535
return $this->gatewayRegistry->getGateway($migrationContext)->read($migrationContext);
3636
} catch (\Throwable $exception) {
37+
$connection = $migrationContext->getConnection();
38+
3739
$this->loggingService->addLogEntry(new ExceptionRunLog(
3840
$migrationContext->getRunUuid(),
39-
$dataSet::getEntity(),
41+
$connection->getProfileName(),
42+
$connection->getGatewayName(),
4043
$exception
4144
));
4245
$this->loggingService->saveLogging($context);

0 commit comments

Comments
 (0)