Skip to content

Commit 1bafb29

Browse files
committed
Create distribution database entity
1 parent daf9c0d commit 1bafb29

4 files changed

Lines changed: 186 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 Version20260708083050 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+
type VARCHAR(255) NOT NULL,
23+
source TEXT DEFAULT NULL,
24+
resolved_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
25+
created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
26+
last_modified_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL,
27+
metadata_id INT NOT NULL,
28+
PRIMARY KEY (id)
29+
)
30+
SQL);
31+
$this->addSql(<<<'SQL'
32+
CREATE INDEX IDX_A4483781DC9EE959 ON distribution (metadata_id)
33+
SQL);
34+
$this->addSql(<<<'SQL'
35+
CREATE UNIQUE INDEX UNIQ_A4483781DC9EE959AEA349138CDE5729 ON distribution (metadata_id, type)
36+
SQL);
37+
$this->addSql(<<<'SQL'
38+
ALTER TABLE
39+
distribution
40+
ADD
41+
CONSTRAINT FK_A4483781DC9EE959 FOREIGN KEY (metadata_id) REFERENCES metadata (id) NOT DEFERRABLE
42+
SQL);
43+
}
44+
45+
public function down(Schema $schema): void
46+
{
47+
$this->addSql(<<<'SQL'
48+
ALTER TABLE distribution DROP CONSTRAINT FK_A4483781DC9EE959
49+
SQL);
50+
$this->addSql(<<<'SQL'
51+
DROP TABLE distribution
52+
SQL);
53+
}
54+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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', '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 $type;
21+
22+
/**
23+
* Source URL.
24+
*
25+
* Contains the source URL if the distribution is mirrored.
26+
*/
27+
#[ORM\Column(type: Types::TEXT, nullable: true)]
28+
private ?string $source = null;
29+
30+
#[ORM\Column]
31+
private \DateTimeImmutable $resolvedAt;
32+
33+
#[ORM\ManyToOne(inversedBy: 'distributions')]
34+
#[ORM\JoinColumn(nullable: false)]
35+
private Metadata $metadata;
36+
37+
public function __construct(Metadata $metadata, string $type)
38+
{
39+
$this->metadata = $metadata;
40+
$this->type = $type;
41+
}
42+
43+
public function getId(): ?int
44+
{
45+
return $this->id;
46+
}
47+
48+
public function getType(): string
49+
{
50+
return $this->type;
51+
}
52+
53+
public function getSource(): ?string
54+
{
55+
return $this->source;
56+
}
57+
58+
public function setSource(?string $source): void
59+
{
60+
$this->source = $source;
61+
}
62+
63+
public function getResolvedAt(): \DateTimeImmutable
64+
{
65+
return $this->resolvedAt;
66+
}
67+
68+
public function setResolvedAt(): void
69+
{
70+
$this->resolvedAt = new \DateTimeImmutable();
71+
}
72+
73+
public function getMetadata(): Metadata
74+
{
75+
return $this->metadata;
76+
}
77+
}

src/Doctrine/Entity/Metadata.php

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

122+
#[ORM\OneToMany(targetEntity: Distribution::class, mappedBy: 'metadata')]
123+
private Collection $distributions;
124+
122125
/**
123126
* @var Collection<int, MetadataRequireLink>&Selectable
124127
*/
@@ -167,6 +170,7 @@ public function __construct(Version $version)
167170
$this->package = $version->getPackage();
168171
$this->files = new MetadataFiles($this);
169172

173+
$this->distributions = new ArrayCollection();
170174
$this->requireLinks = new ArrayCollection();
171175
$this->devRequireLinks = new ArrayCollection();
172176
$this->conflictLinks = new ArrayCollection();
@@ -415,6 +419,14 @@ public function getPackage(): Package
415419
return $this->package;
416420
}
417421

422+
/**
423+
* @return Collection<int, Distribution>
424+
*/
425+
public function getDistributions(): Collection
426+
{
427+
return $this->distributions;
428+
}
429+
418430
/**
419431
* @return Collection<int, MetadataRequireLink>
420432
*/
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)