From 1625d36819d41728d496feabbe8bc209534ace49 Mon Sep 17 00:00:00 2001 From: William Date: Sat, 10 Jan 2026 09:55:15 +0200 Subject: [PATCH] Fix: Resolve DBAL contract violation with truncate() method (#387) Problem: - deleteTable() existed only in SleekDbal, not in contract - IdiormDbal lacked equivalent, breaking adapter interchangeability - Method name implied schema deletion vs actual data cleanup Solution: - Add truncate(): bool to DbalInterface contract - Implement truncate() in both SleekDbal and IdiormDbal - Replace all deleteTable() usage throughout codebase - Fix deprecated implicit nullable parameter in having() method Files changed: 11 - 1 contract (DbalInterface) - 2 adapters (SleekDbal, IdiormDbal) - 6 module templates - 2 test files All adapter tests pass (92 tests, 296 assertions). Fixes #387 --- .../Database/Adapters/Idiorm/IdiormDbal.php | 13 +++++++++++++ .../Database/Adapters/Sleekdb/SleekDbal.php | 16 ++++++++-------- .../Database/Contracts/DbalInterface.php | 8 +++++++- .../DemoApi/src/Services/AuthService.php.tpl | 2 +- .../DemoApi/src/Services/CommentService.php.tpl | 2 +- .../DemoApi/src/Services/PostService.php.tpl | 2 +- .../DemoWeb/src/Services/AuthService.php.tpl | 2 +- .../DemoWeb/src/Services/CommentService.php.tpl | 2 +- .../DemoWeb/src/Services/PostService.php.tpl | 2 +- .../Adapters/Sleekdb/SleekDbalTestCase.php | 2 +- tests/Unit/Model/ModelSoftDeletesSleek.php | 2 +- 11 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/Libraries/Database/Adapters/Idiorm/IdiormDbal.php b/src/Libraries/Database/Adapters/Idiorm/IdiormDbal.php index 1017fcbfd..f664fcde8 100644 --- a/src/Libraries/Database/Adapters/Idiorm/IdiormDbal.php +++ b/src/Libraries/Database/Adapters/Idiorm/IdiormDbal.php @@ -234,6 +234,19 @@ public function getModelName(): string return $this->modelName; } + /** + * @inheritDoc + */ + public function truncate(): bool + { + try { + $this->getOrmModel()->raw_execute("DELETE FROM {$this->table}"); + return true; + } catch (\Exception $e) { + return false; + } + } + /** * @param ORM $ormModel */ diff --git a/src/Libraries/Database/Adapters/Sleekdb/SleekDbal.php b/src/Libraries/Database/Adapters/Sleekdb/SleekDbal.php index dc92a9535..dc508b6ca 100644 --- a/src/Libraries/Database/Adapters/Sleekdb/SleekDbal.php +++ b/src/Libraries/Database/Adapters/Sleekdb/SleekDbal.php @@ -278,16 +278,16 @@ public function updateOrmModel(?array $modelData) } /** - * Deletes the table and the data - * @throws BaseException - * @throws DatabaseException - * @throws IOException - * @throws InvalidArgumentException - * @throws InvalidConfigurationException + * @inheritdoc */ - public function deleteTable() + public function truncate(): bool { - $this->getOrmModel()->deleteStore(); + try { + $this->getOrmModel()->deleteStore(); + return true; + } catch (\Exception $e) { + return false; + } } /** diff --git a/src/Libraries/Database/Contracts/DbalInterface.php b/src/Libraries/Database/Contracts/DbalInterface.php index ed8094192..88c143def 100644 --- a/src/Libraries/Database/Contracts/DbalInterface.php +++ b/src/Libraries/Database/Contracts/DbalInterface.php @@ -96,7 +96,7 @@ public function criterias(...$criterias): DbalInterface; * @param string|null $value * @return DbalInterface */ - public function having(string $column, string $operator, string $value = null): DbalInterface; + public function having(string $column, string $operator, ?string $value = null): DbalInterface; /** * Groups the result by given column @@ -171,6 +171,12 @@ public function save(): bool; */ public function delete(): bool; + /** + * Deletes all records from the table + * @return bool + */ + public function truncate(): bool; + /** * Deletes many records by previously applied criteria * @return bool diff --git a/src/Module/Templates/DemoApi/src/Services/AuthService.php.tpl b/src/Module/Templates/DemoApi/src/Services/AuthService.php.tpl index 2e9d53fd8..eb1881d36 100644 --- a/src/Module/Templates/DemoApi/src/Services/AuthService.php.tpl +++ b/src/Module/Templates/DemoApi/src/Services/AuthService.php.tpl @@ -134,7 +134,7 @@ class AuthService extends QtService implements AuthServiceInterface */ public function deleteAllUsers() { - $this->model->deleteTable(); + $this->model->truncate(); } /** diff --git a/src/Module/Templates/DemoApi/src/Services/CommentService.php.tpl b/src/Module/Templates/DemoApi/src/Services/CommentService.php.tpl index 0f8fade95..0d1f4ee11 100644 --- a/src/Module/Templates/DemoApi/src/Services/CommentService.php.tpl +++ b/src/Module/Templates/DemoApi/src/Services/CommentService.php.tpl @@ -118,7 +118,7 @@ class CommentService extends QtService */ public function deleteAllComments() { - $this->model->deleteTable(); + $this->model->truncate(); } /** diff --git a/src/Module/Templates/DemoApi/src/Services/PostService.php.tpl b/src/Module/Templates/DemoApi/src/Services/PostService.php.tpl index e7f431bbe..4ffb9f326 100644 --- a/src/Module/Templates/DemoApi/src/Services/PostService.php.tpl +++ b/src/Module/Templates/DemoApi/src/Services/PostService.php.tpl @@ -205,7 +205,7 @@ class PostService extends QtService */ public function deleteAllPost() { - $this->model->deleteTable(); + $this->model->truncate(); } /** diff --git a/src/Module/Templates/DemoWeb/src/Services/AuthService.php.tpl b/src/Module/Templates/DemoWeb/src/Services/AuthService.php.tpl index 2e9d53fd8..eb1881d36 100644 --- a/src/Module/Templates/DemoWeb/src/Services/AuthService.php.tpl +++ b/src/Module/Templates/DemoWeb/src/Services/AuthService.php.tpl @@ -134,7 +134,7 @@ class AuthService extends QtService implements AuthServiceInterface */ public function deleteAllUsers() { - $this->model->deleteTable(); + $this->model->truncate(); } /** diff --git a/src/Module/Templates/DemoWeb/src/Services/CommentService.php.tpl b/src/Module/Templates/DemoWeb/src/Services/CommentService.php.tpl index 219d47368..83e66250c 100644 --- a/src/Module/Templates/DemoWeb/src/Services/CommentService.php.tpl +++ b/src/Module/Templates/DemoWeb/src/Services/CommentService.php.tpl @@ -119,7 +119,7 @@ class CommentService extends QtService */ public function deleteAllComments() { - $this->model->deleteTable(); + $this->model->truncate(); } /** diff --git a/src/Module/Templates/DemoWeb/src/Services/PostService.php.tpl b/src/Module/Templates/DemoWeb/src/Services/PostService.php.tpl index f1c27ec5b..a91b70819 100644 --- a/src/Module/Templates/DemoWeb/src/Services/PostService.php.tpl +++ b/src/Module/Templates/DemoWeb/src/Services/PostService.php.tpl @@ -206,7 +206,7 @@ class PostService extends QtService */ public function deleteAllPosts() { - $this->model->deleteTable(); + $this->model->truncate(); } /** diff --git a/tests/Unit/Libraries/Database/Adapters/Sleekdb/SleekDbalTestCase.php b/tests/Unit/Libraries/Database/Adapters/Sleekdb/SleekDbalTestCase.php index 28f47a524..335cdb1ee 100644 --- a/tests/Unit/Libraries/Database/Adapters/Sleekdb/SleekDbalTestCase.php +++ b/tests/Unit/Libraries/Database/Adapters/Sleekdb/SleekDbalTestCase.php @@ -51,7 +51,7 @@ public function deleteTables(): void { foreach ($this->tables as $table) { $model = new SleekDbal($table); - $model->deleteTable(); + $model->truncate(); } } } diff --git a/tests/Unit/Model/ModelSoftDeletesSleek.php b/tests/Unit/Model/ModelSoftDeletesSleek.php index 5cf950298..bd0ec8ced 100644 --- a/tests/Unit/Model/ModelSoftDeletesSleek.php +++ b/tests/Unit/Model/ModelSoftDeletesSleek.php @@ -31,7 +31,7 @@ public function setUp(): void public function tearDown(): void { - ModelFactory::get(TestProductsModel::class)->deleteTable(); + ModelFactory::get(TestProductsModel::class)->truncate(); } public function testSleekDeleteSetsDeletedAt()