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');
|
2018-03-31 07:41:54 +08:00
|
|
|
const cls = require('./cls');
|
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
|
|
|
}
|
|
|
|
|
2018-03-25 09:39:15 +08:00
|
|
|
async function addBranchSync(branchId, sourceId) {
|
|
|
|
await addEntitySync("branches", branchId, 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-03-26 08:52:38 +08:00
|
|
|
async function addNoteRevisionSync(noteRevisionId, sourceId) {
|
2018-01-29 08:38:05 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-25 09:39:15 +08:00
|
|
|
async function addRecentNoteSync(branchId, sourceId) {
|
|
|
|
await addEntitySync("recent_notes", branchId, 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-03-25 10:02:26 +08:00
|
|
|
async function addLabelSync(labelId, sourceId) {
|
|
|
|
await addEntitySync("labels", labelId, sourceId);
|
2018-01-10 11:09:45 +08:00
|
|
|
}
|
|
|
|
|
2018-02-11 13:18:59 +08:00
|
|
|
async function addApiTokenSync(apiTokenId, sourceId) {
|
|
|
|
await addEntitySync("api_tokens", apiTokenId, sourceId);
|
|
|
|
}
|
|
|
|
|
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-03-31 07:41:54 +08:00
|
|
|
sourceId: sourceId || cls.getSourceId() || 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);
|
|
|
|
|
2018-01-30 06:41:59 +08:00
|
|
|
const entityIds = await sql.getColumn(`SELECT ${entityKey} FROM ${entityName}`);
|
2017-12-23 22:35:00 +08:00
|
|
|
|
|
|
|
for (const entityId of entityIds) {
|
2018-01-30 06:41:59 +08:00
|
|
|
const existingRows = await sql.getValue("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-03-25 09:39:15 +08:00
|
|
|
await fillSyncRows("branches", "branchId");
|
2018-01-29 08:38:05 +08:00
|
|
|
await fillSyncRows("note_revisions", "noteRevisionId");
|
2018-03-25 09:39:15 +08:00
|
|
|
await fillSyncRows("recent_notes", "branchId");
|
2018-01-29 08:30:14 +08:00
|
|
|
await fillSyncRows("images", "imageId");
|
2018-01-29 08:38:05 +08:00
|
|
|
await fillSyncRows("note_images", "noteImageId");
|
2018-03-25 10:02:26 +08:00
|
|
|
await fillSyncRows("labels", "labelId");
|
2018-02-11 13:18:59 +08:00
|
|
|
await fillSyncRows("api_tokens", "apiTokenId");
|
2017-12-24 02:55:13 +08:00
|
|
|
}
|
|
|
|
|
2017-11-17 10:50:00 +08:00
|
|
|
module.exports = {
|
|
|
|
addNoteSync,
|
2018-03-25 09:39:15 +08:00
|
|
|
addBranchSync,
|
2017-11-17 10:50:00 +08:00
|
|
|
addNoteReorderingSync,
|
2018-03-26 08:52:38 +08:00
|
|
|
addNoteRevisionSync,
|
2017-11-17 10:50:00 +08:00
|
|
|
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-03-25 10:02:26 +08:00
|
|
|
addLabelSync,
|
2018-02-11 13:18:59 +08:00
|
|
|
addApiTokenSync,
|
2018-01-31 09:12:19 +08:00
|
|
|
addEntitySync,
|
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
|
|
|
};
|