diff --git a/webapp/migrations/Version20240825115643.php b/webapp/migrations/Version20240825115643.php new file mode 100644 index 0000000000..15a68824f0 --- /dev/null +++ b/webapp/migrations/Version20240825115643.php @@ -0,0 +1,66 @@ +dropKeys(); + $this->addKeys(true); + } + + public function down(Schema $schema): void + { + $this->dropKeys(); + $this->addKeys(false); + } + + public function dropKeys(): void + { + $this->addSql('ALTER TABLE debug_package DROP CONSTRAINT FK_9E17399BE0E4FC3E'); + $this->addSql('ALTER TABLE version DROP CONSTRAINT FK_BF1CD3C32271845'); + $this->addSql('ALTER TABLE version DROP CONSTRAINT FK_BF1CD3C3E0E4FC3E'); + $this->addSql('ALTER TABLE judging_run DROP CONSTRAINT FK_29A6E6E13CBA64F2'); + $this->addSql('ALTER TABLE judging_run DROP CONSTRAINT judging_run_ibfk_1'); + $this->addSql('ALTER TABLE judgetask DROP CONSTRAINT judgetask_ibfk_1'); + $this->addSql('ALTER TABLE submission DROP FOREIGN KEY FK_DB055AF3F132696E'); + } + + public function addKeys(bool $isUp): void + { + // foreign-keys that are related to judgehosts are set to null so that no data is lost. + $cascadeClause = $isUp ? 'ON DELETE CASCADE' : ''; + $nullClause = $isUp ? 'ON DELETE SET NULL' : ''; + + $this->addSql('ALTER TABLE version ADD CONSTRAINT `FK_BF1CD3C32271845` FOREIGN KEY (`langid`) REFERENCES `language` (`langid`) ' . $cascadeClause); + $this->addSql('ALTER TABLE version ADD CONSTRAINT `FK_BF1CD3C3E0E4FC3E` FOREIGN KEY (`judgehostid`) REFERENCES `judgehost` (`judgehostid`) ' . $nullClause); + $this->addSql('ALTER TABLE judging_run ADD CONSTRAINT `FK_29A6E6E13CBA64F2` FOREIGN KEY (`judgetaskid`) REFERENCES `judgetask` (`judgetaskid`) ' . $cascadeClause); + $this->addSql('ALTER TABLE judging_run ADD CONSTRAINT `judging_run_ibfk_1` FOREIGN KEY (`testcaseid`) REFERENCES `testcase` (`testcaseid`) ' . $cascadeClause); + $this->addSql('ALTER TABLE judgetask ADD CONSTRAINT `judgetask_ibfk_1` FOREIGN KEY (`judgehostid`) REFERENCES `judgehost` (`judgehostid`) ' . $nullClause); + $this->addSql('ALTER TABLE debug_package ADD CONSTRAINT `FK_9E17399BE0E4FC3E` FOREIGN KEY (`judgehostid`) REFERENCES `judgehost` (`judgehostid`) ' . $nullClause); + + $clause = $isUp ? 'ON DELETE SET NULL' : 'ON DELETE CASCADE'; + $this->addSql('ALTER TABLE submission ADD CONSTRAINT FK_DB055AF3F132696E FOREIGN KEY (userid) REFERENCES user (userid) ' . $clause); + } + + public function isTransactional(): bool + { + return false; + } +} diff --git a/webapp/src/Entity/DebugPackage.php b/webapp/src/Entity/DebugPackage.php index e8b579730f..2340a012ce 100644 --- a/webapp/src/Entity/DebugPackage.php +++ b/webapp/src/Entity/DebugPackage.php @@ -28,7 +28,7 @@ class DebugPackage private string $filename; #[ORM\ManyToOne] - #[ORM\JoinColumn(name: 'judgehostid', referencedColumnName: 'judgehostid')] + #[ORM\JoinColumn(name: 'judgehostid', referencedColumnName: 'judgehostid', onDelete: 'SET NULL')] private Judgehost $judgehost; public function getDebugPackageId(): int diff --git a/webapp/src/Entity/JudgeTask.php b/webapp/src/Entity/JudgeTask.php index f5e0046986..513efebee1 100644 --- a/webapp/src/Entity/JudgeTask.php +++ b/webapp/src/Entity/JudgeTask.php @@ -38,7 +38,7 @@ class JudgeTask private int $judgetaskid; #[ORM\ManyToOne(inversedBy: 'judgetasks')] - #[ORM\JoinColumn(name: 'judgehostid', referencedColumnName: 'judgehostid')] + #[ORM\JoinColumn(name: 'judgehostid', referencedColumnName: 'judgehostid', onDelete: 'SET NULL')] #[Serializer\Exclude] private ?Judgehost $judgehost = null; diff --git a/webapp/src/Entity/JudgingRun.php b/webapp/src/Entity/JudgingRun.php index 2480a215f2..88fddf5436 100644 --- a/webapp/src/Entity/JudgingRun.php +++ b/webapp/src/Entity/JudgingRun.php @@ -66,7 +66,7 @@ class JudgingRun extends BaseApiEntity private Judging $judging; #[ORM\ManyToOne(inversedBy: 'judging_runs')] - #[ORM\JoinColumn(name: 'testcaseid', referencedColumnName: 'testcaseid')] + #[ORM\JoinColumn(name: 'testcaseid', referencedColumnName: 'testcaseid', onDelete: 'CASCADE')] #[Serializer\Exclude] private Testcase $testcase; @@ -82,7 +82,7 @@ class JudgingRun extends BaseApiEntity private Collection $output; #[ORM\ManyToOne(inversedBy: 'judging_runs')] - #[ORM\JoinColumn(name: 'judgetaskid', referencedColumnName: 'judgetaskid')] + #[ORM\JoinColumn(name: 'judgetaskid', referencedColumnName: 'judgetaskid', onDelete: 'CASCADE')] #[Serializer\Exclude] private ?JudgeTask $judgetask = null; diff --git a/webapp/src/Entity/Submission.php b/webapp/src/Entity/Submission.php index 841565177d..74f481c4d0 100644 --- a/webapp/src/Entity/Submission.php +++ b/webapp/src/Entity/Submission.php @@ -118,7 +118,7 @@ class Submission extends BaseApiEntity implements private Team $team; #[ORM\ManyToOne(inversedBy: 'submissions')] - #[ORM\JoinColumn(name: 'userid', referencedColumnName: 'userid', onDelete: 'CASCADE')] + #[ORM\JoinColumn(name: 'userid', referencedColumnName: 'userid', onDelete: 'SET NULL')] #[Serializer\Exclude] private ?User $user = null; diff --git a/webapp/src/Entity/Version.php b/webapp/src/Entity/Version.php index 5b1d2b1c55..f78a98212a 100644 --- a/webapp/src/Entity/Version.php +++ b/webapp/src/Entity/Version.php @@ -37,11 +37,11 @@ class Version private ?string $compilerVersionCommand = null; #[ORM\ManyToOne(targetEntity: Language::class, inversedBy: "versions")] - #[ORM\JoinColumn(name: 'langid', referencedColumnName: 'langid')] + #[ORM\JoinColumn(name: 'langid', referencedColumnName: 'langid', onDelete: 'CASCADE')] private Language $language; #[ORM\ManyToOne(targetEntity: Judgehost::class)] - #[ORM\JoinColumn(name: 'judgehostid', referencedColumnName: 'judgehostid')] + #[ORM\JoinColumn(name: 'judgehostid', referencedColumnName: 'judgehostid', onDelete: 'SET NULL')] private Judgehost $judgehost; #[ORM\Column(