2017-11-17 10:50:00 +08:00
|
|
|
const sql = require('./sql');
|
2018-04-02 09:27:46 +08:00
|
|
|
const sourceIdService = require('./source_id');
|
2018-04-03 08:46:46 +08:00
|
|
|
const dateUtils = require('./date_utils');
|
2017-12-23 22:35:00 +08:00
|
|
|
const log = require('./log');
|
2018-03-31 07:41:54 +08:00
|
|
|
const cls = require('./cls');
|
2018-04-19 11:11:30 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
async function addEntitySync(entityName, entityId, sourceId) {
|
2017-12-14 20:57:31 +08:00
|
|
|
await sql.replace("sync", {
|
2018-01-29 08:30:14 +08:00
|
|
|
entityName: entityName,
|
|
|
|
entityId: entityId,
|
2019-03-14 05:43:59 +08:00
|
|
|
utcSyncDate: dateUtils.utcNowDateTime(),
|
2018-04-02 09:27:46 +08:00
|
|
|
sourceId: sourceId || cls.getSourceId() || sourceIdService.getCurrentSourceId()
|
2017-12-14 20:57:31 +08:00
|
|
|
});
|
2017-11-17 10:50:00 +08:00
|
|
|
}
|
|
|
|
|
2017-12-23 22:35:00 +08:00
|
|
|
async function cleanupSyncRowsForMissingEntities(entityName, entityKey) {
|
|
|
|
await sql.execute(`
|
|
|
|
DELETE
|
|
|
|
FROM sync
|
2018-01-29 08:30:14 +08:00
|
|
|
WHERE sync.entityName = '${entityName}'
|
|
|
|
AND sync.entityId NOT IN (SELECT ${entityKey} FROM ${entityName})`);
|
2017-12-23 22:35:00 +08:00
|
|
|
}
|
|
|
|
|
2018-07-25 02:35:03 +08:00
|
|
|
async function fillSyncRows(entityName, entityKey, condition = '') {
|
2019-06-06 04:07:12 +08:00
|
|
|
try {
|
|
|
|
await cleanupSyncRowsForMissingEntities(entityName, entityKey);
|
|
|
|
|
|
|
|
const entityIds = await sql.getColumn(`SELECT ${entityKey} FROM ${entityName}`
|
|
|
|
+ (condition ? ` WHERE ${condition}` : ''));
|
|
|
|
|
2019-10-03 05:22:58 +08:00
|
|
|
let createdCount = 0;
|
|
|
|
|
2019-06-06 04:07:12 +08:00
|
|
|
for (const entityId of entityIds) {
|
|
|
|
const existingRows = await sql.getValue("SELECT COUNT(id) FROM sync WHERE entityName = ? AND entityId = ?", [entityName, entityId]);
|
|
|
|
|
|
|
|
// we don't want to replace existing entities (which would effectively cause full resync)
|
|
|
|
if (existingRows === 0) {
|
2019-10-03 05:22:58 +08:00
|
|
|
createdCount++;
|
2019-06-06 04:07:12 +08:00
|
|
|
|
|
|
|
await sql.insert("sync", {
|
|
|
|
entityName: entityName,
|
|
|
|
entityId: entityId,
|
|
|
|
sourceId: "SYNC_FILL",
|
|
|
|
utcSyncDate: dateUtils.utcNowDateTime()
|
|
|
|
});
|
|
|
|
}
|
2017-12-23 22:35:00 +08:00
|
|
|
}
|
2019-10-03 05:22:58 +08:00
|
|
|
|
|
|
|
if (createdCount > 0) {
|
|
|
|
log.info(`Created ${createdCount} missing sync records for ${entityName}.`);
|
|
|
|
}
|
2017-12-23 22:35:00 +08:00
|
|
|
}
|
2019-06-06 04:07:12 +08:00
|
|
|
catch (e) {
|
|
|
|
// this is to fix migration from 0.30 to 0.32, can be removed later
|
|
|
|
// see https://github.com/zadam/trilium/issues/557
|
|
|
|
log.error(`Filling sync rows failed for ${entityName} ${entityKey} with error "${e.message}", continuing`);
|
|
|
|
}
|
2017-12-23 22:35:00 +08:00
|
|
|
}
|
|
|
|
|
2017-12-24 02:55:13 +08:00
|
|
|
async function fillAllSyncRows() {
|
2018-04-08 10:25:28 +08:00
|
|
|
await sql.execute("DELETE FROM sync");
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
await fillSyncRows("notes", "noteId");
|
2019-03-28 04:04:25 +08:00
|
|
|
await fillSyncRows("note_contents", "noteId");
|
2018-03-25 09:39:15 +08:00
|
|
|
await fillSyncRows("branches", "branchId");
|
2018-01-29 08:38:05 +08:00
|
|
|
await fillSyncRows("note_revisions", "noteRevisionId");
|
2019-11-02 03:00:56 +08:00
|
|
|
await fillSyncRows("note_revision_contents", "noteRevisionId");
|
2019-05-22 03:47:28 +08:00
|
|
|
await fillSyncRows("recent_notes", "noteId");
|
2018-08-03 04:48:21 +08:00
|
|
|
await fillSyncRows("attributes", "attributeId");
|
2018-02-11 13:18:59 +08:00
|
|
|
await fillSyncRows("api_tokens", "apiTokenId");
|
2018-07-25 02:35:03 +08:00
|
|
|
await fillSyncRows("options", "name", 'isSynced = 1');
|
2017-12-24 02:55:13 +08:00
|
|
|
}
|
|
|
|
|
2017-11-17 10:50:00 +08:00
|
|
|
module.exports = {
|
2019-11-01 04:58:34 +08:00
|
|
|
addNoteSync: async (noteId, sourceId) => await addEntitySync("notes", noteId, sourceId),
|
|
|
|
addNoteContentSync: async (noteId, sourceId) => await addEntitySync("note_contents", noteId, sourceId),
|
|
|
|
addBranchSync: async (branchId, sourceId) => await addEntitySync("branches", branchId, sourceId),
|
|
|
|
addNoteReorderingSync: async (parentNoteId, sourceId) => await addEntitySync("note_reordering", parentNoteId, sourceId),
|
|
|
|
addNoteRevisionSync: async (noteRevisionId, sourceId) => await addEntitySync("note_revisions", noteRevisionId, sourceId),
|
2019-11-02 03:00:56 +08:00
|
|
|
addNoteRevisionContentSync: async (noteRevisionId, sourceId) => await addEntitySync("note_revision_contents", noteRevisionId, sourceId),
|
2019-11-01 04:58:34 +08:00
|
|
|
addOptionsSync: async (name, sourceId) => await addEntitySync("options", name, sourceId),
|
|
|
|
addRecentNoteSync: async (noteId, sourceId) => await addEntitySync("recent_notes", noteId, sourceId),
|
|
|
|
addAttributeSync: async (attributeId, sourceId) => await addEntitySync("attributes", attributeId, sourceId),
|
|
|
|
addApiTokenSync: async (apiTokenId, sourceId) => await addEntitySync("api_tokens", apiTokenId, sourceId),
|
2018-01-31 09:12:19 +08:00
|
|
|
addEntitySync,
|
2017-12-24 02:55:13 +08:00
|
|
|
fillAllSyncRows
|
2017-11-17 10:50:00 +08:00
|
|
|
};
|