Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 54x 54x 784x 483x 483x 483x 483x 483x 483x 483x 483x 483x 483x 784x 931x 931x 467x 467x 464x 464x 464x 464x 464x 464x 464x 464x 464x 464x 464x 931x 931x 931x 931x 16x 16x 448x 448x 448x 448x 448x 448x 448x 448x 458x 458x 448x 448x 448x 931x 931x 931x 931x 931x 931x 931x 931x 931x 931x 931x 931x 464x 464x 931x 784x 54x | 'use strict';
module.exports = ({ journal: { scanJournalSources, }, databaseMigration: { SCHEMA_VERSION }, external: { fs, path, sqlite: Database }, }) => {
function databaseWithoutSchema(databasePath, journalPath, reason) {
return {
databasePath: path.resolve(databasePath),
journalPath: path.resolve(journalPath),
inSync: false,
reason,
added: [],
removed: [],
changed: [],
};
}
function checkDatabaseSync(databasePath, journalPath) {
const resolvedDatabasePath = path.resolve(databasePath);
if (!fs.existsSync(resolvedDatabasePath)) {
return databaseWithoutSchema(databasePath, journalPath, 'database_missing');
}
const database = new Database(resolvedDatabasePath, { readonly: true, fileMustExist: true });
try {
const hasDatabaseMetadata = database.prepare(`
SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = 'database_metadata'
`).pluck().get();
const hasLegacyMetadata = database.prepare(`
SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = 'metadata'
`).pluck().get();
const hasSourceFiles = database.prepare(`
SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = 'source_files'
`).pluck().get();
if ((!hasDatabaseMetadata && !hasLegacyMetadata) || !hasSourceFiles) {
return databaseWithoutSchema(databasePath, journalPath, 'schema_missing');
}
const metadataTable = hasDatabaseMetadata ? 'database_metadata' : 'metadata';
const metadata = new Map(database.prepare(`SELECT key, value FROM ${metadataTable}`).raw().all());
if (metadata.get('schema_version') !== SCHEMA_VERSION) {
return databaseWithoutSchema(databasePath, journalPath, 'schema_version_mismatch');
}
const journal = scanJournalSources(journalPath);
const storedFiles = database.prepare('SELECT path, sha256, size FROM source_files ORDER BY id').all();
const storedByPath = new Map(storedFiles.map((file) => [file.path, file]));
const currentByPath = new Map(journal.files.map((file) => [file.path, file]));
const added = journal.files.filter((file) => !storedByPath.has(file.path)).map((file) => file.path);
const removed = storedFiles.filter((file) => !currentByPath.has(file.path)).map((file) => file.path);
const changed = journal.files
.filter((file) => {
const stored = storedByPath.get(file.path);
return stored && (stored.sha256 !== file.sha256 || stored.size !== file.size);
})
.map((file) => file.path);
const rootMatches = metadata.get('root_path') === journal.journalPath;
const inSync = rootMatches && added.length === 0 && removed.length === 0 && changed.length === 0;
return {
databasePath: resolvedDatabasePath,
journalPath: journal.journalPath,
inSync,
reason: inSync ? 'in_sync' : (rootMatches ? 'source_files_changed' : 'root_path_changed'),
added,
removed,
changed,
};
}
finally {
database.close();
}
}
return { checkDatabaseSync };
};
|