From 8d5679a8ab365886d09cc4b15768395a8fa43d17 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Mon, 3 Apr 2023 19:35:31 +0800 Subject: [PATCH 1/3] Fix: Create database before connect --- server/database.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/server/database.js b/server/database.js index 6c435b43c..f04035667 100644 --- a/server/database.js +++ b/server/database.js @@ -7,6 +7,7 @@ const knex = require("knex"); const { PluginsManager } = require("./plugins-manager"); const path = require("path"); const { EmbeddedMariaDB } = require("./embedded-mariadb"); +const mysql = require("mysql2/promise"); /** * Database & App Data Folder @@ -188,6 +189,19 @@ class Database { } }; } else if (dbConfig.type === "mariadb") { + if (!/^\w+$/.test(dbConfig.dbName)) { + throw Error("Invalid Database name"); + } + + const connection = await mysql.createConnection({ + host: dbConfig.hostname, + port: dbConfig.port, + user: dbConfig.username, + password: dbConfig.password, + }); + + await connection.execute("CREATE DATABASE IF NOT EXISTS " + dbConfig.dbName + " CHARACTER SET utf8mb4"); + config = { client: "mysql2", connection: { From 38fab198bb3240ea17b5965e07fa08424706a63a Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Mon, 3 Apr 2023 19:36:07 +0800 Subject: [PATCH 2/3] Fix: Fix user count check --- server/server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/server.js b/server/server.js index 8d9ea76d3..021965a09 100644 --- a/server/server.js +++ b/server/server.js @@ -614,7 +614,7 @@ let needSetup = false; throw new Error("Password is too weak. It should contain alphabetic and numeric characters. It must be at least 6 characters in length."); } - if ((await R.count("user")) !== 0) { + if ((await R.knex("user").count("id as count").first()).count !== 0) { throw new Error("Uptime Kuma has been initialized. If you want to run setup again, please delete the database."); } @@ -1683,7 +1683,7 @@ async function initDatabase(testMode = false) { } // If there is no record in user table, it is a new Uptime Kuma instance, need to setup - if ((await R.count("user")) === 0) { + if ((await R.knex("user").count("id as count").first()).count === 0) { log.info("server", "No user, need setup"); needSetup = true; } From f70b97181068932e16e581121011757450dc29a9 Mon Sep 17 00:00:00 2001 From: Nelson Chan <3271800+chakflying@users.noreply.github.com> Date: Sun, 21 May 2023 15:42:13 +0800 Subject: [PATCH 3/3] Fix: Improve error message Co-authored-by: Frank Elsinga --- server/database.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/database.js b/server/database.js index f04035667..dbf5a6d69 100644 --- a/server/database.js +++ b/server/database.js @@ -190,7 +190,7 @@ class Database { }; } else if (dbConfig.type === "mariadb") { if (!/^\w+$/.test(dbConfig.dbName)) { - throw Error("Invalid Database name"); + throw Error("Invalid database name. A database name can only consist of letters, numbers and underscores"); } const connection = await mysql.createConnection({