|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +import * as jsDiff from "diff"; |
| 4 | +import * as sqlite from "sqlite"; |
| 5 | +import * as sqlite3 from "sqlite3"; |
| 6 | +import sql from "./sql.js"; |
| 7 | + |
| 8 | +import "colors"; |
| 9 | +import path from "path"; |
| 10 | + |
| 11 | +function printDiff(one: string, two: string) { |
| 12 | + const diff = jsDiff.diffChars(one, two); |
| 13 | + |
| 14 | + diff.forEach(function(part){ |
| 15 | + // green for additions, red for deletions |
| 16 | + // grey for common parts |
| 17 | + const color = part.added ? 'green' : |
| 18 | + part.removed ? 'red' : 'grey'; |
| 19 | + process.stderr.write(part.value[color]); |
| 20 | + }); |
| 21 | + |
| 22 | + console.log(""); |
| 23 | +} |
| 24 | + |
| 25 | +function checkMissing(table: string, name: string, ids1: string[], ids2: string[]) { |
| 26 | + const missing = ids1.filter(item => ids2.indexOf(item) < 0); |
| 27 | + |
| 28 | + if (missing.length > 0) { |
| 29 | + console.log("Missing IDs from " + name + " table " + table + ": ", missing); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function handleBuffer(obj: { content: Buffer | string }) { |
| 34 | + if (obj && Buffer.isBuffer(obj.content)) { |
| 35 | + obj.content = obj.content.toString(); |
| 36 | + } |
| 37 | + |
| 38 | + return obj; |
| 39 | +} |
| 40 | + |
| 41 | +function compareRows(table: string, rsLeft: Record<string, any>, rsRight: Record<string, any>, column: string) { |
| 42 | + const leftIds = Object.keys(rsLeft); |
| 43 | + const rightIds = Object.keys(rsRight); |
| 44 | + |
| 45 | + console.log(""); |
| 46 | + console.log("--------------------------------------------------------"); |
| 47 | + console.log(`${table} - ${leftIds.length}/${rightIds.length}`); |
| 48 | + |
| 49 | + checkMissing(table, "right", leftIds, rightIds); |
| 50 | + checkMissing(table, "left", rightIds, leftIds); |
| 51 | + |
| 52 | + const commonIds = leftIds.filter(item => rightIds.includes(item)); |
| 53 | + |
| 54 | + for (const id of commonIds) { |
| 55 | + const valueLeft = handleBuffer(rsLeft[id]); |
| 56 | + const valueRight = handleBuffer(rsRight[id]); |
| 57 | + |
| 58 | + const left = JSON.stringify(valueLeft, null, 2); |
| 59 | + const right = JSON.stringify(valueRight, null, 2); |
| 60 | + |
| 61 | + if (left !== right) { |
| 62 | + console.log("Table " + table + " row with " + column + "=" + id + " differs:"); |
| 63 | + console.log("Left: ", left); |
| 64 | + console.log("Right: ", right); |
| 65 | + printDiff(left, right); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +async function main() { |
| 71 | + const dbLeftPath = process.argv.at(-2); |
| 72 | + const dbRightPath = process.argv.at(-1); |
| 73 | + |
| 74 | + if (process.argv.length < 4 || !dbLeftPath || !dbRightPath) { |
| 75 | + console.log(`Usage: ${process.argv[0]} ${process.argv[1]} path/to/first.db path/to/second.db`); |
| 76 | + process.exit(1); |
| 77 | + } |
| 78 | + |
| 79 | + let dbLeft: sqlite.Database; |
| 80 | + let dbRight: sqlite.Database; |
| 81 | + |
| 82 | + try { |
| 83 | + dbLeft = await sqlite.open({filename: dbLeftPath, driver: sqlite3.Database}); |
| 84 | + } catch (e: any) { |
| 85 | + console.error(`Could not load first database at ${path.resolve(dbRightPath)} due to: ${e.message}`); |
| 86 | + process.exit(2); |
| 87 | + } |
| 88 | + |
| 89 | + try { |
| 90 | + dbRight = await sqlite.open({filename: dbRightPath, driver: sqlite3.Database}); |
| 91 | + } catch (e: any) { |
| 92 | + console.error(`Could not load second database at ${path.resolve(dbRightPath)} due to: ${e.message}`); |
| 93 | + process.exit(3); |
| 94 | + } |
| 95 | + |
| 96 | + async function compare(table: string, column: string, query: string) { |
| 97 | + const rsLeft = await sql.getIndexed(dbLeft, column, query); |
| 98 | + const rsRight = await sql.getIndexed(dbRight, column, query); |
| 99 | + |
| 100 | + compareRows(table, rsLeft, rsRight, column); |
| 101 | + } |
| 102 | + |
| 103 | + await compare("branches", "branchId", |
| 104 | + "SELECT branchId, noteId, parentNoteId, notePosition, utcDateModified, isDeleted, prefix FROM branches"); |
| 105 | + |
| 106 | + await compare("notes", "noteId", |
| 107 | + "SELECT noteId, title, dateCreated, utcDateCreated, isProtected, isDeleted FROM notes WHERE isDeleted = 0"); |
| 108 | + |
| 109 | + await compare("note_contents", "noteId", |
| 110 | + "SELECT note_contents.noteId, note_contents.content FROM note_contents JOIN notes USING(noteId) WHERE isDeleted = 0"); |
| 111 | + |
| 112 | + await compare("note_revisions", "noteRevisionId", |
| 113 | + "SELECT noteRevisionId, noteId, title, dateCreated, dateLastEdited, utcDateCreated, utcDateLastEdited, isProtected FROM note_revisions"); |
| 114 | + |
| 115 | + await compare("note_revision_contents", "noteRevisionId", |
| 116 | + "SELECT noteRevisionId, content FROM note_revision_contents"); |
| 117 | + |
| 118 | + await compare("options", "name", |
| 119 | + `SELECT name, value, utcDateModified FROM options WHERE isSynced = 1`); |
| 120 | + |
| 121 | + await compare("attributes", "attributeId", |
| 122 | + "SELECT attributeId, noteId, type, name, value FROM attributes"); |
| 123 | + |
| 124 | + await compare("etapi_tokens", "etapiTokenId", |
| 125 | + "SELECT etapiTokenId, name, tokenHash, utcDateCreated, utcDateModified, isDeleted FROM etapi_tokens"); |
| 126 | + |
| 127 | + await compare("entity_changes", "uniqueId", |
| 128 | + "SELECT entityName || '-' || entityId AS uniqueId, hash, isErased, utcDateChanged FROM entity_changes WHERE isSynced = 1"); |
| 129 | +} |
| 130 | + |
| 131 | +(async () => { |
| 132 | + try { |
| 133 | + await main(); |
| 134 | + } catch (e) { |
| 135 | + console.error(e); |
| 136 | + } |
| 137 | +})(); |
0 commit comments