| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  | async function addNoteSync(noteId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("notes", noteId, sourceId) | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-26 22:24:04 +01:00
										 |  |  | async function addNoteContentSync(noteId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("note_contents", noteId, sourceId) | 
					
						
							| 
									
										
										
										
											2019-02-15 00:15:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  | async function addBranchSync(branchId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("branches", branchId, sourceId) | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 21:57:46 -05:00
										 |  |  | async function addNoteReorderingSync(parentNoteId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("note_reordering", parentNoteId, sourceId) | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  | async function addNoteRevisionSync(noteRevisionId, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |     await addEntitySync("note_revisions", noteRevisionId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  | async function addOptionsSync(name, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("options", name, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-21 21:47:28 +02:00
										 |  |  | async function addRecentNoteSync(noteId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("recent_notes", noteId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | async function addAttributeSync(attributeId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("attributes", attributeId, sourceId); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  | async function addApiTokenSync(apiTokenId, sourceId) { | 
					
						
							|  |  |  |     await addEntitySync("api_tokens", apiTokenId, sourceId); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  | async function addEntitySync(entityName, entityId, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-12-14 07:57:31 -05:00
										 |  |  |     await sql.replace("sync", { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         entityName: entityName, | 
					
						
							|  |  |  |         entityId: entityId, | 
					
						
							| 
									
										
										
										
											2019-03-13 22:43:59 +01:00
										 |  |  |         utcSyncDate: dateUtils.utcNowDateTime(), | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |         sourceId: sourceId || cls.getSourceId() || sourceIdService.getCurrentSourceId() | 
					
						
							| 
									
										
										
										
											2017-12-14 07:57:31 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | async function cleanupSyncRowsForMissingEntities(entityName, entityKey) { | 
					
						
							|  |  |  |     await sql.execute(`
 | 
					
						
							|  |  |  |       DELETE  | 
					
						
							|  |  |  |       FROM sync  | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |       WHERE sync.entityName = '${entityName}'  | 
					
						
							|  |  |  |         AND sync.entityId NOT IN (SELECT ${entityKey} FROM ${entityName})`);
 | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 20:35:03 +02:00
										 |  |  | async function fillSyncRows(entityName, entityKey, condition = '') { | 
					
						
							| 
									
										
										
										
											2019-06-05 22:07:12 +02:00
										 |  |  |     try { | 
					
						
							|  |  |  |         await cleanupSyncRowsForMissingEntities(entityName, entityKey); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const entityIds = await sql.getColumn(`SELECT ${entityKey} FROM ${entityName}` | 
					
						
							|  |  |  |             + (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) { | 
					
						
							|  |  |  |             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-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
 | 
					
						
							|  |  |  |         log.error(`Filling sync rows failed for ${entityName} ${entityKey} with error "${e.message}", continuing`); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											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-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 = { | 
					
						
							|  |  |  |     addNoteSync, | 
					
						
							| 
									
										
										
										
											2019-02-15 00:15:09 +01:00
										 |  |  |     addNoteContentSync, | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |     addBranchSync, | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  |     addNoteReorderingSync, | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |     addNoteRevisionSync, | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  |     addOptionsSync, | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |     addRecentNoteSync, | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  |     addAttributeSync, | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  |     addApiTokenSync, | 
					
						
							| 
									
										
										
										
											2018-01-30 20:12:19 -05:00
										 |  |  |     addEntitySync, | 
					
						
							| 
									
										
										
										
											2017-12-23 13:55:13 -05:00
										 |  |  |     fillAllSyncRows | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | }; |