2017-12-23 22:35:00 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const utils = require('../../services/utils');
|
|
|
|
const sync_table = require('../../services/sync_table');
|
2017-12-23 22:57:20 +08:00
|
|
|
const auth = require('../../services/auth');
|
2017-12-24 02:16:18 +08:00
|
|
|
const log = require('../../services/log');
|
2018-01-07 22:35:44 +08:00
|
|
|
const wrap = require('express-promise-wrap').wrap;
|
2017-12-23 22:35:00 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.post('/cleanup-soft-deleted-items', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-12-23 22:35:00 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2018-01-30 06:41:59 +08:00
|
|
|
const noteIdsToDelete = await sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1");
|
2017-12-23 22:35:00 +08:00
|
|
|
const noteIdsSql = noteIdsToDelete
|
|
|
|
.map(noteId => "'" + utils.sanitizeSql(noteId) + "'")
|
|
|
|
.join(', ');
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sql.execute(`DELETE FROM event_log WHERE noteId IN (${noteIdsSql})`);
|
2017-12-23 22:35:00 +08:00
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
await sql.execute(`DELETE FROM note_revisions WHERE noteId IN (${noteIdsSql})`);
|
2017-12-23 22:35:00 +08:00
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
await sql.execute(`DELETE FROM note_images WHERE noteId IN (${noteIdsSql})`);
|
2018-01-10 11:09:45 +08:00
|
|
|
|
2018-03-25 10:02:26 +08:00
|
|
|
await sql.execute(`DELETE FROM labels WHERE noteId IN (${noteIdsSql})`);
|
2018-01-10 11:09:45 +08:00
|
|
|
|
2018-03-25 09:39:15 +08:00
|
|
|
await sql.execute("DELETE FROM branches WHERE isDeleted = 1");
|
2017-12-23 22:35:00 +08:00
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
await sql.execute("DELETE FROM note_images WHERE isDeleted = 1");
|
2018-01-08 03:07:59 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sql.execute("DELETE FROM images WHERE isDeleted = 1");
|
2018-01-08 03:07:59 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sql.execute("DELETE FROM notes WHERE isDeleted = 1");
|
2017-12-23 22:35:00 +08:00
|
|
|
|
|
|
|
await sql.execute("DELETE FROM recent_notes");
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sync_table.cleanupSyncRowsForMissingEntities("notes", "noteId");
|
2018-03-25 09:39:15 +08:00
|
|
|
await sync_table.cleanupSyncRowsForMissingEntities("branches", "branchId");
|
2018-01-29 08:38:05 +08:00
|
|
|
await sync_table.cleanupSyncRowsForMissingEntities("note_revisions", "noteRevisionId");
|
2018-03-25 09:39:15 +08:00
|
|
|
await sync_table.cleanupSyncRowsForMissingEntities("recent_notes", "branchId");
|
2017-12-24 02:16:18 +08:00
|
|
|
|
|
|
|
log.info("Following notes has been completely cleaned from database: " + noteIdsSql);
|
2017-12-23 22:35:00 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
res.send({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-12-23 22:35:00 +08:00
|
|
|
|
2018-01-08 03:07:59 +08:00
|
|
|
router.post('/cleanup-unused-images', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2018-01-29 10:57:46 +08:00
|
|
|
const sourceId = req.headers.source_id;
|
2018-01-08 03:07:59 +08:00
|
|
|
|
|
|
|
await sql.doInTransaction(async () => {
|
2018-01-30 06:41:59 +08:00
|
|
|
const unusedImageIds = await sql.getColumn(`
|
2018-01-29 08:30:14 +08:00
|
|
|
SELECT images.imageId
|
2018-01-08 03:07:59 +08:00
|
|
|
FROM images
|
2018-01-29 08:38:05 +08:00
|
|
|
LEFT JOIN note_images ON note_images.imageId = images.imageId AND note_images.isDeleted = 0
|
2018-01-08 03:07:59 +08:00
|
|
|
WHERE
|
2018-01-29 08:30:14 +08:00
|
|
|
images.isDeleted = 0
|
2018-01-29 08:38:05 +08:00
|
|
|
AND note_images.noteImageId IS NULL`);
|
2018-01-08 03:07:59 +08:00
|
|
|
|
|
|
|
const now = utils.nowDate();
|
|
|
|
|
|
|
|
for (const imageId of unusedImageIds) {
|
|
|
|
log.info(`Deleting unused image: ${imageId}`);
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await sql.execute("UPDATE images SET isDeleted = 1, data = null, dateModified = ? WHERE imageId = ?",
|
2018-01-08 03:07:59 +08:00
|
|
|
[now, imageId]);
|
|
|
|
|
|
|
|
await sync_table.addImageSync(imageId, sourceId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
res.send({});
|
|
|
|
}));
|
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.post('/vacuum-database', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-12-23 22:35:00 +08:00
|
|
|
await sql.execute("VACUUM");
|
|
|
|
|
2017-12-24 02:16:18 +08:00
|
|
|
log.info("Database has been vacuumed.");
|
|
|
|
|
2017-12-23 22:35:00 +08:00
|
|
|
res.send({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-12-23 22:35:00 +08:00
|
|
|
|
|
|
|
module.exports = router;
|