Skip to content

Commit dc2f5ed

Browse files
committed
recreates foreign-keys, changes delete rules.
Checked all remaining foreign keys using: ``` SELECT rc.DELETE_RULE, rc.UPDATE_RULE, kcu.TABLE_NAME, kcu.COLUMN_NAME, kcu.CONSTRAINT_NAME, kcu.REFERENCED_TABLE_NAME, kcu.REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS AS rc ON kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME WHERE kcu.REFERENCED_TABLE_SCHEMA = 'domjudge' AND DELETE_RULE = 'RESTRICT'; ``` After some discussion with Jaap it was decided to set columns referring to judgedaemons to null instead of cascading the delete. A single foreign key's delete rule is kept as 'restrict' since that case looks to be sensible. Fixes #2464, #2465.
1 parent e78b7cc commit dc2f5ed

File tree

5 files changed

+70
-6
lines changed

5 files changed

+70
-6
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20240825115643 extends AbstractMigration
14+
{
15+
// All remaining foreign keys are now cascade.
16+
// There is one exception: immutable_executable.immutable_execid is still referenced and removal is restricted.
17+
public function getDescription(): string
18+
{
19+
return 'Recreate \'restrict foreign key constraints\', cascading or setting to null where needed.';
20+
}
21+
22+
public function up(Schema $schema): void
23+
{
24+
$this->dropKeys();
25+
$this->addKeys(true);
26+
}
27+
28+
public function down(Schema $schema): void
29+
{
30+
$this->dropKeys();
31+
32+
// Reinstate the 'restrict' keys.
33+
$this->addKeys(false);
34+
}
35+
36+
public function dropKeys(): void
37+
{
38+
$this->addSql('ALTER TABLE debug_package DROP CONSTRAINT FK_9E17399BE0E4FC3E');
39+
$this->addSql('ALTER TABLE version DROP CONSTRAINT FK_BF1CD3C32271845');
40+
$this->addSql('ALTER TABLE version DROP CONSTRAINT FK_BF1CD3C3E0E4FC3E');
41+
$this->addSql('ALTER TABLE judging_run DROP CONSTRAINT FK_29A6E6E13CBA64F2');
42+
$this->addSql('ALTER TABLE judging_run DROP CONSTRAINT judging_run_ibfk_1');
43+
$this->addSql('ALTER TABLE judgetask DROP CONSTRAINT judgetask_ibfk_1');
44+
}
45+
46+
public function addKeys(bool $suffix): void
47+
{
48+
// 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' : '';
51+
52+
$this->addSql('ALTER TABLE version ADD CONSTRAINT `FK_BF1CD3C32271845` FOREIGN KEY (`langid`) REFERENCES `language` (`langid`) ' . $cascadeClause);
53+
$this->addSql('ALTER TABLE version ADD CONSTRAINT `FK_BF1CD3C3E0E4FC3E` FOREIGN KEY (`judgehostid`) REFERENCES `judgehost` (`judgehostid`) ' . $nullClause);
54+
$this->addSql('ALTER TABLE judging_run ADD CONSTRAINT `FK_29A6E6E13CBA64F2` FOREIGN KEY (`judgetaskid`) REFERENCES `judgetask` (`judgetaskid`) ' . $cascadeClause);
55+
$this->addSql('ALTER TABLE judging_run ADD CONSTRAINT `judging_run_ibfk_1` FOREIGN KEY (`testcaseid`) REFERENCES `testcase` (`testcaseid`) ' . $cascadeClause);
56+
$this->addSql('ALTER TABLE judgetask ADD CONSTRAINT `judgetask_ibfk_1` FOREIGN KEY (`judgehostid`) REFERENCES `judgehost` (`judgehostid`) ' . $nullClause);
57+
$this->addSql('ALTER TABLE debug_package ADD CONSTRAINT `FK_9E17399BE0E4FC3E` FOREIGN KEY (`judgehostid`) REFERENCES `judgehost` (`judgehostid`) ' . $nullClause);
58+
}
59+
60+
public function isTransactional(): bool
61+
{
62+
return false;
63+
}
64+
}

webapp/src/Entity/DebugPackage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class DebugPackage
2828
private string $filename;
2929

3030
#[ORM\ManyToOne]
31-
#[ORM\JoinColumn(name: 'judgehostid', referencedColumnName: 'judgehostid')]
31+
#[ORM\JoinColumn(name: 'judgehostid', referencedColumnName: 'judgehostid', onDelete: 'SET NULL')]
3232
private Judgehost $judgehost;
3333

3434
public function getDebugPackageId(): int

webapp/src/Entity/JudgeTask.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class JudgeTask
3838
private int $judgetaskid;
3939

4040
#[ORM\ManyToOne(inversedBy: 'judgetasks')]
41-
#[ORM\JoinColumn(name: 'judgehostid', referencedColumnName: 'judgehostid')]
41+
#[ORM\JoinColumn(name: 'judgehostid', referencedColumnName: 'judgehostid', onDelete: 'SET NULL')]
4242
#[Serializer\Exclude]
4343
private ?Judgehost $judgehost = null;
4444

webapp/src/Entity/JudgingRun.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class JudgingRun extends BaseApiEntity
6666
private Judging $judging;
6767

6868
#[ORM\ManyToOne(inversedBy: 'judging_runs')]
69-
#[ORM\JoinColumn(name: 'testcaseid', referencedColumnName: 'testcaseid')]
69+
#[ORM\JoinColumn(name: 'testcaseid', referencedColumnName: 'testcaseid', onDelete: 'CASCADE')]
7070
#[Serializer\Exclude]
7171
private Testcase $testcase;
7272

@@ -82,7 +82,7 @@ class JudgingRun extends BaseApiEntity
8282
private Collection $output;
8383

8484
#[ORM\ManyToOne(inversedBy: 'judging_runs')]
85-
#[ORM\JoinColumn(name: 'judgetaskid', referencedColumnName: 'judgetaskid')]
85+
#[ORM\JoinColumn(name: 'judgetaskid', referencedColumnName: 'judgetaskid', onDelete: 'CASCADE')]
8686
#[Serializer\Exclude]
8787
private ?JudgeTask $judgetask = null;
8888

webapp/src/Entity/Version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ class Version
3737
private ?string $compilerVersionCommand = null;
3838

3939
#[ORM\ManyToOne(targetEntity: Language::class, inversedBy: "versions")]
40-
#[ORM\JoinColumn(name: 'langid', referencedColumnName: 'langid')]
40+
#[ORM\JoinColumn(name: 'langid', referencedColumnName: 'langid', onDelete: 'CASCADE')]
4141
private Language $language;
4242

4343
#[ORM\ManyToOne(targetEntity: Judgehost::class)]
44-
#[ORM\JoinColumn(name: 'judgehostid', referencedColumnName: 'judgehostid')]
44+
#[ORM\JoinColumn(name: 'judgehostid', referencedColumnName: 'judgehostid', onDelete: 'SET NULL')]
4545
private Judgehost $judgehost;
4646

4747
#[ORM\Column(

0 commit comments

Comments
 (0)