mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-07-23 23:11:13 -04:00
add db migration
This commit is contained in:
parent
1c0dc18d72
commit
e02eb72863
4 changed files with 198 additions and 28 deletions
|
@ -12,6 +12,7 @@ const fs = require("fs");
|
|||
const {getSettings} = require("./util-server");
|
||||
const {Notification} = require("./notification")
|
||||
const gracefulShutdown = require('http-graceful-shutdown');
|
||||
const Database = require("./database");
|
||||
const {sleep} = require("./util");
|
||||
const args = require('args-parser')(process.argv);
|
||||
|
||||
|
@ -27,9 +28,28 @@ const server = http.createServer(app);
|
|||
const io = new Server(server);
|
||||
app.use(express.json())
|
||||
|
||||
/**
|
||||
* Total WebSocket client connected to server currently, no actual use
|
||||
* @type {number}
|
||||
*/
|
||||
let totalClient = 0;
|
||||
|
||||
/**
|
||||
* Use for decode the auth object
|
||||
* @type {null}
|
||||
*/
|
||||
let jwtSecret = null;
|
||||
|
||||
/**
|
||||
* Main monitor list
|
||||
* @type {{}}
|
||||
*/
|
||||
let monitorList = {};
|
||||
|
||||
/**
|
||||
* Show Setup Page
|
||||
* @type {boolean}
|
||||
*/
|
||||
let needSetup = false;
|
||||
|
||||
(async () => {
|
||||
|
@ -555,19 +575,21 @@ function checkLogin(socket) {
|
|||
}
|
||||
|
||||
async function initDatabase() {
|
||||
const path = './data/kuma.db';
|
||||
|
||||
if (! fs.existsSync(path)) {
|
||||
if (! fs.existsSync(Database.path)) {
|
||||
console.log("Copying Database")
|
||||
fs.copyFileSync("./db/kuma.db", path);
|
||||
fs.copyFileSync(Database.templatePath, Database.path);
|
||||
}
|
||||
|
||||
console.log("Connecting to Database")
|
||||
R.setup('sqlite', {
|
||||
filename: path
|
||||
filename: Database.path
|
||||
});
|
||||
console.log("Connected")
|
||||
|
||||
// Patch the database
|
||||
await Database.patch()
|
||||
|
||||
// Auto map the model to a bean object
|
||||
R.freeze(true)
|
||||
await R.autoloadModels("./server/model");
|
||||
|
||||
|
@ -587,6 +609,7 @@ async function initDatabase() {
|
|||
console.log("Load JWT secret from database.")
|
||||
}
|
||||
|
||||
// If there is no record in user table, it is a new Uptime Kuma instance, need to setup
|
||||
if ((await R.count("user")) === 0) {
|
||||
console.log("No user, need setup")
|
||||
needSetup = true;
|
||||
|
@ -705,11 +728,6 @@ const startGracefulShutdown = async () => {
|
|||
|
||||
}
|
||||
|
||||
let noReject = true;
|
||||
process.on('unhandledRejection', (reason, p) => {
|
||||
noReject = false;
|
||||
});
|
||||
|
||||
async function shutdownFunction(signal) {
|
||||
console.log('Called signal: ' + signal);
|
||||
|
||||
|
@ -718,24 +736,8 @@ async function shutdownFunction(signal) {
|
|||
let monitor = monitorList[id]
|
||||
monitor.stop()
|
||||
}
|
||||
await sleep(2000)
|
||||
|
||||
console.log("Closing DB")
|
||||
|
||||
// Special handle, because tarn.js throw a promise reject that cannot be caught
|
||||
while (true) {
|
||||
noReject = true;
|
||||
await R.close()
|
||||
await sleep(2000)
|
||||
|
||||
if (noReject) {
|
||||
break;
|
||||
} else {
|
||||
console.log("Waiting...")
|
||||
}
|
||||
}
|
||||
|
||||
console.log("OK")
|
||||
await sleep(2000);
|
||||
await Database.close();
|
||||
}
|
||||
|
||||
function finalFunction() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue