mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-11-04 07:01:31 +08:00 
			
		
		
		
	added changeId into entity_changes to have cross-sync change ID
This commit is contained in:
		
							parent
							
								
									15ac81627c
								
							
						
					
					
						commit
						e36bc42519
					
				
							
								
								
									
										23
									
								
								db/migrations/0187__add_changeId_to_entity_changes.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								db/migrations/0187__add_changeId_to_entity_changes.sql
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
			
		||||
CREATE TABLE IF NOT EXISTS "mig_entity_changes" (
 | 
			
		||||
                                                `id`	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
 | 
			
		||||
                                                `entityName`	TEXT NOT NULL,
 | 
			
		||||
                                                `entityId`	TEXT NOT NULL,
 | 
			
		||||
                                                `hash`	TEXT NOT NULL,
 | 
			
		||||
                                                `isErased` INT NOT NULL,
 | 
			
		||||
                                                `changeId` TEXT NOT NULL,
 | 
			
		||||
                                                `sourceId` TEXT NOT NULL,
 | 
			
		||||
                                                `isSynced` INTEGER NOT NULL,
 | 
			
		||||
                                                `utcDateChanged` TEXT NOT NULL
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
INSERT INTO mig_entity_changes (entityName, entityId, hash, isErased, changeId, sourceId, isSynced, utcDateChanged)
 | 
			
		||||
    SELECT entityName, entityId, hash, isErased, '', sourceId, isSynced, utcDateChanged FROM entity_changes;
 | 
			
		||||
 | 
			
		||||
DROP TABLE  entity_changes;
 | 
			
		||||
 | 
			
		||||
ALTER TABLE mig_entity_changes RENAME TO entity_changes;
 | 
			
		||||
 | 
			
		||||
CREATE UNIQUE INDEX `IDX_entityChanges_entityName_entityId` ON "entity_changes" (
 | 
			
		||||
                                                                                 `entityName`,
 | 
			
		||||
                                                                                 `entityId`
 | 
			
		||||
    );
 | 
			
		||||
@ -4,6 +4,7 @@ CREATE TABLE IF NOT EXISTS "entity_changes" (
 | 
			
		||||
                                                `entityId`	TEXT NOT NULL,
 | 
			
		||||
                                                `hash`	TEXT NOT NULL,
 | 
			
		||||
                                                `isErased` INT NOT NULL,
 | 
			
		||||
                                                `changeId` TEXT NOT NULL,
 | 
			
		||||
                                                `sourceId` TEXT NOT NULL,
 | 
			
		||||
                                                `isSynced` INTEGER NOT NULL,
 | 
			
		||||
                                                `utcDateChanged` TEXT NOT NULL
 | 
			
		||||
 | 
			
		||||
@ -4,7 +4,7 @@ const build = require('./build');
 | 
			
		||||
const packageJson = require('../../package');
 | 
			
		||||
const {TRILIUM_DATA_DIR} = require('./data_dir');
 | 
			
		||||
 | 
			
		||||
const APP_DB_VERSION = 186;
 | 
			
		||||
const APP_DB_VERSION = 187;
 | 
			
		||||
const SYNC_VERSION = 21;
 | 
			
		||||
const CLIPPER_PROTOCOL_VERSION = "1.0";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -44,14 +44,10 @@ function isEntityEventsDisabled() {
 | 
			
		||||
    return !!namespace.get('disableEntityEvents');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function clearEntityChangeIds() {
 | 
			
		||||
    namespace.set('entityChangeIds', []);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function getAndClearEntityChangeIds() {
 | 
			
		||||
    const entityChangeIds = namespace.get('entityChangeIds') || [];
 | 
			
		||||
 | 
			
		||||
    clearEntityChangeIds();
 | 
			
		||||
    namespace.set('entityChangeIds', []);
 | 
			
		||||
 | 
			
		||||
    return entityChangeIds;
 | 
			
		||||
}
 | 
			
		||||
@ -89,7 +85,6 @@ module.exports = {
 | 
			
		||||
    disableEntityEvents,
 | 
			
		||||
    isEntityEventsDisabled,
 | 
			
		||||
    reset,
 | 
			
		||||
    clearEntityChangeIds,
 | 
			
		||||
    getAndClearEntityChangeIds,
 | 
			
		||||
    addEntityChange,
 | 
			
		||||
    ignoreEntityChangeIds
 | 
			
		||||
 | 
			
		||||
@ -3,6 +3,7 @@ const sourceIdService = require('./source_id');
 | 
			
		||||
const dateUtils = require('./date_utils');
 | 
			
		||||
const log = require('./log');
 | 
			
		||||
const cls = require('./cls');
 | 
			
		||||
const utils = require('./utils');
 | 
			
		||||
const becca = require("../becca/becca");
 | 
			
		||||
 | 
			
		||||
let maxEntityChangeId = 0;
 | 
			
		||||
@ -12,6 +13,10 @@ function addEntityChange(origEntityChange) {
 | 
			
		||||
 | 
			
		||||
    delete ec.id;
 | 
			
		||||
 | 
			
		||||
    if (!ec.changeId) {
 | 
			
		||||
        ec.changeId = utils.randomString(12);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ec.sourceId = ec.sourceId || cls.getSourceId() || sourceIdService.getCurrentSourceId();
 | 
			
		||||
    ec.isSynced = ec.isSynced ? 1 : 0;
 | 
			
		||||
    ec.isErased = ec.isErased ? 1 : 0;
 | 
			
		||||
 | 
			
		||||
@ -149,10 +149,10 @@ async function pullChanges(syncContext) {
 | 
			
		||||
 | 
			
		||||
        sql.transactional(() => {
 | 
			
		||||
            for (const {entityChange, entity} of entityChanges) {
 | 
			
		||||
                // FIXME: temporary fix
 | 
			
		||||
                const existsAlready = !!sql.getValue("SELECT id FROM entity_changes WHERE entityName = ? AND entityId = ? AND utcDateChanged = ? AND hash = ?", [entityChange.entityName, entityChange.entityId, entityChange.utcDateChanged, entityChange.hash]);
 | 
			
		||||
                const changeAppliedAlready = !entityChange.changeId
 | 
			
		||||
                    || !!sql.getValue("SELECT id FROM entity_changes WHERE changeId = ?", [entityChange.changeId]);
 | 
			
		||||
 | 
			
		||||
                if (!existsAlready && !sourceIdService.isLocalSourceId(entityChange.sourceId)) {
 | 
			
		||||
                if (!changeAppliedAlready && !sourceIdService.isLocalSourceId(entityChange.sourceId)) {
 | 
			
		||||
                    if (!atLeastOnePullApplied) { // send only for first
 | 
			
		||||
                        ws.syncPullInProgress();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user