sync recovery fixes

This commit is contained in:
zadam 2019-12-24 16:00:31 +01:00
parent a155b6e8d5
commit 474baa7d95
3 changed files with 18 additions and 14 deletions

View File

@ -12,11 +12,15 @@ const RecentNote = require('../entities/recent_note');
const Option = require('../entities/option');
async function getSectorHashes(tableName, primaryKeyName, whereBranch) {
// subselect is necessary to have correct ordering in GROUP_CONCAT
const query = `SELECT SUBSTR(${primaryKeyName}, 1, 1), GROUP_CONCAT(hash) FROM ${tableName} `
+ (whereBranch ? `WHERE ${whereBranch} ` : '') + `GROUP BY SUBSTR(${primaryKeyName}, 1, 1) ORDER BY ${primaryKeyName}`;
const hashes = await sql.getRows(`SELECT ${primaryKeyName} AS id, hash FROM ${tableName} `
+ (whereBranch ? `WHERE ${whereBranch} ` : '')
+ ` ORDER BY ${primaryKeyName}`);
const map = await sql.getMap(query);
const map = {};
for (const {id, hash} of hashes) {
map[id[0]] = (map[id[0]] || "") + hash;
}
for (const key in map) {
map[key] = utils.hash(map[key]);
@ -55,7 +59,7 @@ async function checkContentHashes(otherHashes) {
const thisSectorHashes = entityHashes[entityName];
const otherSectorHashes = otherHashes[entityName];
const sectors = new Set(Object.keys(entityHashes).concat(Object.keys(otherHashes)));
const sectors = new Set(Object.keys(thisSectorHashes).concat(Object.keys(otherSectorHashes)));
for (const sector of sectors) {
if (thisSectorHashes[sector] !== otherSectorHashes[sector]) {

View File

@ -53,7 +53,7 @@ async function updateEntity(sync, entity, sourceId) {
async function updateNote(entity, sourceId) {
const origNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]);
if (!origNote || origNote.utcDateModified < entity.utcDateModified) {
if (!origNote || origNote.utcDateModified < entity.utcDateModified || origNote.hash !== entity.hash) {
await sql.transactional(async () => {
await sql.replace("notes", entity);
@ -67,7 +67,7 @@ async function updateNote(entity, sourceId) {
async function updateNoteContent(entity, sourceId) {
const origNoteContent = await sql.getRow("SELECT * FROM note_contents WHERE noteId = ?", [entity.noteId]);
if (!origNoteContent || origNoteContent.utcDateModified < entity.utcDateModified) {
if (!origNoteContent || origNoteContent.utcDateModified < entity.utcDateModified || origNoteContent.hash !== entity.hash) {
entity.content = entity.content === null ? null : Buffer.from(entity.content, 'base64');
await sql.transactional(async () => {
@ -84,7 +84,7 @@ async function updateBranch(entity, sourceId) {
const orig = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [entity.branchId]);
await sql.transactional(async () => {
if (orig === null || orig.utcDateModified < entity.utcDateModified) {
if (orig === null || orig.utcDateModified < entity.utcDateModified || orig.hash !== entity.hash) {
// isExpanded is not synced unless it's a new branch instance
// otherwise in case of full new sync we'll get all branches (even root) collapsed.
if (orig) {
@ -104,7 +104,7 @@ async function updateNoteRevision(entity, sourceId) {
const orig = await sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [entity.noteRevisionId]);
await sql.transactional(async () => {
if (orig === null || orig.utcDateModified < entity.utcDateModified) {
if (orig === null || orig.utcDateModified < entity.utcDateModified || orig.hash !== entity.hash) {
await sql.replace('note_revisions', entity);
await syncTableService.addNoteRevisionSync(entity.noteRevisionId, sourceId);
@ -118,7 +118,7 @@ async function updateNoteRevisionContent(entity, sourceId) {
const orig = await sql.getRowOrNull("SELECT * FROM note_revision_contents WHERE noteRevisionId = ?", [entity.noteRevisionId]);
await sql.transactional(async () => {
if (orig === null || orig.utcDateModified < entity.utcDateModified) {
if (orig === null || orig.utcDateModified < entity.utcDateModified || orig.hash !== entity.hash) {
entity.content = entity.content === null ? null : Buffer.from(entity.content, 'base64');
await sql.replace('note_revision_contents', entity);
@ -148,7 +148,7 @@ async function updateOptions(entity, sourceId) {
}
await sql.transactional(async () => {
if (orig === null || orig.utcDateModified < entity.utcDateModified) {
if (orig === null || orig.utcDateModified < entity.utcDateModified || orig.hash !== entity.hash) {
await sql.replace('options', entity);
await syncTableService.addOptionsSync(entity.name, sourceId);
@ -159,7 +159,7 @@ async function updateOptions(entity, sourceId) {
async function updateRecentNotes(entity, sourceId) {
const orig = await sql.getRowOrNull("SELECT * FROM recent_notes WHERE noteId = ?", [entity.noteId]);
if (orig === null || orig.utcDateCreated < entity.utcDateCreated) {
if (orig === null || orig.utcDateCreated < entity.utcDateCreated || orig.hash !== entity.hash) {
await sql.transactional(async () => {
await sql.replace('recent_notes', entity);
@ -171,7 +171,7 @@ async function updateRecentNotes(entity, sourceId) {
async function updateAttribute(entity, sourceId) {
const origAttribute = await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [entity.attributeId]);
if (!origAttribute || origAttribute.utcDateModified <= entity.utcDateModified) {
if (!origAttribute || origAttribute.utcDateModified <= entity.utcDateModified || origAttribute.hash !== entity.hash) {
await sql.transactional(async () => {
await sql.replace("attributes", entity);

View File

@ -54,7 +54,7 @@ function sanitizeSql(str) {
}
function sanitizeSqlIdentifier(str) {
return str.replace(/[A-Za-z0-9_]/g, "");
return str.replace(/[^A-Za-z0-9_]/g, "");
}
function prepareSqlForLike(prefix, str, suffix) {