Skip to content

Commit 7f5a8f9

Browse files
committed
Flip package and version installations association direction
1 parent 6c0a8e3 commit 7f5a8f9

File tree

5 files changed

+176
-22
lines changed

5 files changed

+176
-22
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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 Version20260409090433 extends AbstractMigration
11+
{
12+
public function getDescription(): string
13+
{
14+
return 'Flip package and version installations association direction';
15+
}
16+
17+
public function up(Schema $schema): void
18+
{
19+
$this->addSql(<<<'SQL'
20+
ALTER TABLE package_installations DROP CONSTRAINT fk_69b10efbf44cabff
21+
SQL);
22+
$this->addSql(<<<'SQL'
23+
ALTER TABLE package_installations DROP CONSTRAINT package_downloads_pkey
24+
SQL);
25+
$this->addSql(<<<'SQL'
26+
ALTER TABLE package_installations ADD id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL
27+
SQL);
28+
$this->addSql(<<<'SQL'
29+
ALTER TABLE package_installations ADD PRIMARY KEY (id)
30+
SQL);
31+
32+
$this->addSql(<<<'SQL'
33+
ALTER TABLE package ADD installations_id INT DEFAULT NULL
34+
SQL);
35+
$this->addSql(<<<'SQL'
36+
UPDATE package SET installations_id = package_installations.id
37+
FROM package_installations
38+
WHERE package_installations.package_id = package.id
39+
SQL);
40+
$this->addSql(<<<'SQL'
41+
ALTER TABLE package ALTER installations_id SET NOT NULL
42+
SQL);
43+
$this->addSql(<<<'SQL'
44+
ALTER TABLE package ADD CONSTRAINT FK_DE6867952B6BB138 FOREIGN KEY (installations_id) REFERENCES package_installations (id)
45+
SQL);
46+
$this->addSql(<<<'SQL'
47+
CREATE UNIQUE INDEX UNIQ_DE6867952B6BB138 ON package (installations_id)
48+
SQL);
49+
50+
$this->addSql(<<<'SQL'
51+
ALTER TABLE package_installations DROP package_id
52+
SQL);
53+
54+
$this->addSql(<<<'SQL'
55+
ALTER TABLE version_installations DROP CONSTRAINT fk_67c4df0d4bbc2705
56+
SQL);
57+
$this->addSql(<<<'SQL'
58+
ALTER TABLE version_installations DROP CONSTRAINT version_downloads_pkey
59+
SQL);
60+
$this->addSql(<<<'SQL'
61+
ALTER TABLE version_installations ADD id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL
62+
SQL);
63+
$this->addSql(<<<'SQL'
64+
ALTER TABLE version_installations ADD PRIMARY KEY (id)
65+
SQL);
66+
67+
$this->addSql(<<<'SQL'
68+
ALTER TABLE version ADD installations_id INT DEFAULT NULL
69+
SQL);
70+
$this->addSql(<<<'SQL'
71+
UPDATE version SET installations_id = version_installations.id
72+
FROM version_installations
73+
WHERE version_installations.version_id = version.id
74+
SQL);
75+
$this->addSql(<<<'SQL'
76+
ALTER TABLE version ALTER installations_id SET NOT NULL
77+
SQL);
78+
$this->addSql(<<<'SQL'
79+
ALTER TABLE version ADD CONSTRAINT FK_BF1CD3C32B6BB138 FOREIGN KEY (installations_id) REFERENCES version_installations (id)
80+
SQL);
81+
$this->addSql(<<<'SQL'
82+
CREATE UNIQUE INDEX UNIQ_BF1CD3C32B6BB138 ON version (installations_id)
83+
SQL);
84+
85+
$this->addSql(<<<'SQL'
86+
ALTER TABLE version_installations DROP version_id
87+
SQL);
88+
}
89+
90+
public function down(Schema $schema): void
91+
{
92+
$this->addSql(<<<'SQL'
93+
ALTER TABLE package_installations ADD package_id INT DEFAULT NULL
94+
SQL);
95+
$this->addSql(<<<'SQL'
96+
UPDATE package_installations SET package_id = package.id
97+
FROM package
98+
WHERE package.installations_id = package_installations.id
99+
SQL);
100+
$this->addSql(<<<'SQL'
101+
ALTER TABLE package_installations ALTER package_id SET NOT NULL
102+
SQL);
103+
104+
$this->addSql(<<<'SQL'
105+
ALTER TABLE package DROP CONSTRAINT FK_DE6867952B6BB138
106+
SQL);
107+
$this->addSql(<<<'SQL'
108+
DROP INDEX UNIQ_DE6867952B6BB138
109+
SQL);
110+
$this->addSql(<<<'SQL'
111+
ALTER TABLE package DROP installations_id
112+
SQL);
113+
114+
$this->addSql(<<<'SQL'
115+
ALTER TABLE package_installations DROP CONSTRAINT package_installations_pkey
116+
SQL);
117+
$this->addSql(<<<'SQL'
118+
ALTER TABLE package_installations DROP id
119+
SQL);
120+
$this->addSql(<<<'SQL'
121+
ALTER TABLE package_installations ADD CONSTRAINT fk_69b10efbf44cabff FOREIGN KEY (package_id) REFERENCES package (id) NOT DEFERRABLE INITIALLY IMMEDIATE
122+
SQL);
123+
$this->addSql(<<<'SQL'
124+
ALTER TABLE package_installations ADD PRIMARY KEY (package_id)
125+
SQL);
126+
127+
$this->addSql(<<<'SQL'
128+
ALTER TABLE version_installations ADD version_id INT DEFAULT NULL
129+
SQL);
130+
$this->addSql(<<<'SQL'
131+
UPDATE version_installations SET version_id = version.id
132+
FROM version
133+
WHERE version.installations_id = version_installations.id
134+
SQL);
135+
$this->addSql(<<<'SQL'
136+
ALTER TABLE version_installations ALTER version_id SET NOT NULL
137+
SQL);
138+
139+
$this->addSql(<<<'SQL'
140+
ALTER TABLE version DROP CONSTRAINT FK_BF1CD3C32B6BB138
141+
SQL);
142+
$this->addSql(<<<'SQL'
143+
DROP INDEX UNIQ_BF1CD3C32B6BB138
144+
SQL);
145+
$this->addSql(<<<'SQL'
146+
ALTER TABLE version DROP installations_id
147+
SQL);
148+
149+
$this->addSql(<<<'SQL'
150+
ALTER TABLE version_installations DROP CONSTRAINT version_installations_pkey
151+
SQL);
152+
$this->addSql(<<<'SQL'
153+
ALTER TABLE version_installations DROP id
154+
SQL);
155+
$this->addSql(<<<'SQL'
156+
ALTER TABLE version_installations ADD CONSTRAINT fk_67c4df0d4bbc2705 FOREIGN KEY (version_id) REFERENCES version (id) NOT DEFERRABLE INITIALLY IMMEDIATE
157+
SQL);
158+
$this->addSql(<<<'SQL'
159+
ALTER TABLE version_installations ADD PRIMARY KEY (version_id)
160+
SQL);
161+
}
162+
}

src/Doctrine/Entity/Package.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Package extends TrackedEntity
6767
#[ORM\ManyToOne]
6868
private ?Registry $mirrorRegistry = null;
6969

70-
#[ORM\OneToOne(mappedBy: 'package', cascade: ['persist', 'detach', 'remove'])]
70+
#[ORM\OneToOne(cascade: ['persist', 'detach', 'remove'])]
7171
private PackageInstallations $installations;
7272

7373
/**
@@ -89,7 +89,7 @@ class Package extends TrackedEntity
8989

9090
public function __construct()
9191
{
92-
$this->installations = new PackageInstallations($this);
92+
$this->installations = new PackageInstallations();
9393
$this->versions = new ArrayCollection();
9494
}
9595

src/Doctrine/Entity/PackageInstallations.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@
88
class PackageInstallations extends AbstractInstallations
99
{
1010
#[ORM\Id]
11-
#[ORM\OneToOne(inversedBy: 'installations')]
12-
private Package $package;
11+
#[ORM\Column]
12+
#[ORM\GeneratedValue]
13+
private ?int $id = null;
1314

14-
public function __construct(Package $package)
15+
public function getId(): ?int
1516
{
16-
$this->package = $package;
17-
}
18-
19-
public function getPackage(): Package
20-
{
21-
return $this->package;
17+
return $this->id;
2218
}
2319
}

src/Doctrine/Entity/Version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ class Version extends TrackedEntity implements \Stringable
3636
#[ORM\OneToOne(fetch: 'EAGER')]
3737
private ?Metadata $currentMetadata = null;
3838

39-
#[ORM\OneToOne(mappedBy: 'version', cascade: ['persist', 'detach', 'remove'])]
39+
#[ORM\OneToOne(cascade: ['persist', 'detach', 'remove'])]
4040
private VersionInstallations $installations;
4141

4242
public function __construct(Package $package)
4343
{
4444
$this->package = $package;
4545

46-
$this->installations = new VersionInstallations($this);
46+
$this->installations = new VersionInstallations();
4747

4848
$package->getVersions()->add($this);
4949
}

src/Doctrine/Entity/VersionInstallations.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@
88
class VersionInstallations extends AbstractInstallations
99
{
1010
#[ORM\Id]
11-
#[ORM\OneToOne(inversedBy: 'installations')]
12-
private Version $version;
11+
#[ORM\Column]
12+
#[ORM\GeneratedValue]
13+
private ?int $id = null;
1314

14-
public function __construct(Version $version)
15+
public function getId(): ?int
1516
{
16-
$this->version = $version;
17-
}
18-
19-
public function getVersion(): Version
20-
{
21-
return $this->version;
17+
return $this->id;
2218
}
2319
}

0 commit comments

Comments
 (0)