diff --git a/src/backupcleaner.js b/src/backupcleaner.js index 9bc11bb2b..197d3a158 100644 --- a/src/backupcleaner.js +++ b/src/backupcleaner.js @@ -119,7 +119,7 @@ async function cleanupAppBackups(site, referencedBackupIds, progressCallback) { const allAppIds = allApps.map(a => a.id); // high number, try to get all app backups as we had a cloudron with over 100 apps with 4 daily backups for one month! - const appBackups = await backups.getByTypePaged(backups.BACKUP_TYPE_APP, 1, 100000); + const appBackups = await backups.listByTypePaged(backups.BACKUP_TYPE_APP, 1, 100000); // collate the backups by app id. note that the app could already have been uninstalled const appBackupsById = {}; @@ -155,7 +155,7 @@ async function cleanupMailBackups(site, referencedBackupIds, progressCallback) { const removedMailBackupPaths = []; - const mailBackups = await backups.getByTypePaged(backups.BACKUP_TYPE_MAIL, 1, 100000); + const mailBackups = await backups.listByTypePaged(backups.BACKUP_TYPE_MAIL, 1, 100000); applyBackupRetention(mailBackups, Object.assign({ keepLatest: true }, site.retention), referencedBackupIds); @@ -180,7 +180,7 @@ async function cleanupBoxBackups(site, progressCallback) { // We need to fetch all box backups to be able to compile a list of all referenced app backupSites. // Otherwise if we miss some app backups, they will get purged! // 100000 here should be seen as infinity - const boxBackups = await backups.getByTypePaged(backups.BACKUP_TYPE_BOX, 1, 100000); + const boxBackups = await backups.listByTypePaged(backups.BACKUP_TYPE_BOX, 1, 100000); applyBackupRetention(boxBackups, Object.assign({ keepLatest: true }, site.retention), [] /* references */); diff --git a/src/backups.js b/src/backups.js index 13063ec03..00641c958 100644 --- a/src/backups.js +++ b/src/backups.js @@ -4,12 +4,12 @@ exports = module.exports = { get, getByIdentifierAndStatePaged, getLatestInTargetByIdentifier, // brutal function name - getByTypePaged, add, update, setState, list, listBySiteId, + listByTypePaged, del, removePrivateFields, @@ -125,18 +125,6 @@ async function get(id) { return postProcess(result[0]); } -async function getByTypePaged(type, page, perPage) { - assert.strictEqual(typeof type, 'string'); - assert(typeof page === 'number' && page > 0); - assert(typeof perPage === 'number' && perPage > 0); - - const results = await database.query(`SELECT ${BACKUPS_FIELDS} FROM backups WHERE type = ? ORDER BY creationTime DESC LIMIT ?,?`, [ type, (page-1)*perPage, perPage ]); - - results.forEach(function (result) { postProcess(result); }); - - return results; -} - function validateLabel(label) { assert.strictEqual(typeof label, 'string'); @@ -196,6 +184,18 @@ async function list(page, perPage) { return results; } +async function listByTypePaged(type, page, perPage) { + assert.strictEqual(typeof type, 'string'); + assert(typeof page === 'number' && page > 0); + assert(typeof perPage === 'number' && perPage > 0); + + const results = await database.query(`SELECT ${BACKUPS_FIELDS} FROM backups WHERE type = ? ORDER BY creationTime DESC LIMIT ?,?`, [ type, (page-1)*perPage, perPage ]); + + results.forEach(function (result) { postProcess(result); }); + + return results; +} + async function listBySiteId(siteId, page, perPage) { assert.strictEqual(typeof siteId, 'string'); assert(typeof page === 'number' && page > 0); diff --git a/src/test/backupcleaner-test.js b/src/test/backupcleaner-test.js index 020b459a0..4c49fe5e7 100644 --- a/src/test/backupcleaner-test.js +++ b/src/test/backupcleaner-test.js @@ -288,7 +288,7 @@ describe('backup cleaner', function () { it('succeeds with box backups, keeps latest', async function () { await cleanupBackups(site); - const results = await backups.getByTypePaged(backups.BACKUP_TYPE_BOX, 1, 1000); + const results = await backups.listByTypePaged(backups.BACKUP_TYPE_BOX, 1, 1000); expect(results.length).to.equal(1); expect(results[0].id).to.equal(BACKUP_1_BOX.id); @@ -300,7 +300,7 @@ describe('backup cleaner', function () { it('does not remove expired backups if only one left', async function () { await cleanupBackups(site); - const results = await backups.getByTypePaged(backups.BACKUP_TYPE_BOX, 1, 1000); + const results = await backups.listByTypePaged(backups.BACKUP_TYPE_BOX, 1, 1000); expect(results[0].id).to.equal(BACKUP_1_BOX.id); // check that app backups are also still there. backup_1 is still there @@ -318,7 +318,7 @@ describe('backup cleaner', function () { await cleanupBackups(site); - let result = await backups.getByTypePaged(backups.BACKUP_TYPE_APP, 1, 1000); + let result = await backups.listByTypePaged(backups.BACKUP_TYPE_APP, 1, 1000); expect(result.length).to.equal(4); result = result.sort((r1, r2) => r1.remotePath.localeCompare(r2.remotePath)); expect(result[0].id).to.be(BACKUP_0_APP_0.id); // because app is installed, latest backup is preserved diff --git a/src/test/backups-test.js b/src/test/backups-test.js index 06583ce22..66698c615 100644 --- a/src/test/backups-test.js +++ b/src/test/backups-test.js @@ -82,8 +82,8 @@ describe('backups', function () { expect(result).to.be(null); }); - it('getByTypePaged succeeds', async function () { - const results = await backups.getByTypePaged(backups.BACKUP_TYPE_BOX, 1, 5); + it('listByTypePaged succeeds', async function () { + const results = await backups.listByTypePaged(backups.BACKUP_TYPE_BOX, 1, 5); expect(results.length).to.be(1); delete results[0].creationTime; expect(results[0]).to.eql(boxBackup);