Delete options and responses together with their poll #30

Merged
gnilebein merged 1 commits from fix/poll-delete-cascade into main 2026-11-08 22:31:14 +01:00
4 changed files with 71 additions and 1 deletions

No files matched your search

+3
View File
@@ -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.
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
use Migrations\BaseMigration;
/**
* Removes answer options and responses whose slide no longer exists.
*
* Deleting a poll used to rely on ON DELETE CASCADE, which SQLite does not enforce
* by default, so options and responses of deleted polls could stay behind.
* PollsTable now deletes them via the ORM; this cleans up what was left before.
* Safe to run repeatedly, a no-op on databases that enforce foreign keys.
*/
class RemoveOrphanedRows extends BaseMigration
{
/**
* @return void
*/
public function up(): void
{
$this->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
{
}
}
+3 -1
View File
@@ -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,
]);
}
@@ -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();