diff --git a/src/apps.js b/src/apps.js index 08997f667..70ddc3616 100644 --- a/src/apps.js +++ b/src/apps.js @@ -1463,7 +1463,7 @@ async function listBackups(app, page, perPage) { assert(typeof page === 'number' && page > 0); assert(typeof perPage === 'number' && perPage > 0); - return await backups.getByIdentifierAndStatePaged(app.id, backups.BACKUP_STATE_NORMAL, page, perPage); + return await backups.listByIdentifierAndStatePaged(app.id, backups.BACKUP_STATE_NORMAL, page, perPage); } async function listEventlog(app, page, perPage) { @@ -2743,7 +2743,7 @@ async function archive(app, backupId, auditSource) { if (app.manifest.id === constants.PROXY_APP_APPSTORE_ID) throw new BoxError(BoxError.BAD_FIELD, 'cannot archive proxy app'); - const result = await backups.getByIdentifierAndStatePaged(app.id, backups.BACKUP_STATE_NORMAL, 1, 1); + const result = await backups.listByIdentifierAndStatePaged(app.id, backups.BACKUP_STATE_NORMAL, 1, 1); if (result.length === 0) throw new BoxError(BoxError.BAD_STATE, 'No recent backup to archive'); if (result[0].id !== backupId) throw new BoxError(BoxError.BAD_STATE, 'Latest backup id has changed'); @@ -2890,7 +2890,7 @@ async function restoreApps(apps, options, auditSource) { apps = apps.filter(app => app.installationState !== ISTATE_PENDING_RESTORE); // safeguard against tasks being created non-stop if we crash on startup for (const app of apps) { - const [error, results] = await safe(backups.getByIdentifierAndStatePaged(app.id, backups.BACKUP_STATE_NORMAL, 1, 1)); + const [error, results] = await safe(backups.listByIdentifierAndStatePaged(app.id, backups.BACKUP_STATE_NORMAL, 1, 1)); let installationState, restoreConfig; if (!error && results.length) { installationState = ISTATE_PENDING_RESTORE; diff --git a/src/backups.js b/src/backups.js index d0652e2e8..d6c5a7aea 100644 --- a/src/backups.js +++ b/src/backups.js @@ -76,19 +76,6 @@ async function add(data) { return id; } -async function getByIdentifierAndStatePaged(identifier, state, page, perPage) { - assert.strictEqual(typeof identifier, 'string'); - assert.strictEqual(typeof state, 'string'); - assert(typeof page === 'number' && page > 0); - assert(typeof perPage === 'number' && perPage > 0); - - const results = await database.query(`SELECT ${BACKUPS_FIELDS} FROM backups WHERE identifier = ? AND state = ? ORDER BY creationTime DESC LIMIT ?,?`, [ identifier, state, (page-1)*perPage, perPage ]); - - results.forEach(postProcess); - - return results; -} - async function getLatestInTargetByIdentifier(identifier, siteId) { assert.strictEqual(typeof identifier, 'string'); assert.strictEqual(typeof siteId, 'string'); @@ -163,6 +150,19 @@ async function listByTypePaged(type, siteId, page, perPage) { return results; } +async function listByIdentifierAndStatePaged(identifier, state, page, perPage) { + assert.strictEqual(typeof identifier, 'string'); + assert.strictEqual(typeof state, 'string'); + assert(typeof page === 'number' && page > 0); + assert(typeof perPage === 'number' && perPage > 0); + + const results = await database.query(`SELECT ${BACKUPS_FIELDS} FROM backups WHERE identifier = ? AND state = ? ORDER BY creationTime DESC LIMIT ?,?`, [ identifier, state, (page-1)*perPage, perPage ]); + + results.forEach(postProcess); + + return results; +} + async function del(id) { assert.strictEqual(typeof id, 'string'); @@ -230,7 +230,7 @@ async function clearTasks() { export default { get, - getByIdentifierAndStatePaged, + listByIdentifierAndStatePaged, getLatestInTargetByIdentifier, // brutal function name add, update, diff --git a/src/provision.js b/src/provision.js index 4a8a80d43..cb6018fbb 100644 --- a/src/provision.js +++ b/src/provision.js @@ -182,7 +182,7 @@ async function restoreTask(backupSite, remotePath, ipv4Config, ipv6Config, optio await backuptask.restore(backupSite, remotePath, (progress) => setProgress('restore', progress.message)); setProgress('restore', 'Downloading mail backup'); - const mailBackups = await backups.getByIdentifierAndStatePaged(backups.BACKUP_IDENTIFIER_MAIL, backups.BACKUP_STATE_NORMAL, 1, 1); + const mailBackups = await backups.listByIdentifierAndStatePaged(backups.BACKUP_IDENTIFIER_MAIL, backups.BACKUP_STATE_NORMAL, 1, 1); if (mailBackups.length === 0) throw new BoxError(BoxError.NOT_FOUND, 'mail backup not found'); const mailRemotePath = mailBackups[0].remotePath; await backuptask.downloadMail(backupSite, mailRemotePath, (progress) => setProgress('restore', progress.message)); diff --git a/src/routes/backups.js b/src/routes/backups.js index a9f46c451..2d7fae4ae 100644 --- a/src/routes/backups.js +++ b/src/routes/backups.js @@ -32,7 +32,7 @@ async function list(req, res, next) { const perPage = typeof req.query.per_page === 'string'? parseInt(req.query.per_page) : 25; if (!perPage || perPage < 0) return next(new HttpError(400, 'per_page query param has to be a postive number')); - const [error, result] = await safe(backups.getByIdentifierAndStatePaged(backups.BACKUP_IDENTIFIER_BOX, backups.BACKUP_STATE_NORMAL, page, perPage)); + const [error, result] = await safe(backups.listByIdentifierAndStatePaged(backups.BACKUP_IDENTIFIER_BOX, backups.BACKUP_STATE_NORMAL, page, perPage)); if (error) return next(BoxError.toHttpError(error)); next(new HttpSuccess(200, { backups: result.map(backups.removePrivateFields) })); diff --git a/src/test/backups-test.js b/src/test/backups-test.js index 9e4df8f44..6da7377a6 100644 --- a/src/test/backups-test.js +++ b/src/test/backups-test.js @@ -119,8 +119,8 @@ describe('backups', function () { expect(result).to.eql(appBackup); }); - it('getByIdentifierAndStatePaged succeeds', async function () { - const results = await backups.getByIdentifierAndStatePaged(appBackup.identifier, backups.BACKUP_STATE_CREATING, 1, 5); + it('listByIdentifierAndStatePaged succeeds', async function () { + const results = await backups.listByIdentifierAndStatePaged(appBackup.identifier, backups.BACKUP_STATE_CREATING, 1, 5); expect(results.length).to.be(1); delete results[0].creationTime; expect(results[0]).to.eql(appBackup); diff --git a/src/test/backuptask-test.js b/src/test/backuptask-test.js index 520b1f4d4..597d57e57 100644 --- a/src/test/backuptask-test.js +++ b/src/test/backuptask-test.js @@ -50,7 +50,7 @@ describe('backuptask', function () { if (p.error) throw new Error(`backup failed: taskId: ${taskId} ${p.error.message}`); if (!p.result) throw new Error('backup has no result:' + p); - const result = await backups.getByIdentifierAndStatePaged(backups.BACKUP_IDENTIFIER_BOX, backups.BACKUP_STATE_NORMAL, 1, 1); + const result = await backups.listByIdentifierAndStatePaged(backups.BACKUP_IDENTIFIER_BOX, backups.BACKUP_STATE_NORMAL, 1, 1); if (result.length !== 1) throw new Error('result is not of length 1');