2021-07-21 11:02:35 -07:00
|
|
|
const fs = require("fs");
|
2021-07-29 21:24:46 -07:00
|
|
|
const { R } = require("redbean-node");
|
2021-08-06 04:12:49 -07:00
|
|
|
const { setSetting, setting } = require("./util-server");
|
2021-09-10 02:23:45 -07:00
|
|
|
const { debug, sleep } = require("../src/util");
|
|
|
|
const dayjs = require("dayjs");
|
2021-09-20 10:15:20 -07:00
|
|
|
const knex = require("knex");
|
2021-07-21 11:02:35 -07:00
|
|
|
|
2021-09-21 06:22:35 -07:00
|
|
|
/**
|
|
|
|
* Database & App Data Folder
|
|
|
|
*/
|
2021-07-21 11:02:35 -07:00
|
|
|
class Database {
|
|
|
|
|
2021-09-10 02:23:45 -07:00
|
|
|
static templatePath = "./db/kuma.db";
|
2021-09-21 06:22:35 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Data Dir (Default: ./data)
|
|
|
|
*/
|
2021-09-02 06:08:00 -07:00
|
|
|
static dataDir;
|
2021-09-21 06:22:35 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* User Upload Dir (Default: ./data/upload)
|
|
|
|
*/
|
|
|
|
static uploadDir;
|
|
|
|
|
2021-09-02 06:08:00 -07:00
|
|
|
static path;
|
2021-09-10 02:23:45 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
static patched = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For Backup only
|
|
|
|
*/
|
|
|
|
static backupPath = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add patch filename in key
|
|
|
|
* Values:
|
|
|
|
* true: Add it regardless of order
|
|
|
|
* false: Do nothing
|
|
|
|
* { parents: []}: Need parents before add it
|
|
|
|
*/
|
|
|
|
static patchList = {
|
|
|
|
"patch-setting-value-type.sql": true,
|
|
|
|
"patch-improve-performance.sql": true,
|
2021-09-11 07:37:33 -07:00
|
|
|
"patch-2fa.sql": true,
|
2021-09-12 08:05:23 -07:00
|
|
|
"patch-add-retry-interval-monitor.sql": true,
|
2021-09-16 07:48:28 -07:00
|
|
|
"patch-incident-table.sql": true,
|
2021-09-19 04:04:51 -07:00
|
|
|
"patch-group-table.sql": true,
|
2021-09-30 09:09:43 -07:00
|
|
|
"patch-monitor-push_token.sql": true,
|
2021-10-02 07:48:27 -07:00
|
|
|
"patch-http-monitor-method-body-and-headers.sql": true,
|
2021-10-18 15:42:33 -07:00
|
|
|
"patch-2fa-invalidate-used-token.sql": true,
|
2021-10-27 00:33:15 -07:00
|
|
|
"patch-notification_sent_history.sql": true,
|
2021-11-04 02:12:06 -07:00
|
|
|
"patch-monitor-basic-auth.sql": true,
|
2021-11-03 18:46:43 -07:00
|
|
|
"patch-added-mqtt-monitor.sql": true,
|
2021-09-10 02:23:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-18 02:02:05 -07:00
|
|
|
* The final version should be 10 after merged tag feature
|
2021-09-10 02:23:45 -07:00
|
|
|
* @deprecated Use patchList for any new feature
|
|
|
|
*/
|
2021-08-26 03:55:19 -07:00
|
|
|
static latestVersion = 10;
|
2021-09-10 02:23:45 -07:00
|
|
|
|
2021-07-21 11:02:35 -07:00
|
|
|
static noReject = true;
|
|
|
|
|
2021-09-20 01:29:18 -07:00
|
|
|
static init(args) {
|
|
|
|
// Data Directory (must be end with "/")
|
|
|
|
Database.dataDir = process.env.DATA_DIR || args["data-dir"] || "./data/";
|
|
|
|
Database.path = Database.dataDir + "kuma.db";
|
|
|
|
if (! fs.existsSync(Database.dataDir)) {
|
|
|
|
fs.mkdirSync(Database.dataDir, { recursive: true });
|
|
|
|
}
|
2021-09-21 06:22:35 -07:00
|
|
|
|
|
|
|
Database.uploadDir = Database.dataDir + "upload/";
|
|
|
|
|
|
|
|
if (! fs.existsSync(Database.uploadDir)) {
|
|
|
|
fs.mkdirSync(Database.uploadDir, { recursive: true });
|
|
|
|
}
|
|
|
|
|
2021-09-20 01:29:18 -07:00
|
|
|
console.log(`Data Dir: ${Database.dataDir}`);
|
|
|
|
}
|
|
|
|
|
2021-11-04 08:19:31 -07:00
|
|
|
static async connect(testMode = false) {
|
2021-08-22 08:35:24 -07:00
|
|
|
const acquireConnectionTimeout = 120 * 1000;
|
2021-08-17 00:59:23 -07:00
|
|
|
|
2021-09-20 10:15:20 -07:00
|
|
|
const Dialect = require("knex/lib/dialects/sqlite3/index.js");
|
|
|
|
Dialect.prototype._driver = () => require("@louislam/sqlite3");
|
|
|
|
|
|
|
|
const knexInstance = knex({
|
|
|
|
client: Dialect,
|
|
|
|
connection: {
|
|
|
|
filename: Database.path,
|
|
|
|
acquireConnectionTimeout: acquireConnectionTimeout,
|
|
|
|
},
|
2021-08-06 04:09:00 -07:00
|
|
|
useNullAsDefault: true,
|
2021-09-20 10:15:20 -07:00
|
|
|
pool: {
|
|
|
|
min: 1,
|
|
|
|
max: 1,
|
|
|
|
idleTimeoutMillis: 120 * 1000,
|
|
|
|
propagateCreateError: false,
|
|
|
|
acquireTimeoutMillis: acquireConnectionTimeout,
|
|
|
|
}
|
2021-08-17 00:59:23 -07:00
|
|
|
});
|
|
|
|
|
2021-09-20 10:15:20 -07:00
|
|
|
R.setup(knexInstance);
|
|
|
|
|
2021-08-16 11:09:40 -07:00
|
|
|
if (process.env.SQL_LOG === "1") {
|
|
|
|
R.debug(true);
|
|
|
|
}
|
|
|
|
|
2021-08-08 22:34:44 -07:00
|
|
|
// Auto map the model to a bean object
|
2021-09-19 04:04:51 -07:00
|
|
|
R.freeze(true);
|
2021-08-08 22:34:44 -07:00
|
|
|
await R.autoloadModels("./server/model");
|
2021-08-23 02:27:03 -07:00
|
|
|
|
2021-09-23 08:21:08 -07:00
|
|
|
await R.exec("PRAGMA foreign_keys = ON");
|
2021-11-04 08:19:31 -07:00
|
|
|
if (testMode) {
|
|
|
|
// Change to MEMORY
|
|
|
|
await R.exec("PRAGMA journal_mode = MEMORY");
|
|
|
|
} else {
|
|
|
|
// Change to WAL
|
|
|
|
await R.exec("PRAGMA journal_mode = WAL");
|
|
|
|
}
|
2021-09-08 20:56:42 -07:00
|
|
|
await R.exec("PRAGMA cache_size = -12000");
|
2021-10-26 08:02:32 -07:00
|
|
|
await R.exec("PRAGMA auto_vacuum = FULL");
|
2021-09-08 20:56:42 -07:00
|
|
|
|
|
|
|
console.log("SQLite config:");
|
2021-08-24 10:11:19 -07:00
|
|
|
console.log(await R.getAll("PRAGMA journal_mode"));
|
2021-09-08 20:56:42 -07:00
|
|
|
console.log(await R.getAll("PRAGMA cache_size"));
|
2021-09-20 10:15:20 -07:00
|
|
|
console.log("SQLite Version: " + await R.getCell("SELECT sqlite_version()"));
|
2021-08-06 04:09:00 -07:00
|
|
|
}
|
|
|
|
|
2021-07-21 11:02:35 -07:00
|
|
|
static async patch() {
|
|
|
|
let version = parseInt(await setting("database_version"));
|
|
|
|
|
|
|
|
if (! version) {
|
|
|
|
version = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.info("Your database version: " + version);
|
|
|
|
console.info("Latest database version: " + this.latestVersion);
|
|
|
|
|
|
|
|
if (version === this.latestVersion) {
|
2021-10-15 18:12:09 -07:00
|
|
|
console.info("Database patch not needed");
|
2021-08-08 00:04:20 -07:00
|
|
|
} else if (version > this.latestVersion) {
|
|
|
|
console.info("Warning: Database version is newer than expected");
|
2021-07-21 11:02:35 -07:00
|
|
|
} else {
|
2021-09-19 04:04:51 -07:00
|
|
|
console.info("Database patch is needed");
|
2021-07-21 11:02:35 -07:00
|
|
|
|
2021-09-10 02:23:45 -07:00
|
|
|
this.backup(version);
|
2021-08-19 02:49:19 -07:00
|
|
|
|
2021-07-21 11:02:35 -07:00
|
|
|
// Try catch anything here, if gone wrong, restore the backup
|
|
|
|
try {
|
|
|
|
for (let i = version + 1; i <= this.latestVersion; i++) {
|
|
|
|
const sqlFile = `./db/patch${i}.sql`;
|
|
|
|
console.info(`Patching ${sqlFile}`);
|
|
|
|
await Database.importSQLFile(sqlFile);
|
|
|
|
console.info(`Patched ${sqlFile}`);
|
|
|
|
await setSetting("database_version", i);
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
await Database.close();
|
|
|
|
|
2021-09-19 04:04:51 -07:00
|
|
|
console.error(ex);
|
2021-10-15 18:12:09 -07:00
|
|
|
console.error("Start Uptime-Kuma failed due to issue patching the database");
|
|
|
|
console.error("Please submit a bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues");
|
2021-09-19 04:04:51 -07:00
|
|
|
|
|
|
|
this.restore();
|
2021-07-21 11:02:35 -07:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 02:23:45 -07:00
|
|
|
|
|
|
|
await this.patch2();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call it from patch() only
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async patch2() {
|
|
|
|
console.log("Database Patch 2.0 Process");
|
|
|
|
let databasePatchedFiles = await setting("databasePatchedFiles");
|
|
|
|
|
|
|
|
if (! databasePatchedFiles) {
|
|
|
|
databasePatchedFiles = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("Patched files:");
|
|
|
|
debug(databasePatchedFiles);
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (let sqlFilename in this.patchList) {
|
2021-09-19 04:04:51 -07:00
|
|
|
await this.patch2Recursion(sqlFilename, databasePatchedFiles);
|
2021-09-10 02:23:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.patched) {
|
|
|
|
console.log("Database Patched Successfully");
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (ex) {
|
|
|
|
await Database.close();
|
|
|
|
|
2021-09-19 04:04:51 -07:00
|
|
|
console.error(ex);
|
2021-10-15 18:12:09 -07:00
|
|
|
console.error("Start Uptime-Kuma failed due to issue patching the database");
|
2021-09-10 02:23:45 -07:00
|
|
|
console.error("Please submit the bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues");
|
2021-09-19 04:04:51 -07:00
|
|
|
|
|
|
|
this.restore();
|
|
|
|
|
2021-09-10 02:23:45 -07:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
await setSetting("databasePatchedFiles", databasePatchedFiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used it patch2() only
|
|
|
|
* @param sqlFilename
|
|
|
|
* @param databasePatchedFiles
|
|
|
|
*/
|
|
|
|
static async patch2Recursion(sqlFilename, databasePatchedFiles) {
|
|
|
|
let value = this.patchList[sqlFilename];
|
|
|
|
|
|
|
|
if (! value) {
|
|
|
|
console.log(sqlFilename + " skip");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if patched
|
|
|
|
if (! databasePatchedFiles[sqlFilename]) {
|
|
|
|
console.log(sqlFilename + " is not patched");
|
|
|
|
|
|
|
|
if (value.parents) {
|
|
|
|
console.log(sqlFilename + " need parents");
|
|
|
|
for (let parentSQLFilename of value.parents) {
|
|
|
|
await this.patch2Recursion(parentSQLFilename, databasePatchedFiles);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.backup(dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
|
|
console.log(sqlFilename + " is patching");
|
|
|
|
this.patched = true;
|
|
|
|
await this.importSQLFile("./db/" + sqlFilename);
|
|
|
|
databasePatchedFiles[sqlFilename] = true;
|
2021-10-15 18:12:09 -07:00
|
|
|
console.log(sqlFilename + " was patched successfully");
|
2021-09-10 02:23:45 -07:00
|
|
|
|
|
|
|
} else {
|
2021-09-19 04:04:51 -07:00
|
|
|
debug(sqlFilename + " is already patched, skip");
|
2021-09-10 02:23:45 -07:00
|
|
|
}
|
2021-07-21 11:02:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sadly, multi sql statements is not supported by many sqlite libraries, I have to implement it myself
|
|
|
|
* @param filename
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async importSQLFile(filename) {
|
|
|
|
|
|
|
|
await R.getCell("SELECT 1");
|
|
|
|
|
|
|
|
let text = fs.readFileSync(filename).toString();
|
|
|
|
|
|
|
|
// Remove all comments (--)
|
|
|
|
let lines = text.split("\n");
|
|
|
|
lines = lines.filter((line) => {
|
2021-09-19 04:04:51 -07:00
|
|
|
return ! line.startsWith("--");
|
2021-07-21 11:02:35 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// Split statements by semicolon
|
|
|
|
// Filter out empty line
|
2021-09-19 04:04:51 -07:00
|
|
|
text = lines.join("\n");
|
2021-07-21 11:02:35 -07:00
|
|
|
|
|
|
|
let statements = text.split(";")
|
|
|
|
.map((statement) => {
|
|
|
|
return statement.trim();
|
|
|
|
})
|
|
|
|
.filter((statement) => {
|
|
|
|
return statement !== "";
|
2021-09-19 04:04:51 -07:00
|
|
|
});
|
2021-07-21 11:02:35 -07:00
|
|
|
|
|
|
|
for (let statement of statements) {
|
2021-09-01 00:02:04 -07:00
|
|
|
await R.exec(statement);
|
2021-07-21 11:02:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 10:11:19 -07:00
|
|
|
static getBetterSQLite3Database() {
|
|
|
|
return R.knex.client.acquireConnection();
|
|
|
|
}
|
|
|
|
|
2021-07-21 11:02:35 -07:00
|
|
|
/**
|
|
|
|
* Special handle, because tarn.js throw a promise reject that cannot be caught
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async close() {
|
2021-09-10 02:23:45 -07:00
|
|
|
const listener = (reason, p) => {
|
|
|
|
Database.noReject = false;
|
|
|
|
};
|
|
|
|
process.addListener("unhandledRejection", listener);
|
|
|
|
|
2021-10-15 18:12:09 -07:00
|
|
|
console.log("Closing the database");
|
2021-09-10 02:23:45 -07:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
Database.noReject = true;
|
|
|
|
await R.close();
|
|
|
|
await sleep(2000);
|
|
|
|
|
|
|
|
if (Database.noReject) {
|
|
|
|
break;
|
|
|
|
} else {
|
2021-10-15 18:12:09 -07:00
|
|
|
console.log("Waiting to close the database");
|
2021-09-10 02:23:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log("SQLite closed");
|
|
|
|
|
|
|
|
process.removeListener("unhandledRejection", listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* One backup one time in this process.
|
|
|
|
* Reset this.backupPath if you want to backup again
|
|
|
|
* @param version
|
|
|
|
*/
|
|
|
|
static backup(version) {
|
|
|
|
if (! this.backupPath) {
|
2021-10-18 12:25:53 -07:00
|
|
|
console.info("Backing up the database");
|
2021-09-10 02:23:45 -07:00
|
|
|
this.backupPath = this.dataDir + "kuma.db.bak" + version;
|
|
|
|
fs.copyFileSync(Database.path, this.backupPath);
|
|
|
|
|
|
|
|
const shmPath = Database.path + "-shm";
|
|
|
|
if (fs.existsSync(shmPath)) {
|
|
|
|
this.backupShmPath = shmPath + ".bak" + version;
|
|
|
|
fs.copyFileSync(shmPath, this.backupShmPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
const walPath = Database.path + "-wal";
|
|
|
|
if (fs.existsSync(walPath)) {
|
|
|
|
this.backupWalPath = walPath + ".bak" + version;
|
|
|
|
fs.copyFileSync(walPath, this.backupWalPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static restore() {
|
|
|
|
if (this.backupPath) {
|
2021-10-15 18:12:09 -07:00
|
|
|
console.error("Patching the database failed!!! Restoring the backup");
|
2021-09-10 02:23:45 -07:00
|
|
|
|
|
|
|
const shmPath = Database.path + "-shm";
|
|
|
|
const walPath = Database.path + "-wal";
|
|
|
|
|
|
|
|
// Delete patch failed db
|
|
|
|
try {
|
|
|
|
if (fs.existsSync(Database.path)) {
|
|
|
|
fs.unlinkSync(Database.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.existsSync(shmPath)) {
|
|
|
|
fs.unlinkSync(shmPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.existsSync(walPath)) {
|
|
|
|
fs.unlinkSync(walPath);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-10-15 18:12:09 -07:00
|
|
|
console.log("Restore failed; you may need to restore the backup manually");
|
2021-09-10 02:23:45 -07:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore backup
|
|
|
|
fs.copyFileSync(this.backupPath, Database.path);
|
|
|
|
|
|
|
|
if (this.backupShmPath) {
|
|
|
|
fs.copyFileSync(this.backupShmPath, shmPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.backupWalPath) {
|
|
|
|
fs.copyFileSync(this.backupWalPath, walPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
console.log("Nothing to restore");
|
2021-07-21 11:02:35 -07:00
|
|
|
}
|
|
|
|
}
|
2021-10-26 08:02:32 -07:00
|
|
|
|
|
|
|
static getSize() {
|
|
|
|
debug("Database.getSize()");
|
|
|
|
let stats = fs.statSync(Database.path);
|
|
|
|
debug(stats);
|
|
|
|
return stats.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async shrink() {
|
|
|
|
await R.exec("VACUUM");
|
|
|
|
}
|
2021-07-21 11:02:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Database;
|