Skip to content

Commit d44adc2

Browse files
committed
prevents removing submissions on user delete
The old foreign key was set to cascade, deleting the entire submission when a user got deleted. This feels conceptually wrong since a submission is connected to a team and not to a user. The user of the team of the submission is more 'metadata' than anything else.
1 parent dc2f5ed commit d44adc2

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

webapp/migrations/Version20240825115643.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public function up(Schema $schema): void
2828
public function down(Schema $schema): void
2929
{
3030
$this->dropKeys();
31-
32-
// Reinstate the 'restrict' keys.
3331
$this->addKeys(false);
3432
}
3533

@@ -41,20 +39,24 @@ public function dropKeys(): void
4139
$this->addSql('ALTER TABLE judging_run DROP CONSTRAINT FK_29A6E6E13CBA64F2');
4240
$this->addSql('ALTER TABLE judging_run DROP CONSTRAINT judging_run_ibfk_1');
4341
$this->addSql('ALTER TABLE judgetask DROP CONSTRAINT judgetask_ibfk_1');
42+
$this->addSql('ALTER TABLE submission DROP FOREIGN KEY FK_DB055AF3F132696E');
4443
}
4544

46-
public function addKeys(bool $suffix): void
45+
public function addKeys(bool $isUp): void
4746
{
4847
// foreign-keys that are related to judgehosts are set to null so that no data is lost.
49-
$cascadeClause = $suffix ? 'ON DELETE CASCADE' : '';
50-
$nullClause = $suffix ? 'ON DELETE SET NULL' : '';
48+
$cascadeClause = $isUp ? 'ON DELETE CASCADE' : '';
49+
$nullClause = $isUp ? 'ON DELETE SET NULL' : '';
5150

5251
$this->addSql('ALTER TABLE version ADD CONSTRAINT `FK_BF1CD3C32271845` FOREIGN KEY (`langid`) REFERENCES `language` (`langid`) ' . $cascadeClause);
5352
$this->addSql('ALTER TABLE version ADD CONSTRAINT `FK_BF1CD3C3E0E4FC3E` FOREIGN KEY (`judgehostid`) REFERENCES `judgehost` (`judgehostid`) ' . $nullClause);
5453
$this->addSql('ALTER TABLE judging_run ADD CONSTRAINT `FK_29A6E6E13CBA64F2` FOREIGN KEY (`judgetaskid`) REFERENCES `judgetask` (`judgetaskid`) ' . $cascadeClause);
5554
$this->addSql('ALTER TABLE judging_run ADD CONSTRAINT `judging_run_ibfk_1` FOREIGN KEY (`testcaseid`) REFERENCES `testcase` (`testcaseid`) ' . $cascadeClause);
5655
$this->addSql('ALTER TABLE judgetask ADD CONSTRAINT `judgetask_ibfk_1` FOREIGN KEY (`judgehostid`) REFERENCES `judgehost` (`judgehostid`) ' . $nullClause);
5756
$this->addSql('ALTER TABLE debug_package ADD CONSTRAINT `FK_9E17399BE0E4FC3E` FOREIGN KEY (`judgehostid`) REFERENCES `judgehost` (`judgehostid`) ' . $nullClause);
57+
58+
$clause = $isUp ? 'ON DELETE SET NULL' : 'ON DELETE CASCADE';
59+
$this->addSql('ALTER TABLE submission ADD CONSTRAINT FK_DB055AF3F132696E FOREIGN KEY (userid) REFERENCES user (userid) ' . $clause);
5860
}
5961

6062
public function isTransactional(): bool

webapp/src/Entity/Submission.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class Submission extends BaseApiEntity implements
118118
private Team $team;
119119

120120
#[ORM\ManyToOne(inversedBy: 'submissions')]
121-
#[ORM\JoinColumn(name: 'userid', referencedColumnName: 'userid', onDelete: 'CASCADE')]
121+
#[ORM\JoinColumn(name: 'userid', referencedColumnName: 'userid', onDelete: 'SET NULL')]
122122
#[Serializer\Exclude]
123123
private ?User $user = null;
124124

0 commit comments

Comments
 (0)