diff --git a/db/schema.sql b/db/schema.sql index 8446f6c0d..8c766571f 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -1,9 +1,3 @@ -CREATE TABLE IF NOT EXISTS "sync" ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - `entityName` TEXT NOT NULL, - `entityId` TEXT NOT NULL, - `sourceId` TEXT NOT NULL, - `utcSyncDate` TEXT NOT NULL); CREATE TABLE IF NOT EXISTS "source_ids" ( `sourceId` TEXT NOT NULL, `utcDateCreated` TEXT NOT NULL, @@ -27,13 +21,6 @@ CREATE TABLE IF NOT EXISTS "options" utcDateCreated TEXT not null, utcDateModified TEXT NOT NULL ); -CREATE UNIQUE INDEX `IDX_sync_entityName_entityId` ON `sync` ( - `entityName`, - `entityId` - ); -CREATE INDEX `IDX_sync_utcSyncDate` ON `sync` ( - `utcSyncDate` - ); CREATE TABLE IF NOT EXISTS "note_contents" ( `noteId` TEXT NOT NULL, `content` TEXT NULL DEFAULT NULL, @@ -130,3 +117,17 @@ CREATE INDEX IDX_attributes_noteId_index on attributes (noteId); CREATE INDEX IDX_attributes_value_index on attributes (value); +CREATE TABLE IF NOT EXISTS "sync" ( + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + `entityName` TEXT NOT NULL, + `entityId` TEXT NOT NULL, + `sourceId` TEXT NOT NULL, + `isSynced` INTEGER default 0 not null, + `utcSyncDate` TEXT NOT NULL); +CREATE UNIQUE INDEX `IDX_sync_entityName_entityId` ON `sync` ( + `entityName`, + `entityId` + ); +CREATE INDEX `IDX_sync_utcSyncDate` ON `sync` ( + `utcSyncDate` + ); diff --git a/src/routes/api/login.js b/src/routes/api/login.js index e3ed0c5e5..23d78807a 100644 --- a/src/routes/api/login.js +++ b/src/routes/api/login.js @@ -49,7 +49,7 @@ async function loginSync(req) { return { sourceId: sourceIdService.getCurrentSourceId(), - maxSyncId: await sql.getValue("SELECT MAX(id) FROM sync") + maxSyncId: await sql.getValue("SELECT MAX(id) FROM sync WHERE isSynced = 1") }; } diff --git a/src/routes/api/sync.js b/src/routes/api/sync.js index 08fc9cfa5..ddd2b4148 100644 --- a/src/routes/api/sync.js +++ b/src/routes/api/sync.js @@ -50,7 +50,7 @@ async function getStats() { async function checkSync() { return { entityHashes: await contentHashService.getEntityHashes(), - maxSyncId: await sql.getValue('SELECT MAX(id) FROM sync') + maxSyncId: await sql.getValue('SELECT MAX(id) FROM sync WHERE isSynced = 1') }; } @@ -116,11 +116,11 @@ async function forceNoteSync(req) { async function getChanged(req) { const lastSyncId = parseInt(req.query.lastSyncId); - const syncs = await sql.getRows("SELECT * FROM sync WHERE id > ? LIMIT 1000", [lastSyncId]); + const syncs = await sql.getRows("SELECT * FROM sync WHERE isSynced = 1 AND id > ? LIMIT 1000", [lastSyncId]); return { syncs: await syncService.getSyncRecords(syncs), - maxSyncId: await sql.getValue('SELECT MAX(id) FROM sync') + maxSyncId: await sql.getValue('SELECT MAX(id) FROM sync WHERE isSynced = 1') }; } diff --git a/src/services/sync.js b/src/services/sync.js index 67994d68a..f68af1cd3 100644 --- a/src/services/sync.js +++ b/src/services/sync.js @@ -176,7 +176,7 @@ async function pushSync(syncContext) { let lastSyncedPush = await getLastSyncedPush(); while (true) { - const syncs = await sql.getRows('SELECT * FROM sync WHERE id > ? LIMIT 1000', [lastSyncedPush]); + const syncs = await sql.getRows('SELECT * FROM sync WHERE isSynced = 1 AND id > ? LIMIT 1000', [lastSyncedPush]); if (syncs.length === 0) { log.info("Nothing to push"); @@ -236,7 +236,7 @@ async function checkContentHash(syncContext) { return true; } - const notPushedSyncs = await sql.getValue("SELECT EXISTS(SELECT 1 FROM sync WHERE id > ?)", [await getLastSyncedPush()]); + const notPushedSyncs = await sql.getValue("SELECT EXISTS(SELECT 1 FROM sync WHERE isSynced = 1 AND id > ?)", [await getLastSyncedPush()]); if (notPushedSyncs) { log.info(`There's ${notPushedSyncs} outstanding pushes, skipping content check.`); @@ -358,7 +358,7 @@ async function updatePushStats() { if (await syncOptions.isSyncSetup()) { const lastSyncedPush = await optionService.getOption('lastSyncedPush'); - stats.outstandingPushes = await sql.getValue("SELECT COUNT(1) FROM sync WHERE id > ?", [lastSyncedPush]); + stats.outstandingPushes = await sql.getValue("SELECT COUNT(1) FROM sync WHERE isSynced = 1 AND id > ?", [lastSyncedPush]); } }