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' ) ;
2018-01-01 19:47:50 -05:00
const log = require ( './log' ) ;
2018-05-22 00:15:54 -04:00
const ApiToken = require ( '../entities/api_token' ) ;
const Branch = require ( '../entities/branch' ) ;
const Note = require ( '../entities/note' ) ;
2018-08-07 13:44:51 +02:00
const Attribute = require ( '../entities/attribute' ) ;
2018-05-22 00:15:54 -04:00
const NoteRevision = require ( '../entities/note_revision' ) ;
const RecentNote = require ( '../entities/recent_note' ) ;
const Option = require ( '../entities/option' ) ;
2019-12-18 22:24:13 +01:00
async function getSectorHashes ( tableName , primaryKeyName , whereBranch ) {
2018-06-02 09:39:04 -04:00
// subselect is necessary to have correct ordering in GROUP_CONCAT
2019-12-18 22:24:13 +01:00
const query = ` SELECT SUBSTR( ${ primaryKeyName } , 1, 1), GROUP_CONCAT(hash) FROM ${ tableName } `
+ ( whereBranch ? ` WHERE ${ whereBranch } ` : '' ) + ` GROUP BY SUBSTR( ${ primaryKeyName } , 1, 1) ORDER BY ${ primaryKeyName } ` ;
2018-06-02 09:39:04 -04:00
2019-12-18 22:24:13 +01:00
const map = await sql . getMap ( query ) ;
2018-05-22 00:15:54 -04:00
2019-12-18 22:24:13 +01:00
for ( const key in map ) {
map [ key ] = utils . hash ( map [ key ] ) ;
2017-11-21 22:11:27 -05:00
}
2019-12-18 22:24:13 +01:00
return map ;
2017-11-21 22:11:27 -05:00
}
2019-12-18 22:24:13 +01:00
async function getEntityHashes ( ) {
2018-01-01 19:47:50 -05:00
const startTime = new Date ( ) ;
const hashes = {
2019-12-18 22:24:13 +01:00
notes : await getSectorHashes ( Note . entityName , Note . primaryKeyName ) ,
note _contents : await getSectorHashes ( "note_contents" , "noteId" ) ,
branches : await getSectorHashes ( Branch . entityName , Branch . primaryKeyName ) ,
note _revisions : await getSectorHashes ( NoteRevision . entityName , NoteRevision . primaryKeyName ) ,
note _revision _contents : await getSectorHashes ( "note_revision_contents" , "noteRevisionId" ) ,
recent _notes : await getSectorHashes ( RecentNote . entityName , RecentNote . primaryKeyName ) ,
options : await getSectorHashes ( Option . entityName , Option . primaryKeyName , "isSynced = 1" ) ,
attributes : await getSectorHashes ( Attribute . entityName , Attribute . primaryKeyName ) ,
api _tokens : await getSectorHashes ( ApiToken . entityName , ApiToken . primaryKeyName ) ,
2017-12-15 21:14:10 -05:00
} ;
2018-01-01 19:47:50 -05:00
2019-12-18 22:24:13 +01:00
const elapsedTimeMs = Date . now ( ) - startTime . getTime ( ) ;
2018-01-01 19:47:50 -05:00
2019-12-18 22:24:13 +01:00
log . info ( ` Content hash computation took ${ elapsedTimeMs } ms ` ) ;
2018-01-01 19:47:50 -05:00
return hashes ;
2017-11-21 22:11:27 -05:00
}
2018-04-07 22:59:47 -04:00
async function checkContentHashes ( otherHashes ) {
2019-12-18 22:24:13 +01:00
const entityHashes = await getEntityHashes ( ) ;
const failedChecks = [ ] ;
2018-04-07 22:59:47 -04:00
2019-12-18 22:24:13 +01:00
for ( const entityName in entityHashes ) {
const thisSectorHashes = entityHashes [ entityName ] ;
const otherSectorHashes = otherHashes [ entityName ] ;
2018-04-07 22:59:47 -04:00
2019-12-18 22:24:13 +01:00
const sectors = new Set ( Object . keys ( entityHashes ) . concat ( Object . keys ( otherHashes ) ) ) ;
2019-11-19 19:07:14 +01:00
2019-12-18 22:24:13 +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
}
}
}
2019-12-18 22:24:13 +01:00
if ( failedChecks . length === 0 ) {
2018-04-07 22:59:47 -04:00
log . info ( "Content hash checks PASSED" ) ;
}
2019-12-18 22:58:30 +01:00
return failedChecks ;
2018-04-07 22:59:47 -04:00
}
2017-11-21 22:11:27 -05:00
module . exports = {
2019-12-18 22:58:30 +01:00
getEntityHashes ,
2018-04-07 22:59:47 -04:00
checkContentHashes
2017-11-21 22:11:27 -05:00
} ;