| 
									
										
										
										
											2020-03-09 21:28:41 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * TODO: rename "sync" table to something like "changelog" since it now also contains rows which are not synced (isSynced=false) | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | const sql = require('./sql'); | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  | const sourceIdService = require('./source_id'); | 
					
						
							| 
									
										
										
										
											2018-04-02 20:46:46 -04:00
										 |  |  | const dateUtils = require('./date_utils'); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | const log = require('./log'); | 
					
						
							| 
									
										
										
										
											2018-03-30 19:41:54 -04:00
										 |  |  | const cls = require('./cls'); | 
					
						
							| 
									
										
										
										
											2018-04-18 23:11:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 21:28:41 +01:00
										 |  |  | async function insertEntitySync(entityName, entityId, sourceId, isSynced) { | 
					
						
							| 
									
										
										
										
											2019-12-02 22:27:06 +01:00
										 |  |  |     const sync = { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         entityName: entityName, | 
					
						
							|  |  |  |         entityId: entityId, | 
					
						
							| 
									
										
										
										
											2019-03-13 22:43:59 +01:00
										 |  |  |         utcSyncDate: dateUtils.utcNowDateTime(), | 
					
						
							| 
									
										
										
										
											2020-03-09 20:56:43 +01:00
										 |  |  |         sourceId: sourceId || cls.getSourceId() || sourceIdService.getCurrentSourceId(), | 
					
						
							| 
									
										
										
										
											2020-03-09 21:28:41 +01:00
										 |  |  |         isSynced: isSynced | 
					
						
							| 
									
										
										
										
											2019-12-02 22:27:06 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sync.id = await sql.replace("sync", sync); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 22:58:30 +01:00
										 |  |  |     return sync; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 21:28:41 +01:00
										 |  |  | async function addEntitySync(entityName, entityId, sourceId, isSynced = true) { | 
					
						
							|  |  |  |     const sync = await insertEntitySync(entityName, entityId, sourceId, isSynced); | 
					
						
							| 
									
										
										
										
											2019-12-18 22:58:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 22:32:24 +01:00
										 |  |  |     cls.addSyncRow(sync); | 
					
						
							| 
									
										
										
										
											2019-12-02 22:27:06 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 22:58:30 +01:00
										 |  |  | async function addEntitySyncsForSector(entityName, entityPrimaryKey, sector) { | 
					
						
							|  |  |  |     const entityIds = await sql.getColumn(`SELECT ${entityPrimaryKey} FROM ${entityName} WHERE SUBSTR(${entityPrimaryKey}, 1, 1) = ?`, [sector]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const entityId of entityIds) { | 
					
						
							| 
									
										
										
										
											2020-03-09 21:28:41 +01:00
										 |  |  |         if (entityName === 'options') { | 
					
						
							|  |  |  |             const isSynced = await sql.getValue(`SELECT isSynced FROM options WHERE name = ?`, [entityId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!isSynced) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await insertEntitySync(entityName, entityId, 'content-check', true); | 
					
						
							| 
									
										
										
										
											2019-12-18 22:58:30 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function cleanupSyncRowsForMissingEntities(entityName, entityPrimaryKey) { | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |     await sql.execute(`
 | 
					
						
							|  |  |  |       DELETE  | 
					
						
							|  |  |  |       FROM sync  | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |       WHERE sync.entityName = '${entityName}'  | 
					
						
							| 
									
										
										
										
											2019-12-18 22:58:30 +01:00
										 |  |  |         AND sync.entityId NOT IN (SELECT ${entityPrimaryKey} FROM ${entityName})`);
 | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 22:58:30 +01:00
										 |  |  | async function fillSyncRows(entityName, entityPrimaryKey, condition = '') { | 
					
						
							| 
									
										
										
										
											2019-06-05 22:07:12 +02:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2019-12-18 22:58:30 +01:00
										 |  |  |         await cleanupSyncRowsForMissingEntities(entityName, entityPrimaryKey); | 
					
						
							| 
									
										
										
										
											2019-06-05 22:07:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 22:58:30 +01:00
										 |  |  |         const entityIds = await sql.getColumn(`SELECT ${entityPrimaryKey} FROM ${entityName}` | 
					
						
							| 
									
										
										
										
											2019-06-05 22:07:12 +02:00
										 |  |  |             + (condition ? ` WHERE ${condition}` : '')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 23:22:58 +02:00
										 |  |  |         let createdCount = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-05 22:07:12 +02:00
										 |  |  |         for (const entityId of entityIds) { | 
					
						
							| 
									
										
										
										
											2019-12-01 15:01:09 +01:00
										 |  |  |             const existingRows = await sql.getValue("SELECT COUNT(1) FROM sync WHERE entityName = ? AND entityId = ?", [entityName, entityId]); | 
					
						
							| 
									
										
										
										
											2019-06-05 22:07:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // we don't want to replace existing entities (which would effectively cause full resync)
 | 
					
						
							|  |  |  |             if (existingRows === 0) { | 
					
						
							| 
									
										
										
										
											2019-10-02 23:22:58 +02:00
										 |  |  |                 createdCount++; | 
					
						
							| 
									
										
										
										
											2019-06-05 22:07:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 await sql.insert("sync", { | 
					
						
							|  |  |  |                     entityName: entityName, | 
					
						
							|  |  |  |                     entityId: entityId, | 
					
						
							|  |  |  |                     sourceId: "SYNC_FILL", | 
					
						
							|  |  |  |                     utcSyncDate: dateUtils.utcNowDateTime() | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-02 23:22:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (createdCount > 0) { | 
					
						
							|  |  |  |             log.info(`Created ${createdCount} missing sync records for ${entityName}.`); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-05 22:07:12 +02: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
 | 
					
						
							| 
									
										
										
										
											2019-12-18 22:58:30 +01:00
										 |  |  |         log.error(`Filling sync rows failed for ${entityName} ${entityPrimaryKey} with error "${e.message}", continuing`); | 
					
						
							| 
									
										
										
										
											2019-06-05 22:07:12 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 13:55:13 -05:00
										 |  |  | async function fillAllSyncRows() { | 
					
						
							| 
									
										
										
										
											2018-04-07 22:25:28 -04:00
										 |  |  |     await sql.execute("DELETE FROM sync"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     await fillSyncRows("notes", "noteId"); | 
					
						
							| 
									
										
										
										
											2019-03-27 21:04:25 +01:00
										 |  |  |     await fillSyncRows("note_contents", "noteId"); | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |     await fillSyncRows("branches", "branchId"); | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |     await fillSyncRows("note_revisions", "noteRevisionId"); | 
					
						
							| 
									
										
										
										
											2019-11-01 20:00:56 +01:00
										 |  |  |     await fillSyncRows("note_revision_contents", "noteRevisionId"); | 
					
						
							| 
									
										
										
										
											2019-05-21 21:47:28 +02:00
										 |  |  |     await fillSyncRows("recent_notes", "noteId"); | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  |     await fillSyncRows("attributes", "attributeId"); | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  |     await fillSyncRows("api_tokens", "apiTokenId"); | 
					
						
							| 
									
										
										
										
											2018-07-24 20:35:03 +02:00
										 |  |  |     await fillSyncRows("options", "name", 'isSynced = 1'); | 
					
						
							| 
									
										
										
										
											2017-12-23 13:55:13 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01: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-01 20:00:56 +01:00
										 |  |  |     addNoteRevisionContentSync: async (noteRevisionId, sourceId) => await addEntitySync("note_revision_contents", noteRevisionId, sourceId), | 
					
						
							| 
									
										
										
										
											2020-03-09 21:28:41 +01:00
										 |  |  |     addOptionsSync: async (name, sourceId, isSynced) => await addEntitySync("options", name, sourceId, isSynced), | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |     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-30 20:12:19 -05:00
										 |  |  |     addEntitySync, | 
					
						
							| 
									
										
										
										
											2019-12-02 22:27:06 +01:00
										 |  |  |     fillAllSyncRows, | 
					
						
							| 
									
										
										
										
											2019-12-18 22:58:30 +01:00
										 |  |  |     addEntitySyncsForSector | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | }; |