2017-12-23 22:35:00 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const utils = require('../../services/utils');
|
2018-04-02 09:27:46 +08:00
|
|
|
const syncTable = require('../../services/sync_table');
|
2017-12-24 02:16:18 +08:00
|
|
|
const log = require('../../services/log');
|
2018-04-01 23:05:09 +08:00
|
|
|
const repository = require('../../services/repository');
|
2017-12-23 22:35:00 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
async function cleanupSoftDeletedItems() {
|
|
|
|
const noteIdsToDelete = await sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1");
|
|
|
|
const noteIdsSql = noteIdsToDelete
|
|
|
|
.map(noteId => "'" + utils.sanitizeSql(noteId) + "'")
|
|
|
|
.join(', ');
|
2017-12-23 22:35:00 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
await sql.execute(`DELETE FROM event_log WHERE noteId IN (${noteIdsSql})`);
|
2017-12-23 22:35:00 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
await sql.execute(`DELETE FROM note_revisions WHERE noteId IN (${noteIdsSql})`);
|
2017-12-23 22:35:00 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
await sql.execute(`DELETE FROM note_images WHERE noteId IN (${noteIdsSql})`);
|
2018-01-10 11:09:45 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
await sql.execute(`DELETE FROM labels WHERE noteId IN (${noteIdsSql})`);
|
2018-01-10 11:09:45 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
await sql.execute("DELETE FROM branches WHERE isDeleted = 1");
|
2017-12-23 22:35:00 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
await sql.execute("DELETE FROM note_images WHERE isDeleted = 1");
|
2018-01-08 03:07:59 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
await sql.execute("DELETE FROM images WHERE isDeleted = 1");
|
2018-01-08 03:07:59 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
await sql.execute("DELETE FROM notes WHERE isDeleted = 1");
|
2017-12-23 22:35:00 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
await sql.execute("DELETE FROM recent_notes");
|
2017-12-23 22:35:00 +08:00
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
await syncTable.cleanupSyncRowsForMissingEntities("notes", "noteId");
|
|
|
|
await syncTable.cleanupSyncRowsForMissingEntities("branches", "branchId");
|
|
|
|
await syncTable.cleanupSyncRowsForMissingEntities("note_revisions", "noteRevisionId");
|
|
|
|
await syncTable.cleanupSyncRowsForMissingEntities("recent_notes", "branchId");
|
|
|
|
await syncTable.cleanupSyncRowsForMissingEntities("images", "imageId");
|
|
|
|
await syncTable.cleanupSyncRowsForMissingEntities("note_images", "noteImageId");
|
|
|
|
await syncTable.cleanupSyncRowsForMissingEntities("labels", "labelId");
|
2017-12-24 02:16:18 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
log.info("Following notes has been completely cleaned from database: " + noteIdsSql);
|
|
|
|
}
|
2017-12-23 22:35:00 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
async function cleanupUnusedImages() {
|
|
|
|
const unusedImageIds = await sql.getColumn(`
|
|
|
|
SELECT images.imageId
|
|
|
|
FROM images
|
|
|
|
LEFT JOIN note_images ON note_images.imageId = images.imageId AND note_images.isDeleted = 0
|
|
|
|
WHERE
|
|
|
|
images.isDeleted = 0
|
|
|
|
AND note_images.noteImageId IS NULL`);
|
2018-01-08 03:07:59 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
const now = utils.nowDate();
|
2018-01-08 03:07:59 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
for (const imageId of unusedImageIds) {
|
|
|
|
log.info(`Deleting unused image: ${imageId}`);
|
2018-01-08 03:07:59 +08:00
|
|
|
|
2018-04-01 23:05:09 +08:00
|
|
|
const image = await repository.getImage(imageId);
|
|
|
|
image.isDeleted = true;
|
|
|
|
image.data = null;
|
|
|
|
await image.save();
|
2018-03-31 05:07:41 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-08 03:07:59 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
async function vacuumDatabase() {
|
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.");
|
2018-03-31 05:07:41 +08:00
|
|
|
}
|
2017-12-24 02:16:18 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
module.exports = {
|
|
|
|
cleanupSoftDeletedItems,
|
|
|
|
cleanupUnusedImages,
|
|
|
|
vacuumDatabase
|
|
|
|
};
|