From 3eab5aad5d3a443375e08ca8301aaf2ef9bc04a0 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Mon, 17 May 2021 15:46:46 -0400 Subject: [PATCH] Fix adding users to a blank database --- lib/Database.php | 2 +- tests/cases/Database/SeriesUser.php | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/Database.php b/lib/Database.php index 776b46d2..f3320ce2 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 b56a64d8..031e5161 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", "")); + } }