From 0a4c9fad091436fe3eb22afba91a226740d8e2f6 Mon Sep 17 00:00:00 2001 From: Patrick Niebeling Date: Sun, 8 Nov 2026 21:56:18 +0100 Subject: [PATCH] Delete options and responses together with their poll Deleting a poll removed its slides with a plain deleteAll and relied on ON DELETE CASCADE for slide options and responses. SQLite does not enforce foreign keys by default, so answers of deleted polls stayed in the database. Cascade through the ORM instead and remove the rows left behind earlier. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 3 ++ .../20261108120000_RemoveOrphanedRows.php | 33 +++++++++++++++++++ src/Model/Table/PollsTable.php | 4 ++- .../Controller/AdminControllerTest.php | 32 ++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 config/Migrations/20261108120000_RemoveOrphanedRows.php diff --git a/AGENTS.md b/AGENTS.md index 574c571..ddfbd10 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -237,6 +237,9 @@ Kein WebSocket: Presenter (`/admin/{token}/state`) und Teilnehmende (`/p/{code}/ - Einrückung 4 Spaces, LF (siehe `.editorconfig`). - Keine neuen Frontend-Abhängigkeiten / kein Build-Schritt ohne Rücksprache. - Schemaänderungen nur per neuer Migration, nie bestehende Migration ändern (sobald deployed). +- Beim Löschen nie auf `ON DELETE CASCADE` verlassen: SQLite setzt Fremdschlüssel ohne `PRAGMA foreign_keys = ON` + nicht durch (die Test-Suite schaltet sie aber ein, Fehler fallen dort also nicht auf). Abhängige Daten über + `dependent` + `cascadeCallbacks` im ORM löschen (z. B. Polls → Slides → SlideOptions/Responses). - Commits klein und thematisch; Commit-Messages auf Englisch im Imperativ. - **`main` ist geschützt:** nie direkt auf `main` committen oder pushen. Jede Änderung auf einem eigenen Branch (z. B. `feature/…`, `fix/…`, `docs/…`), pushen und per Pull Request in Gitea mergen. diff --git a/config/Migrations/20261108120000_RemoveOrphanedRows.php b/config/Migrations/20261108120000_RemoveOrphanedRows.php new file mode 100644 index 0000000..348358b --- /dev/null +++ b/config/Migrations/20261108120000_RemoveOrphanedRows.php @@ -0,0 +1,33 @@ +execute('DELETE FROM responses WHERE slide_id NOT IN (SELECT id FROM slides)'); + $this->execute('DELETE FROM slide_options WHERE slide_id NOT IN (SELECT id FROM slides)'); + } + + /** + * Deleted rows cannot be restored. + * + * @return void + */ + public function down(): void + { + } +} diff --git a/src/Model/Table/PollsTable.php b/src/Model/Table/PollsTable.php index f0fcc87..d867b3d 100644 --- a/src/Model/Table/PollsTable.php +++ b/src/Model/Table/PollsTable.php @@ -48,7 +48,9 @@ class PollsTable extends Table 'foreignKey' => 'poll_id', 'sort' => ['Slides.position' => 'ASC', 'Slides.id' => 'ASC'], 'dependent' => true, - 'cascadeCallbacks' => false, + // Delete slide by slide so their options and responses go too. Do not rely on + // ON DELETE CASCADE: SQLite does not enforce foreign keys by default. + 'cascadeCallbacks' => true, ]); } diff --git a/tests/TestCase/Controller/AdminControllerTest.php b/tests/TestCase/Controller/AdminControllerTest.php index 5bb126f..3d8d108 100644 --- a/tests/TestCase/Controller/AdminControllerTest.php +++ b/tests/TestCase/Controller/AdminControllerTest.php @@ -5,7 +5,9 @@ namespace App\Test\TestCase\Controller; use App\Model\Entity\Slide; use App\Model\Table\PollsTable; +use App\Service\VoteService; use App\Test\TestCase\PlenarioTestTrait; +use Cake\Database\Driver\Sqlite; use Cake\TestSuite\EmailTrait; use Cake\TestSuite\IntegrationTestTrait; use Cake\TestSuite\TestCase; @@ -284,6 +286,36 @@ class AdminControllerTest extends TestCase $this->assertFalse($this->pollsTable()->exists(['id' => $poll->id])); } + public function testDeletePollRemovesOptionsAndResponses(): void + { + [$poll, $token] = $this->createPoll(); + $slide = $this->createSlide($poll, Slide::TYPE_CHOICE); + $poll = $this->goLive($poll, $slide); + (new VoteService())->submit($poll, $slide, 'participant', ['option_id' => $slide->slide_options[0]->id]); + $options = $this->fetchTable('SlideOptions'); + $responses = $this->fetchTable('Responses'); + $this->assertTrue($responses->exists(['slide_id' => $slide->id])); + + // A normal SQLite connection does not enforce foreign keys (the test suite turns them on), + // so deleting must not rely on ON DELETE CASCADE. + $connection = $responses->getConnection(); + $sqlite = $connection->getDriver() instanceof Sqlite; + if ($sqlite) { + $connection->execute('PRAGMA foreign_keys = OFF'); + } + try { + $this->post("/admin/$token/delete", ['confirm_code' => $poll->join_code]); + } finally { + if ($sqlite) { + $connection->execute('PRAGMA foreign_keys = ON'); + } + } + + $this->assertFalse($this->slidesTable()->exists(['id' => $slide->id])); + $this->assertFalse($options->exists(['slide_id' => $slide->id]), 'Options must not outlive their poll.'); + $this->assertFalse($responses->exists(['slide_id' => $slide->id]), 'Responses must not outlive their poll.'); + } + public function testQrAndExport(): void { [, $token] = $this->createPoll(); -- 2.54.1