backups: stash the stats to the backups table

This commit is contained in:
Girish Ramakrishnan
2025-08-12 19:41:50 +05:30
parent 847f57855c
commit 2e16dd983f
5 changed files with 107 additions and 118 deletions
+8 -12
View File
@@ -8,7 +8,6 @@ exports = module.exports = {
add,
update,
setState,
setIntegrity,
list,
del,
@@ -30,7 +29,7 @@ const assert = require('assert'),
hat = require('./hat.js'),
safe = require('safetydance');
const BACKUPS_FIELDS = [ 'id', 'remotePath', 'label', 'identifier', 'creationTime', 'packageVersion', 'type', 'integrityJson', 'dependsOnJson', 'state', 'manifestJson', 'preserveSecs', 'encryptionVersion', 'appConfigJson', 'targetId' ].join(',');
const BACKUPS_FIELDS = [ 'id', 'remotePath', 'label', 'identifier', 'creationTime', 'packageVersion', 'type', 'integrityJson', 'statsJson', 'dependsOnJson', 'state', 'manifestJson', 'preserveSecs', 'encryptionVersion', 'appConfigJson', 'targetId' ].join(',');
function postProcess(result) {
assert.strictEqual(typeof result, 'object');
@@ -44,6 +43,9 @@ function postProcess(result) {
result.integrity = result.integrityJson ? safe.JSON.parse(result.integrityJson) : null;
delete result.integrityJson;
result.stats = result.statsJson ? safe.JSON.parse(result.statsJson) : null;
delete result.statsJson;
result.appConfig = result.appConfigJson ? safe.JSON.parse(result.appConfigJson) : null;
delete result.appConfigJson;
@@ -69,9 +71,11 @@ async function add(data) {
const prefixId = data.type === exports.BACKUP_TYPE_APP ? `${data.type}_${data.identifier}` : data.type; // type and identifier are same for other types
const id = `${prefixId}_v${data.packageVersion}_${hat(32)}`; // id is used by the UI to derive dependent packages. making this a UUID will require a lot of db querying
const appConfigJson = data.appConfig ? JSON.stringify(data.appConfig) : null;
const statsJson = data.statsJson ? JSON.stringify(data.statsJson) : null;
const integrityJson = data.integrityJson ? JSON.stringify(data.integrityJson) : null;
const [error] = await safe(database.query('INSERT INTO backups (id, remotePath, identifier, encryptionVersion, packageVersion, type, creationTime, state, dependsOnJson, manifestJson, preserveSecs, appConfigJson, targetId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[ id, data.remotePath, data.identifier, data.encryptionVersion, data.packageVersion, data.type, creationTime, data.state, JSON.stringify(data.dependsOn), manifestJson, data.preserveSecs, appConfigJson, data.targetId ]));
const [error] = await safe(database.query('INSERT INTO backups (id, remotePath, identifier, encryptionVersion, packageVersion, type, creationTime, state, dependsOnJson, manifestJson, preserveSecs, appConfigJson, targetId, statsJson, integrityJson) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[ id, data.remotePath, data.identifier, data.encryptionVersion, data.packageVersion, data.type, creationTime, data.state, JSON.stringify(data.dependsOn), manifestJson, data.preserveSecs, appConfigJson, data.targetId, statsJson, integrityJson ]));
if (error && error.code === 'ER_DUP_ENTRY') throw new BoxError(BoxError.ALREADY_EXISTS, 'Backup already exists');
if (error) throw error;
@@ -170,14 +174,6 @@ async function setState(id, state) {
if (result.affectedRows !== 1) throw new BoxError(BoxError.NOT_FOUND, 'Backup not found');
}
async function setIntegrity(id, integrity) {
assert.strictEqual(typeof id, 'string');
assert.strictEqual(typeof integrity, 'object');
const result = await database.query('UPDATE backups SET integrityJson = ? WHERE id = ?', [JSON.stringify(integrity), id]);
if (result.affectedRows !== 1) throw new BoxError(BoxError.NOT_FOUND, 'Backup not found');
}
async function list(page, perPage) {
assert(typeof page === 'number' && page > 0);
assert(typeof perPage === 'number' && perPage > 0);