archives: use separate table

Cleaner to separate things from the backups table.

* icon, appConfig, appStoreIcon etc are only valid for archives
* older version cloudron does not have appConfig in backups table (so it
  cannot be an archive entry)
This commit is contained in:
Girish Ramakrishnan
2024-12-10 10:06:52 +01:00
parent 2ad93c114e
commit 490840b71d
12 changed files with 261 additions and 122 deletions

84
src/test/archives-test.js Normal file
View File

@@ -0,0 +1,84 @@
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict';
const archives = require('../archives.js'),
backups = require('../backups.js'),
BoxError = require('../boxerror.js'),
common = require('./common.js'),
expect = require('expect.js'),
safe = require('safetydance');
describe('Archives', function () {
const { setup, cleanup, auditSource } = common;
const appBackup = {
id: null,
remotePath: 'backup-box',
encryptionVersion: 2,
packageVersion: '1.0.0',
type: backups.BACKUP_TYPE_APP,
state: backups.BACKUP_STATE_NORMAL,
identifier: 'box',
dependsOn: [ 'dep1' ],
manifest: null,
format: 'tgz',
preserveSecs: 0,
label: '',
};
before(async function () {
await setup();
appBackup.id = await backups.add(appBackup);
});
after(cleanup);
const appConfig = { loc: 'loc1' };
let archiveId;
it('cannot add bad backup to archives', async function () {
const [error] = await safe(archives.add('badId', { appConfig }));
expect(error.reason).to.be(BoxError.NOT_FOUND);
});
it('can add good backup to archives', async function () {
archiveId = await archives.add(appBackup.id, { appConfig });
});
it('cannot get invalid archive', async function () {
const result = await archives.get('bad');
expect(result).to.be(null);
});
it('can get archive', async function () {
const result = await archives.get(archiveId);
expect(result.appConfig).to.eql(appConfig);
});
it('can list archives', async function () {
const result = await archives.list(1, 100);
expect(result.length).to.be(1);
expect(result[0].id).to.be(archiveId);
expect(result[0].appConfig).to.eql(appConfig);
});
it('can list backupIds', async function () {
const result = await archives.listBackupIds();
expect(result.length).to.be(1);
expect(result[0]).to.be(appBackup.id);
});
it('cannot delete bad archive', async function () {
const [error] = await safe(archives.del('badId', auditSource));
expect(error.reason).to.be(BoxError.NOT_FOUND);
});
it('can del valid archive', async function () {
await archives.del(archiveId, auditSource);
const result = await archives.list(1, 10);
expect(result.length).to.be(0);
});
});

View File

@@ -6,7 +6,8 @@
'use strict';
const backupCleaner = require('../backupcleaner.js'),
const archives = require('../archives.js'),
backupCleaner = require('../backupcleaner.js'),
backups = require('../backups.js'),
common = require('./common.js'),
expect = require('expect.js'),
@@ -33,7 +34,6 @@ describe('backup cleaner', function () {
manifest: null,
format: 'tgz',
preserveSecs: 0,
archive: false
};
describe('retention', function () {
@@ -123,12 +123,6 @@ describe('backup cleaner', function () {
expect(b[8].keepReason).to.be('keepMonthly');
expect(b[9].keepReason).to.be(undefined);
});
it('keeps archive', function () {
const backup = Object.assign({}, backupTemplate, { archive: true });
backupCleaner._applyBackupRetention([backup], { keepWithinSecs: 0, keepLatest: false }, []);
expect(backup.keepReason).to.be('archive');
});
});
describe('task', function () {
@@ -216,6 +210,22 @@ describe('backup cleaner', function () {
preserveSecs: 0
};
const BACKUP_2_APP_2 = { // this is archived and left alone
id: null,
remotePath: 'backup-app-2',
encryptionVersion: null,
packageVersion: '2.0.0',
type: backups.BACKUP_TYPE_APP,
state: backups.BACKUP_STATE_NORMAL,
identifier: 'app2',
dependsOn: [],
manifest: null,
format: 'tgz',
preserveSecs: 0
};
const app2Config = { loc: 'apploc2' };
before(async function () {
await settings._set(settings.BACKUP_STORAGE_KEY, JSON.stringify({
provider: 'filesystem',
@@ -231,7 +241,6 @@ describe('backup cleaner', function () {
console.log('started task', taskId);
// eslint-disable-next-line no-constant-condition
while (true) {
await timers.setTimeout(1000);
@@ -260,6 +269,9 @@ describe('backup cleaner', function () {
BACKUP_1_APP_1.id = await backups.add(BACKUP_1_APP_1);
BACKUP_1_BOX.dependsOn = [ BACKUP_1_APP_0.id, BACKUP_1_APP_1.id ];
BACKUP_1_BOX.id = await backups.add(BACKUP_1_BOX);
BACKUP_2_APP_2.id = await backups.add(BACKUP_2_APP_2);
await archives.add(BACKUP_2_APP_2.id, { appConfig: app2Config });
});
it('succeeds with box backups, keeps latest', async function () {
@@ -296,11 +308,12 @@ describe('backup cleaner', function () {
await cleanupBackups();
let result = await backups.getByTypePaged(backups.BACKUP_TYPE_APP, 1, 1000);
expect(result.length).to.equal(3);
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
expect(result[1].id).to.be(BACKUP_1_APP_0.id); // referenced by box
expect(result[2].id).to.be(BACKUP_1_APP_1.id); // referenced by box
expect(result[3].id).to.be(BACKUP_2_APP_2.id); // referenced by archive
});
});
});

View File

@@ -31,8 +31,6 @@ describe('backups', function () {
format: 'tgz',
preserveSecs: 0,
label: '',
archive: false,
appConfig: null
};
const appBackup = {
@@ -48,8 +46,6 @@ describe('backups', function () {
format: 'tgz',
preserveSecs: 0,
label: '',
archive: false,
appConfig: null
};
describe('crud', function () {