functionality to fill up / cleanup sync rows compared to the entity rows

This commit is contained in:
azivner 2017-12-19 22:04:51 -05:00
parent 72712bc24b
commit 5403f340ec
4 changed files with 53 additions and 1 deletions

View File

@ -154,6 +154,7 @@ settings.addModule((async function () {
settings.addModule((async function () {
const forceFullSyncButton = $("#force-full-sync-button");
const fillSyncRowsButton = $("#fill-sync-rows-button");
forceFullSyncButton.click(async () => {
await server.post('sync/force-full-sync');
@ -161,6 +162,12 @@ settings.addModule((async function () {
showMessage("Full sync triggered");
});
fillSyncRowsButton.click(async () => {
await server.post('sync/fill-sync-rows');
showMessage("Sync rows filled successfully");
});
return {};
})());

View File

@ -8,6 +8,8 @@ const syncUpdate = require('../../services/sync_update');
const sql = require('../../services/sql');
const options = require('../../services/options');
const content_hash = require('../../services/content_hash');
const utils = require('../../services/utils');
const log = require('../../services/log');
router.get('/check', auth.checkApiAuth, async (req, res, next) => {
res.send({
@ -20,6 +22,44 @@ router.post('/now', auth.checkApiAuth, async (req, res, next) => {
res.send(await sync.sync());
});
async function fillSyncRows(entityName, entityKey) {
// cleanup sync rows for missing entities
await sql.execute(`
DELETE
FROM sync
WHERE sync.entity_name = '${entityName}'
AND sync.entity_id NOT IN (SELECT ${entityKey} FROM ${entityName})`);
const entityIds = await sql.getFlattenedResults(`SELECT ${entityKey} FROM ${entityName}`);
for (const entityId of entityIds) {
const existingRows = await sql.getSingleValue("SELECT COUNT(id) FROM sync WHERE entity_name = ? AND entity_id = ?", [entityName, entityId]);
// we don't want to replace existing entities (which would effectively cause full resync)
if (existingRows === 0) {
log.info(`Creating missing sync record for ${entityName} ${entityId}`);
await sql.insert("sync", {
entity_name: entityName,
entity_id: entityId,
source_id: "SYNC_FILL",
sync_date: utils.nowDate()
});
}
}
}
router.post('/fill-sync-rows', auth.checkApiAuth, async (req, res, next) => {
await sql.doInTransaction(async () => {
await fillSyncRows("notes", "note_id");
await fillSyncRows("notes_tree", "note_tree_id");
await fillSyncRows("notes_history", "note_history_id");
await fillSyncRows("recent_notes", "note_tree_id");
});
res.send({});
});
router.post('/force-full-sync', auth.checkApiAuth, async (req, res, next) => {
await sql.doInTransaction(async () => {
await options.setOption('last_synced_pull', 0);

View File

@ -16,7 +16,7 @@ async function runCheck(query, errorText, errorList) {
}
async function runSyncRowChecks(table, key, errorList) {
await runCheck(`SELECT ${key} FROM ${table} LEFT JOIN sync ON sync.entity_name = '${table}' AND entity_id = ${key} WHERE entity_id != 'root' AND sync.id IS NULL`,
await runCheck(`SELECT ${key} FROM ${table} LEFT JOIN sync ON sync.entity_name = '${table}' AND entity_id = ${key} WHERE sync.id IS NULL`,
`Missing sync records for ${key} in table ${table}`, errorList);
await runCheck(`SELECT entity_id FROM sync LEFT JOIN ${table} ON entity_id = ${key} WHERE sync.entity_name = '${table}' AND ${key} IS NULL`,

View File

@ -219,6 +219,11 @@
</div>
<div id="sync">
<button id="force-full-sync-button" class="btn btn-sm">Force full sync</button>
<br/>
<br/>
<button id="fill-sync-rows-button" class="btn btn-sm">Fill sync rows</button>
</div>
<div id="debugging">
<button id="anonymize-button" class="btn btn-sm">Save anonymized database</button>