Skip to content

Commit 2359432

Browse files
committed
Create distribution database entity
1 parent 201ec42 commit 2359432

4 files changed

Lines changed: 218 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 Version20260701083050 extends AbstractMigration
11+
{
12+
public function getDescription(): string
13+
{
14+
return 'Create distribution table';
15+
}
16+
17+
public function up(Schema $schema): void
18+
{
19+
$this->addSql(<<<'SQL'
20+
CREATE TABLE distribution (
21+
id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
22+
reference VARCHAR(255) NOT NULL,
23+
type VARCHAR(255) NOT NULL,
24+
source TEXT DEFAULT NULL,
25+
released_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
26+
resolved_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
27+
created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
28+
last_modified_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL,
29+
metadata_id INT NOT NULL,
30+
PRIMARY KEY (id)
31+
)
32+
SQL);
33+
$this->addSql(<<<'SQL'
34+
CREATE INDEX IDX_A4483781DC9EE959 ON distribution (metadata_id)
35+
SQL);
36+
$this->addSql(<<<'SQL'
37+
CREATE UNIQUE INDEX UNIQ_A4483781DC9EE959AEA349138CDE5729 ON distribution (metadata_id, reference, type)
38+
SQL);
39+
$this->addSql(<<<'SQL'
40+
ALTER TABLE
41+
distribution
42+
ADD
43+
CONSTRAINT FK_A4483781DC9EE959 FOREIGN KEY (metadata_id) REFERENCES metadata (id) NOT DEFERRABLE
44+
SQL);
45+
}
46+
47+
public function down(Schema $schema): void
48+
{
49+
$this->addSql(<<<'SQL'
50+
ALTER TABLE distribution DROP CONSTRAINT FK_A4483781DC9EE959
51+
SQL);
52+
$this->addSql(<<<'SQL'
53+
DROP TABLE distribution
54+
SQL);
55+
}
56+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodedMonkey\Dirigent\Doctrine\Entity;
6+
7+
use CodedMonkey\Dirigent\Doctrine\Repository\DistributionRepository;
8+
use Doctrine\DBAL\Types\Types;
9+
use Doctrine\ORM\Mapping as ORM;
10+
11+
#[ORM\Entity(repositoryClass: DistributionRepository::class)]
12+
#[ORM\ChangeTrackingPolicy('DEFERRED_EXPLICIT')]
13+
#[ORM\UniqueConstraint(columns: ['metadata_id', 'reference', 'type'])]
14+
class Distribution extends TrackedEntity
15+
{
16+
#[ORM\Column, ORM\GeneratedValue, ORM\Id]
17+
private ?int $id = null;
18+
19+
#[ORM\Column]
20+
private ?string $reference = null;
21+
22+
#[ORM\Column]
23+
private ?string $type = null;
24+
25+
/**
26+
* Source URL.
27+
*
28+
* Contains the source URL if the distribution is mirrored.
29+
*/
30+
#[ORM\Column(type: Types::TEXT, nullable: true)]
31+
private ?string $source = null;
32+
33+
#[ORM\Column]
34+
private \DateTimeImmutable $releasedAt;
35+
36+
#[ORM\Column]
37+
private \DateTimeImmutable $resolvedAt;
38+
39+
#[ORM\ManyToOne(inversedBy: 'distributions')]
40+
#[ORM\JoinColumn(nullable: false)]
41+
private Metadata $metadata;
42+
43+
public function __construct(Metadata $metadata)
44+
{
45+
$this->metadata = $metadata;
46+
}
47+
48+
public function getId(): ?int
49+
{
50+
return $this->id;
51+
}
52+
53+
public function getReference(): string
54+
{
55+
return $this->reference;
56+
}
57+
58+
public function setReference(string $reference): void
59+
{
60+
$this->reference = $reference;
61+
}
62+
63+
public function getType(): string
64+
{
65+
return $this->type;
66+
}
67+
68+
public function setType(string $type): void
69+
{
70+
$this->type = $type;
71+
}
72+
73+
public function getSource(): ?string
74+
{
75+
return $this->source;
76+
}
77+
78+
public function setSource(?string $source): void
79+
{
80+
$this->source = $source;
81+
}
82+
83+
public function getReleasedAt(): \DateTimeImmutable
84+
{
85+
return $this->releasedAt;
86+
}
87+
88+
public function setReleasedAt(\DateTimeImmutable $releasedAt): void
89+
{
90+
$this->releasedAt = $releasedAt;
91+
}
92+
93+
public function getResolvedAt(): \DateTimeImmutable
94+
{
95+
return $this->resolvedAt;
96+
}
97+
98+
public function setResolvedAt(): void
99+
{
100+
$this->resolvedAt = new \DateTimeImmutable();
101+
}
102+
103+
public function getMetadata(): Metadata
104+
{
105+
return $this->metadata;
106+
}
107+
}

src/Doctrine/Entity/Metadata.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ class Metadata extends TrackedEntity implements \Stringable
104104
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
105105
private MetadataFiles $files;
106106

107+
#[ORM\OneToMany(targetEntity: Distribution::class, mappedBy: 'metadata')]
108+
private Collection $distributions;
109+
107110
/**
108111
* @var Collection<int, MetadataRequireLink>&Selectable
109112
*/
@@ -152,6 +155,7 @@ public function __construct(Version $version)
152155
$this->package = $version->getPackage();
153156
$this->files = new MetadataFiles($this);
154157

158+
$this->distributions = new ArrayCollection();
155159
$this->requireLinks = new ArrayCollection();
156160
$this->devRequireLinks = new ArrayCollection();
157161
$this->conflictLinks = new ArrayCollection();
@@ -420,6 +424,14 @@ public function getPackage(): Package
420424
return $this->package;
421425
}
422426

427+
/**
428+
* @return Collection<int, Distribution>
429+
*/
430+
public function getDistributions(): Collection
431+
{
432+
return $this->distributions;
433+
}
434+
423435
/**
424436
* @return Collection<int, MetadataRequireLink>
425437
*/
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodedMonkey\Dirigent\Doctrine\Repository;
6+
7+
use CodedMonkey\Dirigent\Doctrine\Entity\Distribution;
8+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
9+
use Doctrine\Persistence\ManagerRegistry;
10+
11+
/**
12+
* @extends ServiceEntityRepository<Distribution>
13+
*
14+
* @method Distribution|null find($id, $lockMode = null, $lockVersion = null)
15+
* @method Distribution[] findAll()
16+
* @method Distribution[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
17+
* @method Distribution|null findOneBy(array $criteria, array $orderBy = null)
18+
*/
19+
class DistributionRepository extends ServiceEntityRepository
20+
{
21+
public function __construct(ManagerRegistry $registry)
22+
{
23+
parent::__construct($registry, Distribution::class);
24+
}
25+
26+
public function save(Distribution $entity, bool $flush = false): void
27+
{
28+
$this->getEntityManager()->persist($entity);
29+
30+
if ($flush) {
31+
$this->getEntityManager()->flush();
32+
}
33+
}
34+
35+
public function remove(Distribution $entity, bool $flush = false): void
36+
{
37+
$this->getEntityManager()->remove($entity);
38+
39+
if ($flush) {
40+
$this->getEntityManager()->flush();
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)