Skip to content

Commit

Permalink
Merge pull request #3017 from chakflying/v2/fix/external-database-setup
Browse files Browse the repository at this point in the history
[2.0] Fix: Add external mariaDB setup
  • Loading branch information
louislam authored Jun 27, 2023
2 parents f2633a5 + f70b971 commit a2d147b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions server/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -188,6 +189,19 @@ class Database {
}
};
} else if (dbConfig.type === "mariadb") {
if (!/^\w+$/.test(dbConfig.dbName)) {
throw Error("Invalid database name. A database name can only consist of letters, numbers and underscores");
}

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: {
Expand Down
4 changes: 2 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit a2d147b

Please # to comment.