mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 05:28:59 +01:00 
			
		
		
		
	small refactors in sync
This commit is contained in:
		
							parent
							
								
									701911b766
								
							
						
					
					
						commit
						4b779d7512
					
				@ -8,7 +8,7 @@ const becca = require("../becca/becca");
 | 
			
		||||
 | 
			
		||||
let maxEntityChangeId = 0;
 | 
			
		||||
 | 
			
		||||
function addEntityChangeWithinstanceId(origEntityChange, instanceId) {
 | 
			
		||||
function addEntityChangeWithInstanceId(origEntityChange, instanceId) {
 | 
			
		||||
    const ec = {...origEntityChange, instanceId};
 | 
			
		||||
 | 
			
		||||
    return addEntityChange(ec);
 | 
			
		||||
@ -85,45 +85,38 @@ function cleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function fillEntityChanges(entityName, entityPrimaryKey, condition = '') {
 | 
			
		||||
    try {
 | 
			
		||||
        cleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey);
 | 
			
		||||
    cleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey);
 | 
			
		||||
 | 
			
		||||
        sql.transactional(() => {
 | 
			
		||||
            const entityIds = sql.getColumn(`SELECT ${entityPrimaryKey} FROM ${entityName}`
 | 
			
		||||
                + (condition ? ` WHERE ${condition}` : ''));
 | 
			
		||||
    sql.transactional(() => {
 | 
			
		||||
        const entityIds = sql.getColumn(`SELECT ${entityPrimaryKey} FROM ${entityName}`
 | 
			
		||||
            + (condition ? ` WHERE ${condition}` : ''));
 | 
			
		||||
 | 
			
		||||
            let createdCount = 0;
 | 
			
		||||
        let createdCount = 0;
 | 
			
		||||
 | 
			
		||||
            for (const entityId of entityIds) {
 | 
			
		||||
                const existingRows = sql.getValue("SELECT COUNT(1) FROM entity_changes WHERE entityName = ? AND entityId = ?", [entityName, entityId]);
 | 
			
		||||
        for (const entityId of entityIds) {
 | 
			
		||||
            const existingRows = sql.getValue("SELECT COUNT(1) FROM entity_changes WHERE entityName = ? AND entityId = ?", [entityName, entityId]);
 | 
			
		||||
 | 
			
		||||
                // we don't want to replace existing entities (which would effectively cause full resync)
 | 
			
		||||
                if (existingRows === 0) {
 | 
			
		||||
                    createdCount++;
 | 
			
		||||
            // we don't want to replace existing entities (which would effectively cause full resync)
 | 
			
		||||
            if (existingRows === 0) {
 | 
			
		||||
                createdCount++;
 | 
			
		||||
 | 
			
		||||
                    const entity = becca.getEntity(entityName, entityId);
 | 
			
		||||
                const entity = becca.getEntity(entityName, entityId);
 | 
			
		||||
 | 
			
		||||
                    addEntityChange({
 | 
			
		||||
                        entityName,
 | 
			
		||||
                        entityId,
 | 
			
		||||
                        hash: entity.generateHash(),
 | 
			
		||||
                        isErased: false,
 | 
			
		||||
                        utcDateChanged: entity.getUtcDateChanged(),
 | 
			
		||||
                        isSynced: entityName !== 'options' || !!entity.isSynced
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
                addEntityChange({
 | 
			
		||||
                    entityName,
 | 
			
		||||
                    entityId,
 | 
			
		||||
                    hash: entity.generateHash(),
 | 
			
		||||
                    isErased: false,
 | 
			
		||||
                    utcDateChanged: entity.getUtcDateChanged(),
 | 
			
		||||
                    isSynced: entityName !== 'options' || !!entity.isSynced
 | 
			
		||||
                });
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
            if (createdCount > 0) {
 | 
			
		||||
                log.info(`Created ${createdCount} missing entity changes for ${entityName}.`);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
    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 entity changes failed for ${entityName} ${entityPrimaryKey} with error "${e.message}", continuing`);
 | 
			
		||||
    }
 | 
			
		||||
        if (createdCount > 0) {
 | 
			
		||||
            log.info(`Created ${createdCount} missing entity changes for ${entityName}.`);
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function fillAllEntityChanges() {
 | 
			
		||||
@ -145,7 +138,7 @@ module.exports = {
 | 
			
		||||
    addNoteReorderingEntityChange,
 | 
			
		||||
    moveEntityChangeToTop,
 | 
			
		||||
    addEntityChange,
 | 
			
		||||
    addEntityChangeWithinstanceId,
 | 
			
		||||
    addEntityChangeWithInstanceId,
 | 
			
		||||
    fillAllEntityChanges,
 | 
			
		||||
    addEntityChangesForSector,
 | 
			
		||||
    getMaxEntityChangeId: () => maxEntityChangeId
 | 
			
		||||
 | 
			
		||||
@ -54,7 +54,7 @@ function updateNormalEntity(remoteEntityChange, entity, instanceId) {
 | 
			
		||||
 | 
			
		||||
            sql.execute(`DELETE FROM ${remoteEntityChange.entityName} WHERE ${primaryKey} = ?`, remoteEntityChange.entityId);
 | 
			
		||||
 | 
			
		||||
            entityChangesService.addEntityChangeWithinstanceId(remoteEntityChange, instanceId);
 | 
			
		||||
            entityChangesService.addEntityChangeWithInstanceId(remoteEntityChange, instanceId);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
@ -71,7 +71,7 @@ function updateNormalEntity(remoteEntityChange, entity, instanceId) {
 | 
			
		||||
        sql.transactional(() => {
 | 
			
		||||
            sql.replace(remoteEntityChange.entityName, entity);
 | 
			
		||||
 | 
			
		||||
            entityChangesService.addEntityChangeWithinstanceId(remoteEntityChange, instanceId);
 | 
			
		||||
            entityChangesService.addEntityChangeWithInstanceId(remoteEntityChange, instanceId);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
@ -86,7 +86,7 @@ function updateNoteReordering(entityChange, entity, instanceId) {
 | 
			
		||||
            sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [entity[key], key]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        entityChangesService.addEntityChangeWithinstanceId(entityChange, instanceId);
 | 
			
		||||
        entityChangesService.addEntityChangeWithInstanceId(entityChange, instanceId);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    return true;
 | 
			
		||||
@ -119,7 +119,7 @@ function eraseEntity(entityChange, instanceId) {
 | 
			
		||||
 | 
			
		||||
    eventService.emit(eventService.ENTITY_DELETE_SYNCED, { entityName, entityId });
 | 
			
		||||
 | 
			
		||||
    entityChangesService.addEntityChangeWithinstanceId(entityChange, instanceId);
 | 
			
		||||
    entityChangesService.addEntityChangeWithInstanceId(entityChange, instanceId);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user