mirror of
https://github.com/TriliumNext/Notes.git
synced 2026-03-12 01:18:41 -05:00
better transaction handling with rollback on exception
This commit is contained in:
@@ -4,6 +4,7 @@ module.exports = {
|
||||
UPDATE_CONTENT: 'CONTENT',
|
||||
UPDATE_TITLE: 'TITLE',
|
||||
CHANGE_POSITION: 'POSITION',
|
||||
CHANGE_EXPANDED: 'EXPANDED',
|
||||
CREATE_NOTE: 'CREATE',
|
||||
DELETE_NOTE: 'DELETE',
|
||||
CHANGE_PARENT: 'PARENT',
|
||||
|
||||
@@ -55,15 +55,13 @@ async function changePassword(currentPassword, newPassword, req = null) {
|
||||
|
||||
const newEncryptedDataKey = encrypt(decryptedDataKey);
|
||||
|
||||
await sql.beginTransaction();
|
||||
await sql.doInTransaction(async () => {
|
||||
await sql.setOption('encrypted_data_key', newEncryptedDataKey);
|
||||
|
||||
await sql.setOption('encrypted_data_key', newEncryptedDataKey);
|
||||
await sql.setOption('password_verification_hash', newPasswordVerificationKey);
|
||||
|
||||
await sql.setOption('password_verification_hash', newPasswordVerificationKey);
|
||||
|
||||
await sql.addAudit(audit_category.CHANGE_PASSWORD, req);
|
||||
|
||||
await sql.commit();
|
||||
await sql.addAudit(audit_category.CHANGE_PASSWORD, req);
|
||||
});
|
||||
|
||||
return {
|
||||
'success': true,
|
||||
|
||||
@@ -42,13 +42,11 @@ async function migrate() {
|
||||
try {
|
||||
log.info("Attempting migration to version " + mig.dbVersion + " with script: " + migrationSql);
|
||||
|
||||
await sql.beginTransaction();
|
||||
await sql.doInTransaction(async () => {
|
||||
await sql.executeScript(migrationSql);
|
||||
|
||||
await sql.executeScript(migrationSql);
|
||||
|
||||
await sql.setOption("db_version", mig.dbVersion);
|
||||
|
||||
await sql.commit();
|
||||
await sql.setOption("db_version", mig.dbVersion);
|
||||
});
|
||||
|
||||
log.info("Migration to version " + mig.dbVersion + " has been successful.");
|
||||
|
||||
|
||||
@@ -109,6 +109,21 @@ async function deleteRecentAudits(category, req, noteId) {
|
||||
[category, browserId, noteId, deleteCutoff])
|
||||
}
|
||||
|
||||
async function doInTransaction(func) {
|
||||
try {
|
||||
await beginTransaction();
|
||||
|
||||
await func();
|
||||
|
||||
await commit();
|
||||
}
|
||||
catch (e) {
|
||||
log.error("Error executing transaction, executing rollback. Inner exception: " + e.stack);
|
||||
|
||||
await rollback();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
insert,
|
||||
getSingleResult,
|
||||
@@ -118,10 +133,8 @@ module.exports = {
|
||||
executeScript,
|
||||
getOption,
|
||||
setOption,
|
||||
beginTransaction,
|
||||
commit,
|
||||
rollback,
|
||||
addAudit,
|
||||
deleteRecentAudits,
|
||||
remove
|
||||
remove,
|
||||
doInTransaction
|
||||
};
|
||||
@@ -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