| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											2020-12-16 22:17:42 +01: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-08-02 23:27:48 +02:00
										 |  |  | let maxEntityChangeId = 0; | 
					
						
							| 
									
										
										
										
											2020-03-09 22:32:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-14 14:17:51 +01:00
										 |  |  | function insertEntityChange(entityName, entityId, hash, isErased, utcDateChanged, sourceId = null, isSynced = true) { | 
					
						
							| 
									
										
										
										
											2020-08-02 23:43:39 +02:00
										 |  |  |     const entityChange = { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         entityName: entityName, | 
					
						
							|  |  |  |         entityId: entityId, | 
					
						
							| 
									
										
										
										
											2020-12-07 23:04:17 +01:00
										 |  |  |         hash: hash, | 
					
						
							| 
									
										
										
										
											2020-03-09 20:56:43 +01:00
										 |  |  |         sourceId: sourceId || cls.getSourceId() || sourceIdService.getCurrentSourceId(), | 
					
						
							| 
									
										
										
										
											2020-12-14 14:17:51 +01:00
										 |  |  |         isSynced: isSynced ? 1 : 0, | 
					
						
							|  |  |  |         isErased: isErased ? 1 : 0, | 
					
						
							|  |  |  |         utcDateChanged: utcDateChanged | 
					
						
							| 
									
										
										
										
											2019-12-02 22:27:06 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:43:39 +02:00
										 |  |  |     entityChange.id = sql.replace("entity_changes", entityChange); | 
					
						
							| 
									
										
										
										
											2019-12-02 22:27:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:43:39 +02:00
										 |  |  |     maxEntityChangeId = Math.max(maxEntityChangeId, entityChange.id); | 
					
						
							| 
									
										
										
										
											2020-03-09 22:32:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:43:39 +02:00
										 |  |  |     return entityChange; | 
					
						
							| 
									
										
										
										
											2019-12-18 22:58:30 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-14 13:15:32 +01:00
										 |  |  | function addEntityChange(entityChange, sourceId, isSynced) { | 
					
						
							| 
									
										
										
										
											2020-12-14 14:17:51 +01:00
										 |  |  |     const localEntityChange = insertEntityChange(entityChange.entityName, entityChange.entityId, entityChange.hash, entityChange.isErased, entityChange.utcDateChanged, sourceId, isSynced); | 
					
						
							| 
									
										
										
										
											2019-12-18 22:58:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-14 14:17:51 +01:00
										 |  |  |     cls.addEntityChange(localEntityChange); | 
					
						
							| 
									
										
										
										
											2019-12-02 22:27:06 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-06 23:53:10 +01:00
										 |  |  | function addNoteReorderingEntityChange(parentNoteId, sourceId) { | 
					
						
							|  |  |  |     addEntityChange({ | 
					
						
							|  |  |  |         entityName: "note_reordering", | 
					
						
							|  |  |  |         entityId: parentNoteId, | 
					
						
							|  |  |  |         hash: 'N/A', | 
					
						
							|  |  |  |         isErased: false, | 
					
						
							|  |  |  |         utcDateChanged: dateUtils.utcNowDateTime() | 
					
						
							|  |  |  |     }, sourceId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const eventService = require('./events'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     eventService.emit(eventService.ENTITY_CHANGED, { | 
					
						
							|  |  |  |         entityName: 'note_reordering', | 
					
						
							|  |  |  |         entity: sql.getMap(`SELECT branchId, notePosition FROM branches WHERE isDeleted = 0 AND parentNoteId = ?`, [parentNoteId]) | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-07 23:04:17 +01:00
										 |  |  | function moveEntityChangeToTop(entityName, entityId) { | 
					
						
							| 
									
										
										
										
											2020-12-10 22:53:52 +01:00
										 |  |  |     const [hash, isSynced] = sql.getRow(`SELECT * FROM entity_changes WHERE entityName = ? AND entityId = ?`, [entityName, entityId]); | 
					
						
							| 
									
										
										
										
											2020-12-07 23:04:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 22:53:52 +01:00
										 |  |  |     addEntityChange(entityName, entityId, hash, null, isSynced); | 
					
						
							| 
									
										
										
										
											2020-12-07 23:04:17 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  | function addEntityChangesForSector(entityName, entityPrimaryKey, sector) { | 
					
						
							| 
									
										
										
										
											2020-04-04 21:49:57 +02:00
										 |  |  |     const startTime = Date.now(); | 
					
						
							| 
									
										
										
										
											2021-01-14 23:22:13 +01:00
										 |  |  |     const repository = require('./repository'); | 
					
						
							| 
									
										
										
										
											2019-12-18 22:58:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     sql.transactional(() => { | 
					
						
							|  |  |  |         const entityIds = sql.getColumn(`SELECT ${entityPrimaryKey} FROM ${entityName} WHERE SUBSTR(${entityPrimaryKey}, 1, 1) = ?`, [sector]); | 
					
						
							| 
									
										
										
										
											2020-03-09 21:28:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 21:49:57 +02:00
										 |  |  |         for (const entityId of entityIds) { | 
					
						
							| 
									
										
										
										
											2020-12-07 23:04:17 +01:00
										 |  |  |             // retrieving entity one by one to avoid memory issues with note_contents
 | 
					
						
							|  |  |  |             const entity = repository.getEntity(`SELECT * FROM ${entityName} WHERE ${entityPrimaryKey} = ?`, [entityId]); | 
					
						
							| 
									
										
										
										
											2020-04-04 21:49:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-07 23:04:17 +01:00
										 |  |  |             if (entityName === 'options' && !entity.isSynced) { | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2020-03-09 21:28:41 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-04-04 21:49:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-14 14:17:51 +01:00
										 |  |  |             insertEntityChange(entityName, entityId, entity.generateHash(), false, entity.getUtcDateChanged(), 'content-check', true); | 
					
						
							| 
									
										
										
										
											2020-03-09 21:28:41 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-04-04 21:49:57 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-09 21:28:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-04 21:49:57 +02:00
										 |  |  |     log.info(`Added sector ${sector} of ${entityName} to sync queue in ${Date.now() - startTime}ms.`); | 
					
						
							| 
									
										
										
										
											2019-12-18 22:58:30 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-14 14:17:51 +01:00
										 |  |  | function cleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     sql.execute(`
 | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |       DELETE  | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |       FROM entity_changes  | 
					
						
							| 
									
										
										
										
											2020-12-14 13:15:32 +01:00
										 |  |  |       WHERE | 
					
						
							|  |  |  |         isErased = 0 | 
					
						
							| 
									
										
										
										
											2020-12-16 22:17:42 +01:00
										 |  |  |         AND entityName = '${entityName}'  | 
					
						
							|  |  |  |         AND entityId NOT IN (SELECT ${entityPrimaryKey} FROM ${entityName})`);
 | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  | function fillEntityChanges(entityName, entityPrimaryKey, condition = '') { | 
					
						
							| 
									
										
										
										
											2019-06-05 22:07:12 +02:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2020-12-14 14:17:51 +01:00
										 |  |  |         cleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey); | 
					
						
							| 
									
										
										
										
											2019-06-05 22:07:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         const entityIds = 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) { | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |             const existingRows = sql.getValue("SELECT COUNT(1) FROM entity_changes 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 22:53:52 +01:00
										 |  |  |                 sql.insert("entity_changes", { | 
					
						
							| 
									
										
										
										
											2019-06-05 22:07:12 +02:00
										 |  |  |                     entityName: entityName, | 
					
						
							|  |  |  |                     entityId: entityId, | 
					
						
							|  |  |  |                     sourceId: "SYNC_FILL", | 
					
						
							| 
									
										
										
										
											2020-08-02 22:53:57 +02:00
										 |  |  |                     isSynced: 1 | 
					
						
							| 
									
										
										
										
											2019-06-05 22:07:12 +02:00
										 |  |  |                 }); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-02 23:22:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (createdCount > 0) { | 
					
						
							| 
									
										
										
										
											2020-08-02 23:43:39 +02:00
										 |  |  |             log.info(`Created ${createdCount} missing entity changes for ${entityName}.`); | 
					
						
							| 
									
										
										
										
											2019-10-02 23:22:58 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:43:39 +02:00
										 |  |  |         log.error(`Filling entity changes 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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  | function fillAllEntityChanges() { | 
					
						
							| 
									
										
										
										
											2020-07-02 21:08:18 +02:00
										 |  |  |     sql.transactional(() => { | 
					
						
							| 
									
										
										
										
											2020-12-14 13:15:32 +01:00
										 |  |  |         sql.execute("DELETE FROM entity_changes WHERE isErased = 0"); | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         fillEntityChanges("notes", "noteId"); | 
					
						
							|  |  |  |         fillEntityChanges("note_contents", "noteId"); | 
					
						
							|  |  |  |         fillEntityChanges("branches", "branchId"); | 
					
						
							|  |  |  |         fillEntityChanges("note_revisions", "noteRevisionId"); | 
					
						
							|  |  |  |         fillEntityChanges("note_revision_contents", "noteRevisionId"); | 
					
						
							|  |  |  |         fillEntityChanges("recent_notes", "noteId"); | 
					
						
							|  |  |  |         fillEntityChanges("attributes", "attributeId"); | 
					
						
							|  |  |  |         fillEntityChanges("api_tokens", "apiTokenId"); | 
					
						
							|  |  |  |         fillEntityChanges("options", "name", 'isSynced = 1'); | 
					
						
							| 
									
										
										
										
											2020-07-02 21:08:18 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-12-23 13:55:13 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2021-03-06 23:53:10 +01:00
										 |  |  |     addNoteReorderingEntityChange, | 
					
						
							| 
									
										
										
										
											2020-12-07 23:04:17 +01:00
										 |  |  |     moveEntityChangeToTop, | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |     addEntityChange, | 
					
						
							|  |  |  |     fillAllEntityChanges, | 
					
						
							|  |  |  |     addEntityChangesForSector, | 
					
						
							|  |  |  |     getMaxEntityChangeId: () => maxEntityChangeId | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | }; |