2021-07-21 19:02:35 +01:00
|
|
|
const fs = require("fs");
|
2021-07-30 05:24:46 +01:00
|
|
|
const { R } = require("redbean-node");
|
2021-08-06 12:12:49 +01:00
|
|
|
const { setSetting, setting } = require("./util-server");
|
2022-04-13 16:33:37 +01:00
|
|
|
const { log, sleep } = require("../src/util");
|
2021-09-10 10:23:45 +01:00
|
|
|
const dayjs = require("dayjs");
|
2021-09-20 18:15:20 +01:00
|
|
|
const knex = require("knex");
|
2023-01-27 10:25:57 +00:00
|
|
|
const { PluginsManager } = require("./plugins-manager");
|
2021-07-21 19:02:35 +01:00
|
|
|
|
2021-09-21 14:22:35 +01:00
|
|
|
/**
|
|
|
|
* Database & App Data Folder
|
|
|
|
*/
|
2021-07-21 19:02:35 +01:00
|
|
|
class Database {
|
|
|
|
|
2021-09-10 10:23:45 +01:00
|
|
|
static templatePath = "./db/kuma.db";
|
2021-09-21 14:22:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Data Dir (Default: ./data)
|
|
|
|
*/
|
2021-09-02 14:08:00 +01:00
|
|
|
static dataDir;
|
2021-09-21 14:22:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* User Upload Dir (Default: ./data/upload)
|
|
|
|
*/
|
|
|
|
static uploadDir;
|
|
|
|
|
2021-09-02 14:08:00 +01:00
|
|
|
static path;
|
2021-09-10 10:23:45 +01: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 15:37:33 +01:00
|
|
|
"patch-2fa.sql": true,
|
2021-09-12 16:05:23 +01:00
|
|
|
"patch-add-retry-interval-monitor.sql": true,
|
2021-09-16 15:48:28 +01:00
|
|
|
"patch-incident-table.sql": true,
|
2021-09-19 12:04:51 +01:00
|
|
|
"patch-group-table.sql": true,
|
2021-09-30 17:09:43 +01:00
|
|
|
"patch-monitor-push_token.sql": true,
|
2021-10-02 15:48:27 +01:00
|
|
|
"patch-http-monitor-method-body-and-headers.sql": true,
|
2021-10-18 23:42:33 +01:00
|
|
|
"patch-2fa-invalidate-used-token.sql": true,
|
2021-10-27 08:33:15 +01:00
|
|
|
"patch-notification_sent_history.sql": true,
|
2021-11-04 09:12:06 +00:00
|
|
|
"patch-monitor-basic-auth.sql": true,
|
2022-01-14 09:09:37 +00:00
|
|
|
"patch-add-docker-columns.sql": true,
|
2021-12-27 10:54:48 +00:00
|
|
|
"patch-status-page.sql": true,
|
2021-10-30 18:37:15 +01:00
|
|
|
"patch-proxy.sql": true,
|
2022-04-05 14:27:50 +01:00
|
|
|
"patch-monitor-expiry-notification.sql": true,
|
2022-04-17 07:53:13 +01:00
|
|
|
"patch-status-page-footer-css.sql": true,
|
2021-11-04 01:46:43 +00:00
|
|
|
"patch-added-mqtt-monitor.sql": true,
|
2022-06-11 17:23:12 +01:00
|
|
|
"patch-add-clickable-status-page-link.sql": true,
|
2022-05-12 18:48:03 +01:00
|
|
|
"patch-add-sqlserver-monitor.sql": true,
|
2022-05-13 18:58:23 +01:00
|
|
|
"patch-add-other-auth.sql": { parents: [ "patch-monitor-basic-auth.sql" ] },
|
2022-08-03 06:00:39 +01:00
|
|
|
"patch-grpc-monitor.sql": true,
|
2022-05-12 10:48:38 +01:00
|
|
|
"patch-add-radius-monitor.sql": true,
|
2022-01-24 08:18:12 +00:00
|
|
|
"patch-monitor-add-resend-interval.sql": true,
|
2022-07-14 08:32:51 +01:00
|
|
|
"patch-ping-packet-size.sql": true,
|
2022-10-11 14:48:43 +01:00
|
|
|
"patch-maintenance-table2.sql": true,
|
2023-01-08 08:22:36 +00:00
|
|
|
"patch-add-gamedig-monitor.sql": true,
|
2023-01-10 20:25:45 +00:00
|
|
|
"patch-add-google-analytics-status-page-tag.sql": true,
|
2023-02-25 11:14:44 +00:00
|
|
|
"patch-http-body-encoding.sql": true,
|
2021-11-11 23:06:32 +00:00
|
|
|
"patch-add-description-monitor.sql": true,
|
2023-02-28 08:58:36 +00:00
|
|
|
"patch-api-key-table.sql": true,
|
2022-04-26 00:26:57 +01:00
|
|
|
};
|
2021-09-10 10:23:45 +01:00
|
|
|
|
|
|
|
/**
|
2021-10-18 10:02:05 +01:00
|
|
|
* The final version should be 10 after merged tag feature
|
2021-09-10 10:23:45 +01:00
|
|
|
* @deprecated Use patchList for any new feature
|
|
|
|
*/
|
2021-08-26 11:55:19 +01:00
|
|
|
static latestVersion = 10;
|
2021-09-10 10:23:45 +01:00
|
|
|
|
2021-07-21 19:02:35 +01:00
|
|
|
static noReject = true;
|
|
|
|
|
2022-04-20 19:56:40 +01:00
|
|
|
/**
|
|
|
|
* Initialize the database
|
|
|
|
* @param {Object} args Arguments to initialize DB with
|
|
|
|
*/
|
2021-09-20 09:29:18 +01:00
|
|
|
static init(args) {
|
|
|
|
// Data Directory (must be end with "/")
|
|
|
|
Database.dataDir = process.env.DATA_DIR || args["data-dir"] || "./data/";
|
2023-01-27 10:25:57 +00:00
|
|
|
|
|
|
|
// Plugin feature is working only if the dataDir = "./data";
|
|
|
|
if (Database.dataDir !== "./data/") {
|
|
|
|
log.warn("PLUGIN", "Warning: In order to enable plugin feature, you need to use the default data directory: ./data/");
|
|
|
|
PluginsManager.disable = true;
|
|
|
|
}
|
|
|
|
|
2021-09-20 09:29:18 +01:00
|
|
|
Database.path = Database.dataDir + "kuma.db";
|
|
|
|
if (! fs.existsSync(Database.dataDir)) {
|
|
|
|
fs.mkdirSync(Database.dataDir, { recursive: true });
|
|
|
|
}
|
2021-09-21 14:22:35 +01:00
|
|
|
|
|
|
|
Database.uploadDir = Database.dataDir + "upload/";
|
|
|
|
|
|
|
|
if (! fs.existsSync(Database.uploadDir)) {
|
|
|
|
fs.mkdirSync(Database.uploadDir, { recursive: true });
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", `Data Dir: ${Database.dataDir}`);
|
2021-09-20 09:29:18 +01:00
|
|
|
}
|
|
|
|
|
2022-04-20 19:56:40 +01:00
|
|
|
/**
|
|
|
|
* Connect to the database
|
|
|
|
* @param {boolean} [testMode=false] Should the connection be
|
|
|
|
* started in test mode?
|
2022-04-21 17:15:39 +01:00
|
|
|
* @param {boolean} [autoloadModels=true] Should models be
|
2022-04-21 13:01:22 +01:00
|
|
|
* automatically loaded?
|
|
|
|
* @param {boolean} [noLog=false] Should logs not be output?
|
2022-04-20 19:56:40 +01:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2022-04-07 17:56:56 +01:00
|
|
|
static async connect(testMode = false, autoloadModels = true, noLog = false) {
|
2021-08-22 16:35:24 +01:00
|
|
|
const acquireConnectionTimeout = 120 * 1000;
|
2021-08-17 08:59:23 +01:00
|
|
|
|
2021-09-20 18:15:20 +01: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 12:09:00 +01:00
|
|
|
useNullAsDefault: true,
|
2021-09-20 18:15:20 +01:00
|
|
|
pool: {
|
|
|
|
min: 1,
|
|
|
|
max: 1,
|
|
|
|
idleTimeoutMillis: 120 * 1000,
|
|
|
|
propagateCreateError: false,
|
|
|
|
acquireTimeoutMillis: acquireConnectionTimeout,
|
|
|
|
}
|
2021-08-17 08:59:23 +01:00
|
|
|
});
|
|
|
|
|
2021-09-20 18:15:20 +01:00
|
|
|
R.setup(knexInstance);
|
|
|
|
|
2021-08-16 19:09:40 +01:00
|
|
|
if (process.env.SQL_LOG === "1") {
|
|
|
|
R.debug(true);
|
|
|
|
}
|
|
|
|
|
2021-08-09 06:34:44 +01:00
|
|
|
// Auto map the model to a bean object
|
2021-09-19 12:04:51 +01:00
|
|
|
R.freeze(true);
|
2022-04-07 17:56:56 +01:00
|
|
|
|
|
|
|
if (autoloadModels) {
|
|
|
|
await R.autoloadModels("./server/model");
|
|
|
|
}
|
2021-08-23 10:27:03 +01:00
|
|
|
|
2021-09-23 16:21:08 +01:00
|
|
|
await R.exec("PRAGMA foreign_keys = ON");
|
2021-11-04 15:19:31 +00: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-09 04:56:42 +01:00
|
|
|
await R.exec("PRAGMA cache_size = -12000");
|
2021-10-26 16:02:32 +01:00
|
|
|
await R.exec("PRAGMA auto_vacuum = FULL");
|
2021-09-09 04:56:42 +01:00
|
|
|
|
2022-04-06 13:48:13 +01:00
|
|
|
// This ensures that an operating system crash or power failure will not corrupt the database.
|
|
|
|
// FULL synchronous is very safe, but it is also slower.
|
|
|
|
// Read more: https://sqlite.org/pragma.html#pragma_synchronous
|
|
|
|
await R.exec("PRAGMA synchronous = FULL");
|
|
|
|
|
2022-04-07 17:56:56 +01:00
|
|
|
if (!noLog) {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", "SQLite config:");
|
|
|
|
log.info("db", await R.getAll("PRAGMA journal_mode"));
|
|
|
|
log.info("db", await R.getAll("PRAGMA cache_size"));
|
|
|
|
log.info("db", "SQLite Version: " + await R.getCell("SELECT sqlite_version()"));
|
2022-04-07 17:56:56 +01:00
|
|
|
}
|
2021-08-06 12:09:00 +01:00
|
|
|
}
|
|
|
|
|
2022-04-20 19:56:40 +01:00
|
|
|
/** Patch the database */
|
2021-07-21 19:02:35 +01:00
|
|
|
static async patch() {
|
|
|
|
let version = parseInt(await setting("database_version"));
|
|
|
|
|
|
|
|
if (! version) {
|
|
|
|
version = 0;
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", "Your database version: " + version);
|
|
|
|
log.info("db", "Latest database version: " + this.latestVersion);
|
2021-07-21 19:02:35 +01:00
|
|
|
|
|
|
|
if (version === this.latestVersion) {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", "Database patch not needed");
|
2021-08-08 08:04:20 +01:00
|
|
|
} else if (version > this.latestVersion) {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", "Warning: Database version is newer than expected");
|
2021-07-21 19:02:35 +01:00
|
|
|
} else {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", "Database patch is needed");
|
2021-07-21 19:02:35 +01:00
|
|
|
|
2022-06-28 15:11:59 +01:00
|
|
|
try {
|
|
|
|
this.backup(version);
|
|
|
|
} catch (e) {
|
|
|
|
log.error("db", e);
|
|
|
|
log.error("db", "Unable to create a backup before patching the database. Please make sure you have enough space and permission.");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2021-08-19 10:49:19 +01:00
|
|
|
|
2021-07-21 19:02:35 +01: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`;
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", `Patching ${sqlFile}`);
|
2021-07-21 19:02:35 +01:00
|
|
|
await Database.importSQLFile(sqlFile);
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", `Patched ${sqlFile}`);
|
2021-07-21 19:02:35 +01:00
|
|
|
await setSetting("database_version", i);
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
await Database.close();
|
|
|
|
|
2022-04-13 16:33:37 +01:00
|
|
|
log.error("db", ex);
|
|
|
|
log.error("db", "Start Uptime-Kuma failed due to issue patching the database");
|
|
|
|
log.error("db", "Please submit a bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues");
|
2021-09-19 12:04:51 +01:00
|
|
|
|
|
|
|
this.restore();
|
2021-07-21 19:02:35 +01:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 10:23:45 +01:00
|
|
|
|
|
|
|
await this.patch2();
|
2022-03-08 06:33:35 +00:00
|
|
|
await this.migrateNewStatusPage();
|
2021-09-10 10:23:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-20 19:56:40 +01:00
|
|
|
* Patch DB using new process
|
2021-09-10 10:23:45 +01:00
|
|
|
* Call it from patch() only
|
2022-04-20 19:56:40 +01:00
|
|
|
* @private
|
2021-09-10 10:23:45 +01:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async patch2() {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", "Database Patch 2.0 Process");
|
2021-09-10 10:23:45 +01:00
|
|
|
let databasePatchedFiles = await setting("databasePatchedFiles");
|
|
|
|
|
|
|
|
if (! databasePatchedFiles) {
|
|
|
|
databasePatchedFiles = {};
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:33:37 +01:00
|
|
|
log.debug("db", "Patched files:");
|
|
|
|
log.debug("db", databasePatchedFiles);
|
2021-09-10 10:23:45 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
for (let sqlFilename in this.patchList) {
|
2021-09-19 12:04:51 +01:00
|
|
|
await this.patch2Recursion(sqlFilename, databasePatchedFiles);
|
2021-09-10 10:23:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.patched) {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", "Database Patched Successfully");
|
2021-09-10 10:23:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} catch (ex) {
|
|
|
|
await Database.close();
|
|
|
|
|
2022-04-13 16:33:37 +01:00
|
|
|
log.error("db", ex);
|
|
|
|
log.error("db", "Start Uptime-Kuma failed due to issue patching the database");
|
|
|
|
log.error("db", "Please submit the bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues");
|
2021-09-19 12:04:51 +01:00
|
|
|
|
|
|
|
this.restore();
|
|
|
|
|
2021-09-10 10:23:45 +01:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
await setSetting("databasePatchedFiles", databasePatchedFiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-08 06:33:35 +00:00
|
|
|
* Migrate status page value in setting to "status_page" table
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async migrateNewStatusPage() {
|
2022-03-24 15:43:07 +00:00
|
|
|
|
|
|
|
// Fix 1.13.0 empty slug bug
|
|
|
|
await R.exec("UPDATE status_page SET slug = 'empty-slug-recover' WHERE TRIM(slug) = ''");
|
|
|
|
|
2022-03-08 06:33:35 +00:00
|
|
|
let title = await setting("title");
|
|
|
|
|
|
|
|
if (title) {
|
|
|
|
console.log("Migrating Status Page");
|
|
|
|
|
2022-03-15 04:00:29 +00:00
|
|
|
let statusPageCheck = await R.findOne("status_page", " slug = 'default' ");
|
2022-03-08 06:33:35 +00:00
|
|
|
|
|
|
|
if (statusPageCheck !== null) {
|
2022-03-15 04:00:29 +00:00
|
|
|
console.log("Migrating Status Page - Skip, default slug record is already existing");
|
2022-03-08 06:33:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let statusPage = R.dispense("status_page");
|
2022-03-18 06:14:22 +00:00
|
|
|
statusPage.slug = "default";
|
2022-03-08 06:33:35 +00:00
|
|
|
statusPage.title = title;
|
2022-03-16 06:14:47 +00:00
|
|
|
statusPage.description = await setting("description");
|
2022-03-08 06:33:35 +00:00
|
|
|
statusPage.icon = await setting("icon");
|
|
|
|
statusPage.theme = await setting("statusPageTheme");
|
2022-03-18 06:14:22 +00:00
|
|
|
statusPage.published = !!await setting("statusPagePublished");
|
|
|
|
statusPage.search_engine_index = !!await setting("searchEngineIndex");
|
|
|
|
statusPage.show_tags = !!await setting("statusPageTags");
|
2022-03-08 06:33:35 +00:00
|
|
|
statusPage.password = null;
|
2022-03-18 06:14:22 +00:00
|
|
|
|
|
|
|
if (!statusPage.title) {
|
|
|
|
statusPage.title = "My Status Page";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!statusPage.icon) {
|
|
|
|
statusPage.icon = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!statusPage.theme) {
|
|
|
|
statusPage.theme = "light";
|
|
|
|
}
|
|
|
|
|
2022-03-16 07:38:10 +00:00
|
|
|
let id = await R.store(statusPage);
|
|
|
|
|
|
|
|
await R.exec("UPDATE incident SET status_page_id = ? WHERE status_page_id IS NULL", [
|
|
|
|
id
|
|
|
|
]);
|
|
|
|
|
|
|
|
await R.exec("UPDATE [group] SET status_page_id = ? WHERE status_page_id IS NULL", [
|
|
|
|
id
|
|
|
|
]);
|
|
|
|
|
2022-03-08 06:33:35 +00:00
|
|
|
await R.exec("DELETE FROM setting WHERE type = 'statusPage'");
|
2022-03-16 07:38:10 +00:00
|
|
|
|
2022-03-18 06:14:22 +00:00
|
|
|
// Migrate Entry Page if it is status page
|
|
|
|
let entryPage = await setting("entryPage");
|
|
|
|
|
|
|
|
if (entryPage === "statusPage") {
|
|
|
|
await setSetting("entryPage", "statusPage-default", "general");
|
|
|
|
}
|
|
|
|
|
2022-03-08 06:33:35 +00:00
|
|
|
console.log("Migrating Status Page - Done");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-10 10:23:45 +01:00
|
|
|
/**
|
2022-04-20 19:56:40 +01:00
|
|
|
* Patch database using new patching process
|
2021-09-10 10:23:45 +01:00
|
|
|
* Used it patch2() only
|
2022-04-20 19:56:40 +01:00
|
|
|
* @private
|
2021-09-10 10:23:45 +01:00
|
|
|
* @param sqlFilename
|
|
|
|
* @param databasePatchedFiles
|
2022-04-20 19:56:40 +01:00
|
|
|
* @returns {Promise<void>}
|
2021-09-10 10:23:45 +01:00
|
|
|
*/
|
|
|
|
static async patch2Recursion(sqlFilename, databasePatchedFiles) {
|
|
|
|
let value = this.patchList[sqlFilename];
|
|
|
|
|
|
|
|
if (! value) {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", sqlFilename + " skip");
|
2021-09-10 10:23:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if patched
|
|
|
|
if (! databasePatchedFiles[sqlFilename]) {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", sqlFilename + " is not patched");
|
2021-09-10 10:23:45 +01:00
|
|
|
|
|
|
|
if (value.parents) {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", sqlFilename + " need parents");
|
2021-09-10 10:23:45 +01:00
|
|
|
for (let parentSQLFilename of value.parents) {
|
|
|
|
await this.patch2Recursion(parentSQLFilename, databasePatchedFiles);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.backup(dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", sqlFilename + " is patching");
|
2021-09-10 10:23:45 +01:00
|
|
|
this.patched = true;
|
|
|
|
await this.importSQLFile("./db/" + sqlFilename);
|
|
|
|
databasePatchedFiles[sqlFilename] = true;
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", sqlFilename + " was patched successfully");
|
2021-09-10 10:23:45 +01:00
|
|
|
|
|
|
|
} else {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.debug("db", sqlFilename + " is already patched, skip");
|
2021-09-10 10:23:45 +01:00
|
|
|
}
|
2021-07-21 19:02:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-20 19:56:40 +01:00
|
|
|
* Load an SQL file and execute it
|
|
|
|
* @param filename Filename of SQL file to import
|
2021-07-21 19:02:35 +01:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async importSQLFile(filename) {
|
2022-04-20 19:56:40 +01:00
|
|
|
// Sadly, multi sql statements is not supported by many sqlite libraries, I have to implement it myself
|
2021-07-21 19:02:35 +01:00
|
|
|
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 12:04:51 +01:00
|
|
|
return ! line.startsWith("--");
|
2021-07-21 19:02:35 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Split statements by semicolon
|
|
|
|
// Filter out empty line
|
2021-09-19 12:04:51 +01:00
|
|
|
text = lines.join("\n");
|
2021-07-21 19:02:35 +01:00
|
|
|
|
|
|
|
let statements = text.split(";")
|
|
|
|
.map((statement) => {
|
|
|
|
return statement.trim();
|
|
|
|
})
|
|
|
|
.filter((statement) => {
|
|
|
|
return statement !== "";
|
2021-09-19 12:04:51 +01:00
|
|
|
});
|
2021-07-21 19:02:35 +01:00
|
|
|
|
|
|
|
for (let statement of statements) {
|
2021-09-01 08:02:04 +01:00
|
|
|
await R.exec(statement);
|
2021-07-21 19:02:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 19:56:40 +01:00
|
|
|
/**
|
|
|
|
* Aquire a direct connection to database
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
2021-08-24 18:11:19 +01:00
|
|
|
static getBetterSQLite3Database() {
|
|
|
|
return R.knex.client.acquireConnection();
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:02:35 +01:00
|
|
|
/**
|
|
|
|
* Special handle, because tarn.js throw a promise reject that cannot be caught
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async close() {
|
2021-09-10 10:23:45 +01:00
|
|
|
const listener = (reason, p) => {
|
|
|
|
Database.noReject = false;
|
|
|
|
};
|
|
|
|
process.addListener("unhandledRejection", listener);
|
|
|
|
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", "Closing the database");
|
2021-09-10 10:23:45 +01:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
Database.noReject = true;
|
|
|
|
await R.close();
|
|
|
|
await sleep(2000);
|
|
|
|
|
|
|
|
if (Database.noReject) {
|
|
|
|
break;
|
|
|
|
} else {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", "Waiting to close the database");
|
2021-09-10 10:23:45 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", "SQLite closed");
|
2021-09-10 10:23:45 +01:00
|
|
|
|
|
|
|
process.removeListener("unhandledRejection", listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* One backup one time in this process.
|
|
|
|
* Reset this.backupPath if you want to backup again
|
2022-04-20 19:56:40 +01:00
|
|
|
* @param {string} version Version code of backup
|
2021-09-10 10:23:45 +01:00
|
|
|
*/
|
|
|
|
static backup(version) {
|
|
|
|
if (! this.backupPath) {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", "Backing up the database");
|
2021-09-10 10:23:45 +01: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);
|
|
|
|
}
|
2022-06-28 15:11:59 +01:00
|
|
|
|
|
|
|
// Double confirm if all files actually backup
|
|
|
|
if (!fs.existsSync(this.backupPath)) {
|
|
|
|
throw new Error("Backup failed! " + this.backupPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.existsSync(shmPath)) {
|
|
|
|
if (!fs.existsSync(this.backupShmPath)) {
|
|
|
|
throw new Error("Backup failed! " + this.backupShmPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.existsSync(walPath)) {
|
|
|
|
if (!fs.existsSync(this.backupWalPath)) {
|
|
|
|
throw new Error("Backup failed! " + this.backupWalPath);
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 10:23:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 19:56:40 +01:00
|
|
|
/** Restore from most recent backup */
|
2021-09-10 10:23:45 +01:00
|
|
|
static restore() {
|
|
|
|
if (this.backupPath) {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.error("db", "Patching the database failed!!! Restoring the backup");
|
2021-09-10 10:23:45 +01:00
|
|
|
|
|
|
|
const shmPath = Database.path + "-shm";
|
|
|
|
const walPath = Database.path + "-wal";
|
|
|
|
|
2023-02-14 16:43:40 +00:00
|
|
|
// Make sure we have a backup to restore before deleting old db
|
|
|
|
if (
|
|
|
|
!fs.existsSync(this.backupPath)
|
|
|
|
&& !fs.existsSync(shmPath)
|
|
|
|
&& !fs.existsSync(walPath)
|
|
|
|
) {
|
|
|
|
log.error("db", "Backup file not found! Leaving database in failed state.");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2021-09-10 10:23:45 +01:00
|
|
|
// 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) {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.error("db", "Restore failed; you may need to restore the backup manually");
|
2021-09-10 10:23:45 +01: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 {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.info("db", "Nothing to restore");
|
2021-07-21 19:02:35 +01:00
|
|
|
}
|
|
|
|
}
|
2021-10-26 16:02:32 +01:00
|
|
|
|
2022-04-20 19:56:40 +01:00
|
|
|
/** Get the size of the database */
|
2021-10-26 16:02:32 +01:00
|
|
|
static getSize() {
|
2022-04-13 16:33:37 +01:00
|
|
|
log.debug("db", "Database.getSize()");
|
2021-10-26 16:02:32 +01:00
|
|
|
let stats = fs.statSync(Database.path);
|
2022-04-13 16:33:37 +01:00
|
|
|
log.debug("db", stats);
|
2021-10-26 16:02:32 +01:00
|
|
|
return stats.size;
|
|
|
|
}
|
|
|
|
|
2022-04-20 19:56:40 +01:00
|
|
|
/**
|
|
|
|
* Shrink the database
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2021-10-26 16:02:32 +01:00
|
|
|
static async shrink() {
|
|
|
|
await R.exec("VACUUM");
|
|
|
|
}
|
2021-07-21 19:02:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Database;
|