Notes/src/services/content_hash.js

84 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-04-07 22:59:47 -04:00
"use strict";
2017-11-21 22:11:27 -05:00
const sql = require('./sql');
const utils = require('./utils');
const log = require('./log');
const ApiToken = require('../entities/api_token');
const Branch = require('../entities/branch');
const Note = require('../entities/note');
const Attribute = require('../entities/attribute');
const NoteRevision = require('../entities/note_revision');
const RecentNote = require('../entities/recent_note');
const Option = require('../entities/option');
2020-06-20 12:31:38 +02:00
function getSectorHashes(tableName, primaryKeyName, whereBranch) {
const hashes = sql.getRows(`SELECT ${primaryKeyName} AS id, hash FROM ${tableName} `
2019-12-24 16:00:31 +01:00
+ (whereBranch ? `WHERE ${whereBranch} ` : '')
+ ` ORDER BY ${primaryKeyName}`);
2018-06-02 09:39:04 -04:00
2019-12-24 16:00:31 +01:00
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]);
2017-11-21 22:11:27 -05:00
}
return map;
2017-11-21 22:11:27 -05:00
}
2020-06-20 12:31:38 +02:00
function getEntityHashes() {
const startTime = new Date();
const hashes = {
2020-06-20 12:31:38 +02:00
notes: getSectorHashes(Note.entityName, Note.primaryKeyName),
note_contents: getSectorHashes("note_contents", "noteId"),
branches: getSectorHashes(Branch.entityName, Branch.primaryKeyName),
note_revisions: getSectorHashes(NoteRevision.entityName, NoteRevision.primaryKeyName),
note_revision_contents: getSectorHashes("note_revision_contents", "noteRevisionId"),
recent_notes: getSectorHashes(RecentNote.entityName, RecentNote.primaryKeyName),
options: getSectorHashes(Option.entityName, Option.primaryKeyName, "isSynced = 1"),
attributes: getSectorHashes(Attribute.entityName, Attribute.primaryKeyName),
api_tokens: getSectorHashes(ApiToken.entityName, ApiToken.primaryKeyName),
};
const elapsedTimeMs = Date.now() - startTime.getTime();
log.info(`Content hash computation took ${elapsedTimeMs}ms`);
return hashes;
2017-11-21 22:11:27 -05:00
}
2020-06-20 12:31:38 +02:00
function checkContentHashes(otherHashes) {
const entityHashes = getEntityHashes();
const failedChecks = [];
2018-04-07 22:59:47 -04:00
for (const entityName in entityHashes) {
const thisSectorHashes = entityHashes[entityName];
const otherSectorHashes = otherHashes[entityName];
2018-04-07 22:59:47 -04:00
2019-12-24 16:00:31 +01:00
const sectors = new Set(Object.keys(thisSectorHashes).concat(Object.keys(otherSectorHashes)));
2019-11-19 19:07:14 +01:00
for (const sector of sectors) {
if (thisSectorHashes[sector] !== otherSectorHashes[sector]) {
log.info(`Content hash check for ${entityName} sector ${sector} FAILED. Local is ${thisSectorHashes[sector]}, remote is ${otherSectorHashes[sector]}`);
failedChecks.push({ entityName, sector });
2018-04-07 22:59:47 -04:00
}
}
}
if (failedChecks.length === 0) {
2018-04-07 22:59:47 -04:00
log.info("Content hash checks PASSED");
}
return failedChecks;
2018-04-07 22:59:47 -04:00
}
2017-11-21 22:11:27 -05:00
module.exports = {
getEntityHashes,
2018-04-07 22:59:47 -04:00
checkContentHashes
2020-06-20 12:31:38 +02:00
};