2021-07-21 14:02:35 -04:00
|
|
|
const fs = require("fs");
|
2021-07-30 00:24:46 -04:00
|
|
|
const { R } = require("redbean-node");
|
2021-08-06 07:12:49 -04:00
|
|
|
const { setSetting, setting } = require("./util-server");
|
2021-07-21 14:02:35 -04:00
|
|
|
|
|
|
|
class Database {
|
|
|
|
|
|
|
|
static templatePath = "./db/kuma.db"
|
2021-07-28 08:52:49 -04:00
|
|
|
static path = "./data/kuma.db";
|
2021-08-05 07:04:38 -04:00
|
|
|
static latestVersion = 6;
|
2021-07-21 14:02:35 -04:00
|
|
|
static noReject = true;
|
2021-08-17 03:59:23 -04:00
|
|
|
static sqliteInstance = null;
|
2021-07-21 14:02:35 -04:00
|
|
|
|
2021-08-09 01:34:44 -04:00
|
|
|
static async connect() {
|
2021-08-22 11:35:24 -04:00
|
|
|
const acquireConnectionTimeout = 120 * 1000;
|
|
|
|
|
2021-08-23 05:27:03 -04:00
|
|
|
R.useBetterSQLite3 = true;
|
|
|
|
R.betterSQLite3Options.timeout = acquireConnectionTimeout;
|
|
|
|
|
|
|
|
R.setup("sqlite", {
|
|
|
|
filename: Database.path,
|
2021-08-06 07:09:00 -04:00
|
|
|
useNullAsDefault: true,
|
2021-08-22 11:35:24 -04:00
|
|
|
acquireConnectionTimeout: acquireConnectionTimeout,
|
2021-08-23 05:27:03 -04:00
|
|
|
}, {
|
|
|
|
min: 1,
|
|
|
|
max: 1,
|
|
|
|
idleTimeoutMillis: 120 * 1000,
|
|
|
|
propagateCreateError: false,
|
|
|
|
acquireTimeoutMillis: acquireConnectionTimeout,
|
2021-08-17 03:59:23 -04:00
|
|
|
});
|
|
|
|
|
2021-08-16 14:09:40 -04:00
|
|
|
if (process.env.SQL_LOG === "1") {
|
|
|
|
R.debug(true);
|
|
|
|
}
|
|
|
|
|
2021-08-09 01:34:44 -04:00
|
|
|
// Auto map the model to a bean object
|
|
|
|
R.freeze(true)
|
|
|
|
await R.autoloadModels("./server/model");
|
2021-08-23 05:27:03 -04:00
|
|
|
|
|
|
|
// Change to WAL
|
|
|
|
await R.exec("PRAGMA journal_mode = WAL");
|
|
|
|
|
2021-08-06 07:09:00 -04:00
|
|
|
}
|
|
|
|
|
2021-07-21 14:02:35 -04: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) {
|
|
|
|
console.info("Database no need to patch");
|
2021-08-08 03:04:20 -04:00
|
|
|
} else if (version > this.latestVersion) {
|
|
|
|
console.info("Warning: Database version is newer than expected");
|
2021-07-21 14:02:35 -04:00
|
|
|
} else {
|
|
|
|
console.info("Database patch is needed")
|
|
|
|
|
|
|
|
console.info("Backup the db")
|
|
|
|
const backupPath = "./data/kuma.db.bak" + version;
|
|
|
|
fs.copyFileSync(Database.path, backupPath);
|
|
|
|
|
2021-08-19 05:49:19 -04:00
|
|
|
const shmPath = Database.path + "-shm";
|
|
|
|
if (fs.existsSync(shmPath)) {
|
|
|
|
fs.copyFileSync(shmPath, shmPath + ".bak" + version);
|
|
|
|
}
|
|
|
|
|
|
|
|
const walPath = Database.path + "-wal";
|
|
|
|
if (fs.existsSync(walPath)) {
|
|
|
|
fs.copyFileSync(walPath, walPath + ".bak" + version);
|
|
|
|
}
|
|
|
|
|
2021-07-21 14:02:35 -04: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);
|
|
|
|
}
|
|
|
|
console.log("Database Patched Successfully");
|
|
|
|
} catch (ex) {
|
|
|
|
await Database.close();
|
|
|
|
console.error("Patch db failed!!! Restoring the backup")
|
|
|
|
fs.copyFileSync(backupPath, Database.path);
|
|
|
|
console.error(ex)
|
|
|
|
|
|
|
|
console.error("Start Uptime-Kuma failed due to patch db failed")
|
|
|
|
console.error("Please submit the bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues")
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) => {
|
|
|
|
return ! line.startsWith("--")
|
|
|
|
});
|
|
|
|
|
|
|
|
// Split statements by semicolon
|
|
|
|
// Filter out empty line
|
|
|
|
text = lines.join("\n")
|
|
|
|
|
|
|
|
let statements = text.split(";")
|
|
|
|
.map((statement) => {
|
|
|
|
return statement.trim();
|
|
|
|
})
|
|
|
|
.filter((statement) => {
|
|
|
|
return statement !== "";
|
|
|
|
})
|
|
|
|
|
|
|
|
for (let statement of statements) {
|
|
|
|
await R.exec(statement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Special handle, because tarn.js throw a promise reject that cannot be caught
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async close() {
|
2021-08-17 03:59:23 -04:00
|
|
|
if (this.sqliteInstance) {
|
|
|
|
this.sqliteInstance.close();
|
2021-07-21 14:02:35 -04:00
|
|
|
}
|
2021-08-17 03:59:23 -04:00
|
|
|
console.log("Stopped database");
|
2021-07-21 14:02:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Database;
|