Skip to content

Commit 128c6a5

Browse files
authored
(Bugfix?): Attempt to fix UniqueConstraintViolationExceptions on import via WebHook (Case 205891) (#60)
When reassigning collections of objects in Doctrine entities, this can create problems with unique constraints if the new collection contains objects with data already present to the old one, but the object does not share the same reference in memory. In this case, Doctrine tries to add the new objects first when saving the collection, leading to duplicate entries in the database. We see issues that look exactly like this in our production log on a regular basis, and this assignment looks exactly like the one causing the problem. But I couldn't reproduce the issue, so I couldn't test if this fixes it. I'm still going to roll it out – if it doesn't fix the issue (or introduces new ones), we will see this in our logs.
2 parents 6c1e399 + 6da7a97 commit 128c6a5

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/AppBundle/Entity/Project.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,24 +127,36 @@ public function setDescription($description)
127127

128128
public function setUsedPackageVersions(ArrayCollection $importedPackageVersions)
129129
{
130-
foreach ($this->packageVersions as $packageVersion) {
131-
$importedPackageVersionIsAlreadyUsed = false;
130+
foreach ($this->packageVersions as $key => $packageVersion) {
131+
$packageVersionIsGoingToStay = false;
132132

133133
foreach ($importedPackageVersions as $importedPackageVersion) {
134134
if ($packageVersion->equals($importedPackageVersion)) {
135-
$importedPackageVersionIsAlreadyUsed = true;
135+
$packageVersionIsGoingToStay = true;
136+
break;
136137
}
137138
}
138139

139-
if (!$importedPackageVersionIsAlreadyUsed) {
140+
if (!$packageVersionIsGoingToStay) {
140141
$packageVersion->removeUsingProject($this);
142+
$this->packageVersions->remove($key);
141143
}
142144
}
143145

144-
$this->packageVersions = $importedPackageVersions;
146+
foreach ($importedPackageVersions as $importedPackageVersion) {
147+
foreach ($this->packageVersions as $packageVersion) {
148+
$importedPackageVersionIsAlreadyInUse = false;
145149

146-
foreach ($this->packageVersions as $usage) {
147-
$usage->addUsingProject($this);
150+
if ($packageVersion->equals($importedPackageVersion)) {
151+
$importedPackageVersionIsAlreadyInUse = true;
152+
break;
153+
}
154+
155+
if (!$importedPackageVersionIsAlreadyInUse) {
156+
$this->packageVersions->add($importedPackageVersion);
157+
$packageVersion->addUsingProject($this);
158+
}
159+
}
148160
}
149161
}
150162

0 commit comments

Comments
 (0)