backups: recursively update the dep preserveSecs
One idea was to compute this at cleanup time, but this has two problems: * the UI won't reflect this value. can be good or bad * the cleaner has no easy way to find out the "parent". I guess we should change our data structure, if we want to go down this route...
This commit is contained in:
@@ -6,6 +6,7 @@ exports = module.exports = {
|
||||
getByTypePaged,
|
||||
add,
|
||||
update,
|
||||
setState,
|
||||
list,
|
||||
del,
|
||||
|
||||
@@ -129,13 +130,14 @@ async function add(data) {
|
||||
assert(Array.isArray(data.dependsOn));
|
||||
assert.strictEqual(typeof data.manifest, 'object');
|
||||
assert.strictEqual(typeof data.format, 'string');
|
||||
assert.strictEqual(typeof data.preserveSecs, 'number');
|
||||
|
||||
const creationTime = data.creationTime || new Date(); // allow tests to set the time
|
||||
const manifestJson = JSON.stringify(data.manifest);
|
||||
const id = 'bid-' + uuid.v4();
|
||||
|
||||
const [error] = await safe(database.query('INSERT INTO backups (id, remotePath, identifier, encryptionVersion, packageVersion, type, creationTime, state, dependsOnJson, manifestJson, format) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
||||
[ id, data.remotePath, data.identifier, data.encryptionVersion, data.packageVersion, data.type, creationTime, data.state, JSON.stringify(data.dependsOn), manifestJson, data.format ]));
|
||||
const [error] = await safe(database.query('INSERT INTO backups (id, remotePath, identifier, encryptionVersion, packageVersion, type, creationTime, state, dependsOnJson, manifestJson, format, preserveSecs) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
||||
[ id, data.remotePath, data.identifier, data.encryptionVersion, data.packageVersion, data.type, creationTime, data.state, JSON.stringify(data.dependsOn), manifestJson, data.format, data.preserveSecs ]));
|
||||
|
||||
if (error && error.code === 'ER_DUP_ENTRY') throw new BoxError(BoxError.ALREADY_EXISTS, 'Backup already exists');
|
||||
if (error) throw error;
|
||||
@@ -186,6 +188,7 @@ function validateLabel(label) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// this is called by REST API
|
||||
async function update(id, data) {
|
||||
assert.strictEqual(typeof id, 'string');
|
||||
assert.strictEqual(typeof data, 'object');
|
||||
@@ -198,15 +201,33 @@ async function update(id, data) {
|
||||
|
||||
const fields = [], values = [];
|
||||
for (const p in data) {
|
||||
if (p === 'label' || p === 'preserveSecs' || p === 'state') {
|
||||
if (p === 'label' || p === 'preserveSecs') {
|
||||
fields.push(p + ' = ?');
|
||||
values.push(data[p]);
|
||||
}
|
||||
}
|
||||
values.push(id);
|
||||
|
||||
const backup = await get(id);
|
||||
if (backup === null) throw new BoxError(BoxError.NOT_FOUND, 'Backup not found');
|
||||
|
||||
const result = await database.query('UPDATE backups SET ' + fields.join(', ') + ' WHERE id = ?', values);
|
||||
if (result.affectedRows !== 1) throw new BoxError(BoxError.NOT_FOUND, 'Backup not found');
|
||||
|
||||
if ('preserveSecs' in data) {
|
||||
// update the dependancies
|
||||
for (const depId of backup.dependsOn) {
|
||||
await database.query('UPDATE backups SET preserveSecs=? WHERE id = ?', [ data.preserveSecs, depId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function setState(id, state) {
|
||||
assert.strictEqual(typeof id, 'string');
|
||||
assert.strictEqual(typeof state, 'string');
|
||||
|
||||
const result = await database.query('UPDATE backups SET state = ? WHERE id = ?', [state, id]);
|
||||
if (result.affectedRows !== 1) throw new BoxError(BoxError.NOT_FOUND, 'Backup not found');
|
||||
}
|
||||
|
||||
async function startBackupTask(auditSource) {
|
||||
|
||||
Reference in New Issue
Block a user