2017-11-17 10:50:00 +08:00
|
|
|
const sql = require('./sql');
|
2018-01-29 10:00:54 +08:00
|
|
|
const source_id = require('./source_id');
|
2017-11-17 10:50:00 +08:00
|
|
|
const utils = require('./utils');
|
2017-12-14 12:03:48 +08:00
|
|
|
const sync_setup = require('./sync_setup');
|
2017-12-23 22:35:00 +08:00
|
|
|
const log = require('./log');
|
2017-11-17 10:50:00 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
async function addNoteSync(noteId, sourceId) {
|
|
|
|
await addEntitySync("notes", noteId, sourceId)
|
2017-11-17 10:50:00 +08:00
|
|
|
}
|
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
async function addNoteTreeSync(noteTreeId, sourceId) {
|
2018-01-29 08:38:05 +08:00
|
|
|
await addEntitySync("note_tree", noteTreeId, sourceId)
|
2017-11-17 10:50:00 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 10:57:46 +08:00
|
|
|
async function addNoteReorderingSync(parentNoteId, sourceId) {
|
|
|
|
await addEntitySync("note_reordering", parentNoteId, sourceId)
|
2017-11-17 10:50:00 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
async function addNoteHistorySync(noteRevisionId, sourceId) {
|
|
|
|
await addEntitySync("note_revisions", noteRevisionId, sourceId);
|
2017-11-17 10:50:00 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
async function addOptionsSync(name, sourceId) {
|
|
|
|
await addEntitySync("options", name, sourceId);
|
2017-11-17 10:50:00 +08:00
|
|
|
}
|
|
|
|
|
2017-12-31 10:44:26 +08:00
|
|
|
async function addRecentNoteSync(noteTreeId, sourceId) {
|
|
|
|
await addEntitySync("recent_notes", noteTreeId, sourceId);
|
2017-11-17 10:50:00 +08:00
|
|
|
}
|
|
|
|
|
2018-01-07 04:56:00 +08:00
|
|
|
async function addImageSync(imageId, sourceId) {
|
|
|
|
await addEntitySync("images", imageId, sourceId);
|
|
|
|
}
|
|
|
|
|
2018-01-07 11:38:53 +08:00
|
|
|
async function addNoteImageSync(noteImageId, sourceId) {
|
2018-01-29 08:38:05 +08:00
|
|
|
await addEntitySync("note_images", noteImageId, sourceId);
|
2018-01-07 10:49:02 +08:00
|
|
|
}
|
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
async function addAttributeSync(attributeId, sourceId) {
|
|
|
|
await addEntitySync("attributes", attributeId, sourceId);
|
2018-01-10 11:09:45 +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,
|
|
|
|
syncDate: utils.nowDate(),
|
2018-01-29 10:00:54 +08:00
|
|
|
sourceId: sourceId || source_id.getCurrentSourceId()
|
2017-12-14 20:57:31 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!sync_setup.isSyncSetup) {
|
|
|
|
// this is because the "server" instances shouldn't have outstanding pushes
|
|
|
|
// useful when you fork the DB for new "client" instance, it won't try to sync the whole DB
|
2018-01-29 08:30:14 +08:00
|
|
|
await sql.execute("UPDATE options SET value = (SELECT MAX(id) FROM sync) WHERE name IN('last_synced_push', 'last_synced_pull')");
|
2017-12-14 09:34:21 +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
|
|
|
}
|
|
|
|
|
|
|
|
async function fillSyncRows(entityName, entityKey) {
|
|
|
|
await cleanupSyncRowsForMissingEntities(entityName, entityKey);
|
|
|
|
|
2017-12-24 00:02:38 +08:00
|
|
|
const entityIds = await sql.getFirstColumn(`SELECT ${entityKey} FROM ${entityName}`);
|
2017-12-23 22:35:00 +08:00
|
|
|
|
|
|
|
for (const entityId of entityIds) {
|
2018-01-29 08:30:14 +08:00
|
|
|
const existingRows = await sql.getFirstValue("SELECT COUNT(id) FROM sync WHERE entityName = ? AND entityId = ?", [entityName, entityId]);
|
2017-12-23 22:35:00 +08:00
|
|
|
|
|
|
|
// 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", {
|
2018-01-29 08:30:14 +08:00
|
|
|
entityName: entityName,
|
|
|
|
entityId: entityId,
|
|
|
|
sourceId: "SYNC_FILL",
|
|
|
|
syncDate: utils.nowDate()
|
2017-12-23 22:35:00 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-24 02:55:13 +08:00
|
|
|
async function fillAllSyncRows() {
|
2018-01-29 08:30:14 +08:00
|
|
|
await fillSyncRows("notes", "noteId");
|
2018-01-29 08:38:05 +08:00
|
|
|
await fillSyncRows("note_tree", "noteTreeId");
|
|
|
|
await fillSyncRows("note_revisions", "noteRevisionId");
|
2018-01-29 08:30:14 +08:00
|
|
|
await fillSyncRows("recent_notes", "noteTreeId");
|
|
|
|
await fillSyncRows("images", "imageId");
|
2018-01-29 08:38:05 +08:00
|
|
|
await fillSyncRows("note_images", "noteImageId");
|
2018-01-29 08:30:14 +08:00
|
|
|
await fillSyncRows("attributes", "attributeId");
|
2017-12-24 02:55:13 +08:00
|
|
|
}
|
|
|
|
|
2017-11-17 10:50:00 +08:00
|
|
|
module.exports = {
|
|
|
|
addNoteSync,
|
|
|
|
addNoteTreeSync,
|
|
|
|
addNoteReorderingSync,
|
|
|
|
addNoteHistorySync,
|
|
|
|
addOptionsSync,
|
2017-12-23 22:35:00 +08:00
|
|
|
addRecentNoteSync,
|
2018-01-07 04:56:00 +08:00
|
|
|
addImageSync,
|
2018-01-07 10:49:02 +08:00
|
|
|
addNoteImageSync,
|
2018-01-10 11:09:45 +08:00
|
|
|
addAttributeSync,
|
2017-12-23 22:35:00 +08:00
|
|
|
cleanupSyncRowsForMissingEntities,
|
2017-12-24 02:55:13 +08:00
|
|
|
fillAllSyncRows
|
2017-11-17 10:50:00 +08:00
|
|
|
};
|