Skip to content

Commit

Permalink
Merge branch 'master' into ntfy-bearer-authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
sharknoon authored Apr 3, 2023
2 parents ba52e1c + 8f449ab commit 6b078b8
Show file tree
Hide file tree
Showing 24 changed files with 1,181 additions and 679 deletions.
11 changes: 11 additions & 0 deletions db/patch-maintenance-cron.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- You should not modify if this have pushed to Github, unless it does serious wrong with the db.
BEGIN TRANSACTION;

DROP TABLE maintenance_timeslot;

-- 999 characters. https://stackoverflow.com/questions/46134830/maximum-length-for-cron-job
ALTER TABLE maintenance ADD cron TEXT;
ALTER TABLE maintenance ADD timezone VARCHAR(255);
ALTER TABLE maintenance ADD duration INTEGER;

COMMIT;
23 changes: 21 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uptime-kuma",
"version": "1.21.1",
"version": "1.21.2-beta.0",
"license": "MIT",
"repository": {
"type": "git",
Expand Down Expand Up @@ -85,6 +85,7 @@
"command-exists": "~1.2.9",
"compare-versions": "~3.6.0",
"compression": "~1.7.4",
"croner": "^6.0.3",
"dayjs": "~1.11.5",
"dotenv": "~16.0.3",
"express": "~4.17.3",
Expand Down Expand Up @@ -146,6 +147,7 @@
"chartjs-adapter-dayjs": "~1.0.0",
"concurrently": "^7.1.0",
"core-js": "~3.26.1",
"cronstrue": "~2.24.0",
"cross-env": "~7.0.3",
"cypress": "^10.1.0",
"delay": "^5.0.0",
Expand Down
115 changes: 2 additions & 113 deletions server/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ class Database {
*/
static patched = false;

/**
* For Backup only
*/
static backupPath = null;

/**
* Add patch filename in key
* Values:
Expand Down Expand Up @@ -74,6 +69,7 @@ class Database {
"patch-add-description-monitor.sql": true,
"patch-api-key-table.sql": true,
"patch-monitor-tls.sql": true,
"patch-maintenance-cron.sql": true,
};

/**
Expand Down Expand Up @@ -198,15 +194,7 @@ class Database {
} else {
log.info("db", "Database patch is needed");

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);
}

// Try catch anything here, if gone wrong, restore the backup
// Try catch anything here
try {
for (let i = version + 1; i <= this.latestVersion; i++) {
const sqlFile = `./db/patch${i}.sql`;
Expand All @@ -222,7 +210,6 @@ class Database {
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");

this.restore();
process.exit(1);
}
}
Expand Down Expand Up @@ -264,8 +251,6 @@ class Database {
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");

this.restore();

process.exit(1);
}

Expand Down Expand Up @@ -367,8 +352,6 @@ class Database {
}
}

this.backup(dayjs().format("YYYYMMDDHHmmss"));

log.info("db", sqlFilename + " is patching");
this.patched = true;
await this.importSQLFile("./db/" + sqlFilename);
Expand Down Expand Up @@ -450,100 +433,6 @@ class Database {
process.removeListener("unhandledRejection", listener);
}

/**
* One backup one time in this process.
* Reset this.backupPath if you want to backup again
* @param {string} version Version code of backup
*/
static backup(version) {
if (! this.backupPath) {
log.info("db", "Backing up the database");
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);
}

// 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);
}
}
}
}

/** Restore from most recent backup */
static restore() {
if (this.backupPath) {
log.error("db", "Patching the database failed!!! Restoring the backup");

const shmPath = Database.path + "-shm";
const walPath = Database.path + "-wal";

// 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);
}

// 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) {
log.error("db", "Restore failed; you may need to restore the backup manually");
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 {
log.info("db", "Nothing to restore");
}
}

/** Get the size of the database */
static getSize() {
log.debug("db", "Database.getSize()");
Expand Down
Loading

0 comments on commit 6b078b8

Please sign in to comment.