diff --git a/lib/Database.php b/lib/Database.php index 776b46d..f3320ce 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -271,7 +271,7 @@ class Database { } $hash = (strlen($password) > 0) ? password_hash($password, \PASSWORD_DEFAULT) : ""; // NOTE: This roundabout construction (with 'select' rather than 'values') is required by MySQL, because MySQL is riddled with pitfalls and exceptions - $this->db->prepare("INSERT INTO arsse_users(id,password,num) select ?, ?, ((select max(num) from arsse_users) + 1)", "str", "str")->runArray([$user,$hash]); + $this->db->prepare("INSERT INTO arsse_users(id,password,num) select ?, ?, (coalesce((select max(num) from arsse_users), 0) + 1)", "str", "str")->runArray([$user,$hash]); return true; } diff --git a/tests/cases/Database/SeriesUser.php b/tests/cases/Database/SeriesUser.php index b56a64d..031e516 100644 --- a/tests/cases/Database/SeriesUser.php +++ b/tests/cases/Database/SeriesUser.php @@ -205,4 +205,11 @@ trait SeriesUser { $this->assertException("alreadyExists", "User", "ExceptionConflict"); Arsse::$db->userRename("john.doe@example.com", "jane.doe@example.com"); } + + public function testAddFirstUser(): void { + // first truncate the users table + static::$drv->exec("DELETE FROM arsse_users"); + // add a user; if the max of the num column is not properly coalesced, this will result in a constraint violation + $this->assertTrue(Arsse::$db->userAdd("john.doe@example.com", "")); + } }