mirror of
https://github.com/zadam/trilium.git
synced 2025-11-01 10:55:55 +01:00
better transaction handling with rollback on exception
This commit is contained in:
@@ -34,41 +34,37 @@ async function pullSync(cookieJar, syncLog) {
|
||||
}
|
||||
|
||||
try {
|
||||
await sql.beginTransaction();
|
||||
await sql.doInTransaction(async () => {
|
||||
await putChanged(resp, syncLog);
|
||||
|
||||
await putChanged(resp, syncLog);
|
||||
for (const noteId of resp.notes) {
|
||||
let note;
|
||||
|
||||
for (const noteId of resp.notes) {
|
||||
let note;
|
||||
try {
|
||||
note = await rp({
|
||||
uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSyncedPull,
|
||||
headers: {
|
||||
auth: 'sync'
|
||||
},
|
||||
json: true,
|
||||
jar: cookieJar
|
||||
});
|
||||
}
|
||||
catch (e) {
|
||||
throw new Error("Can't pull note " + noteId + ", inner exception: " + e.stack);
|
||||
}
|
||||
|
||||
try {
|
||||
note = await rp({
|
||||
uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSyncedPull,
|
||||
headers: {
|
||||
auth: 'sync'
|
||||
},
|
||||
json: true,
|
||||
jar: cookieJar
|
||||
});
|
||||
}
|
||||
catch (e) {
|
||||
throw new Error("Can't pull note " + noteId + ", inner exception: " + e.stack);
|
||||
await putNote(note, syncLog);
|
||||
}
|
||||
|
||||
await putNote(note, syncLog);
|
||||
}
|
||||
if (resp.notes.length > 0) {
|
||||
await sql.addAudit(audit_category.SYNC);
|
||||
}
|
||||
|
||||
if (resp.notes.length > 0) {
|
||||
await sql.addAudit(audit_category.SYNC);
|
||||
}
|
||||
|
||||
await sql.setOption('last_synced_pull', resp.syncTimestamp);
|
||||
|
||||
await sql.commit();
|
||||
await sql.setOption('last_synced_pull', resp.syncTimestamp);
|
||||
});
|
||||
}
|
||||
catch (e) {
|
||||
await sql.rollback();
|
||||
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user