mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
functionality to fill up / cleanup sync rows compared to the entity rows
This commit is contained in:
parent
72712bc24b
commit
5403f340ec
@ -154,6 +154,7 @@ settings.addModule((async function () {
|
|||||||
|
|
||||||
settings.addModule((async function () {
|
settings.addModule((async function () {
|
||||||
const forceFullSyncButton = $("#force-full-sync-button");
|
const forceFullSyncButton = $("#force-full-sync-button");
|
||||||
|
const fillSyncRowsButton = $("#fill-sync-rows-button");
|
||||||
|
|
||||||
forceFullSyncButton.click(async () => {
|
forceFullSyncButton.click(async () => {
|
||||||
await server.post('sync/force-full-sync');
|
await server.post('sync/force-full-sync');
|
||||||
@ -161,6 +162,12 @@ settings.addModule((async function () {
|
|||||||
showMessage("Full sync triggered");
|
showMessage("Full sync triggered");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
fillSyncRowsButton.click(async () => {
|
||||||
|
await server.post('sync/fill-sync-rows');
|
||||||
|
|
||||||
|
showMessage("Sync rows filled successfully");
|
||||||
|
});
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
})());
|
})());
|
||||||
|
|
||||||
|
@ -8,6 +8,8 @@ const syncUpdate = require('../../services/sync_update');
|
|||||||
const sql = require('../../services/sql');
|
const sql = require('../../services/sql');
|
||||||
const options = require('../../services/options');
|
const options = require('../../services/options');
|
||||||
const content_hash = require('../../services/content_hash');
|
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) => {
|
router.get('/check', auth.checkApiAuth, async (req, res, next) => {
|
||||||
res.send({
|
res.send({
|
||||||
@ -20,6 +22,44 @@ router.post('/now', auth.checkApiAuth, async (req, res, next) => {
|
|||||||
res.send(await sync.sync());
|
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) => {
|
router.post('/force-full-sync', auth.checkApiAuth, async (req, res, next) => {
|
||||||
await sql.doInTransaction(async () => {
|
await sql.doInTransaction(async () => {
|
||||||
await options.setOption('last_synced_pull', 0);
|
await options.setOption('last_synced_pull', 0);
|
||||||
|
@ -16,7 +16,7 @@ async function runCheck(query, errorText, errorList) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function runSyncRowChecks(table, key, 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);
|
`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`,
|
await runCheck(`SELECT entity_id FROM sync LEFT JOIN ${table} ON entity_id = ${key} WHERE sync.entity_name = '${table}' AND ${key} IS NULL`,
|
||||||
|
@ -219,6 +219,11 @@
|
|||||||
</div>
|
</div>
|
||||||
<div id="sync">
|
<div id="sync">
|
||||||
<button id="force-full-sync-button" class="btn btn-sm">Force full sync</button>
|
<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>
|
||||||
<div id="debugging">
|
<div id="debugging">
|
||||||
<button id="anonymize-button" class="btn btn-sm">Save anonymized database</button>
|
<button id="anonymize-button" class="btn btn-sm">Save anonymized database</button>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user