Skip to content

Commit 47cdcce

Browse files
committed
Refactor source and dist fields in Metadata entity to individual fields
1 parent e9f2330 commit 47cdcce

3 files changed

Lines changed: 192 additions & 47 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
final class Version20260707081705 extends AbstractMigration
11+
{
12+
public function getDescription(): string
13+
{
14+
return 'Replace the source and dist json fields of metadata with individual fields';
15+
}
16+
17+
public function up(Schema $schema): void
18+
{
19+
$this->addSql(<<<'SQL'
20+
ALTER TABLE metadata ADD source_type VARCHAR(255) DEFAULT NULL
21+
SQL);
22+
$this->addSql(<<<'SQL'
23+
ALTER TABLE metadata ADD source_url VARCHAR(255) DEFAULT NULL
24+
SQL);
25+
$this->addSql(<<<'SQL'
26+
ALTER TABLE metadata ADD source_reference VARCHAR(255) DEFAULT NULL
27+
SQL);
28+
$this->addSql(<<<'SQL'
29+
ALTER TABLE metadata ADD distribution_type VARCHAR(255) DEFAULT NULL
30+
SQL);
31+
$this->addSql(<<<'SQL'
32+
ALTER TABLE metadata ADD distribution_url VARCHAR(255) DEFAULT NULL
33+
SQL);
34+
$this->addSql(<<<'SQL'
35+
ALTER TABLE metadata ADD distribution_reference VARCHAR(255) DEFAULT NULL
36+
SQL);
37+
$this->addSql(<<<'SQL'
38+
ALTER TABLE metadata ADD distribution_sha1_checksum VARCHAR(255) DEFAULT NULL
39+
SQL);
40+
$this->addSql(<<<'SQL'
41+
UPDATE metadata SET
42+
source_type = source ->> 'type',
43+
source_url = source ->> 'url',
44+
source_reference = source ->> 'reference',
45+
distribution_type = dist ->> 'type',
46+
distribution_url = dist ->> 'url',
47+
distribution_reference = dist ->> 'reference',
48+
distribution_sha1_checksum = dist ->> 'shasum'
49+
SQL);
50+
$this->addSql(<<<'SQL'
51+
ALTER TABLE metadata DROP source
52+
SQL);
53+
$this->addSql(<<<'SQL'
54+
ALTER TABLE metadata DROP dist
55+
SQL);
56+
}
57+
58+
public function down(Schema $schema): void
59+
{
60+
$this->addSql(<<<'SQL'
61+
ALTER TABLE metadata ADD source JSON DEFAULT NULL
62+
SQL);
63+
$this->addSql(<<<'SQL'
64+
ALTER TABLE metadata ADD dist JSON DEFAULT NULL
65+
SQL);
66+
$this->addSql(<<<'SQL'
67+
UPDATE metadata SET source = json_build_object(
68+
'type', source_type,
69+
'url', source_url,
70+
'reference', source_reference
71+
)
72+
WHERE source_type IS NOT NULL
73+
SQL);
74+
$this->addSql(<<<'SQL'
75+
UPDATE metadata SET dist = json_build_object(
76+
'type', distribution_type,
77+
'url', distribution_url,
78+
'reference', distribution_reference,
79+
'shasum', distribution_sha1_checksum
80+
)
81+
WHERE distribution_type IS NOT NULL
82+
SQL);
83+
$this->addSql(<<<'SQL'
84+
ALTER TABLE metadata DROP source_type
85+
SQL);
86+
$this->addSql(<<<'SQL'
87+
ALTER TABLE metadata DROP source_url
88+
SQL);
89+
$this->addSql(<<<'SQL'
90+
ALTER TABLE metadata DROP source_reference
91+
SQL);
92+
$this->addSql(<<<'SQL'
93+
ALTER TABLE metadata DROP distribution_type
94+
SQL);
95+
$this->addSql(<<<'SQL'
96+
ALTER TABLE metadata DROP distribution_url
97+
SQL);
98+
$this->addSql(<<<'SQL'
99+
ALTER TABLE metadata DROP distribution_reference
100+
SQL);
101+
$this->addSql(<<<'SQL'
102+
ALTER TABLE metadata DROP distribution_sha1_checksum
103+
SQL);
104+
}
105+
}

src/Doctrine/Entity/Metadata.php

Lines changed: 80 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,25 @@ class Metadata extends TrackedEntity implements \Stringable
5454
private ?string $targetDir = null;
5555

5656
#[ORM\Column(nullable: true)]
57-
private ?array $source = null;
57+
private ?string $sourceType = null;
5858

5959
#[ORM\Column(nullable: true)]
60-
private ?array $dist = null;
60+
private ?string $sourceUrl = null;
61+
62+
#[ORM\Column(nullable: true)]
63+
private ?string $sourceReference = null;
64+
65+
#[ORM\Column(nullable: true)]
66+
private ?string $distributionType = null;
67+
68+
#[ORM\Column(nullable: true)]
69+
private ?string $distributionUrl = null;
70+
71+
#[ORM\Column(nullable: true)]
72+
private ?string $distributionReference = null;
73+
74+
#[ORM\Column(nullable: true)]
75+
private ?string $distributionSha1Checksum = null;
6176

6277
#[ORM\Column(nullable: true)]
6378
private ?array $autoload = null;
@@ -282,26 +297,6 @@ public function setTargetDir(?string $targetDir): void
282297
$this->targetDir = $targetDir;
283298
}
284299

285-
public function getSource(): ?array
286-
{
287-
return $this->source;
288-
}
289-
290-
public function setSource(?array $source): void
291-
{
292-
$this->source = $source;
293-
}
294-
295-
public function getDist(): ?array
296-
{
297-
return $this->dist;
298-
}
299-
300-
public function setDist(?array $dist): void
301-
{
302-
$this->dist = $dist;
303-
}
304-
305300
public function getAutoload(): ?array
306301
{
307302
return $this->autoload;
@@ -488,42 +483,82 @@ public function getReference(): ?string
488483

489484
public function hasSource(): bool
490485
{
491-
return null !== $this->source;
486+
return null !== $this->sourceType;
492487
}
493488

494-
public function getSourceReference(): ?string
489+
public function getSourceType(): ?string
495490
{
496-
return $this->source['reference'] ?? null;
491+
return $this->sourceType;
497492
}
498493

499-
public function getSourceType(): ?string
494+
public function setSourceType(?string $sourceType): void
500495
{
501-
return $this->source['type'] ?? null;
496+
$this->sourceType = $sourceType;
502497
}
503498

504499
public function getSourceUrl(): ?string
505500
{
506-
return $this->source['url'] ?? null;
501+
return $this->sourceUrl;
507502
}
508503

509-
public function hasDist(): bool
504+
public function setSourceUrl(?string $sourceUrl): void
510505
{
511-
return null !== $this->dist;
506+
$this->sourceUrl = $sourceUrl;
512507
}
513508

514-
public function getDistReference(): ?string
509+
public function getSourceReference(): ?string
510+
{
511+
return $this->sourceReference;
512+
}
513+
514+
public function setSourceReference(?string $sourceReference): void
515+
{
516+
$this->sourceReference = $sourceReference;
517+
}
518+
519+
public function hasDist(): bool
515520
{
516-
return $this->dist['reference'] ?? null;
521+
return null !== $this->distributionType;
517522
}
518523

519524
public function getDistType(): ?string
520525
{
521-
return $this->dist['type'] ?? null;
526+
return $this->distributionType;
527+
}
528+
529+
public function setDistType(?string $distType): void
530+
{
531+
$this->distributionType = $distType;
522532
}
523533

524534
public function getDistUrl(): ?string
525535
{
526-
return $this->dist['url'] ?? null;
536+
return $this->distributionUrl;
537+
}
538+
539+
public function setDistUrl(?string $distUrl): void
540+
{
541+
$this->distributionUrl = $distUrl;
542+
}
543+
544+
public function getDistReference(): ?string
545+
{
546+
return $this->distributionReference;
547+
}
548+
549+
public function setDistReference(?string $distReference): void
550+
{
551+
$this->distributionReference = $distReference;
552+
}
553+
554+
public function getDistributionSha1Checksum(): ?string
555+
{
556+
return $this->distributionSha1Checksum;
557+
}
558+
559+
public function setDistributionSha1Checksum(?string $distributionSha1Checksum): void
560+
{
561+
$this->distributionSha1Checksum = $distributionSha1Checksum;
527562
}
528563

529564
public function hasVersionAlias(): bool
@@ -637,8 +672,17 @@ public function toComposerArray(): array
637672
'version_normalized' => $this->normalizedVersionName,
638673
'license' => $this->license,
639674
'authors' => $this->getAuthorsSorted(),
640-
'source' => $this->source,
641-
'dist' => $this->dist,
675+
'source' => !$this->hasSource() ? null : [
676+
'type' => $this->sourceType,
677+
'url' => $this->sourceUrl,
678+
'reference' => $this->sourceReference,
679+
],
680+
'dist' => !$this->hasDist() ? null : [
681+
'type' => $this->distributionType,
682+
'url' => $this->distributionUrl,
683+
'reference' => $this->distributionReference,
684+
'shasum' => $this->distributionSha1Checksum,
685+
],
642686
'type' => $this->type,
643687
'autoload' => $this->autoload,
644688
];

src/Package/PackageMetadataResolver.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -303,20 +303,16 @@ private function createMetadata(Version $version, CompletePackageInterface $data
303303
}
304304

305305
if ($data->getSourceType()) {
306-
$metadata->setSource([
307-
'type' => $data->getSourceType(),
308-
'url' => static::optimizeRepositoryUrl($data->getSourceUrl()),
309-
'reference' => $data->getSourceReference(),
310-
]);
306+
$metadata->setSourceType($data->getSourceType());
307+
$metadata->setSourceUrl(static::optimizeRepositoryUrl($data->getSourceUrl()));
308+
$metadata->setSourceReference($data->getSourceReference());
311309
}
312310

313311
if ($data->getDistType()) {
314-
$metadata->setDist([
315-
'type' => $data->getDistType(),
316-
'url' => $data->getDistUrl(),
317-
'reference' => $data->getDistReference(),
318-
'shasum' => $data->getDistSha1Checksum(),
319-
]);
312+
$metadata->setDistType($data->getDistType());
313+
$metadata->setDistUrl($data->getDistUrl());
314+
$metadata->setDistReference($data->getDistReference());
315+
$metadata->setDistributionSha1Checksum($data->getDistSha1Checksum());
320316
}
321317

322318
// Handle links

0 commit comments

Comments
 (0)