Skip to content

Commit e9d77d9

Browse files
committed
feat: add list backups to v3 API
1 parent a0fde37 commit e9d77d9

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

src/collections/backup/client.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
Backend,
33
BackupCreateStatusGetter,
44
BackupCreator,
5+
BackupGetter,
56
BackupRestoreStatusGetter,
67
BackupRestorer,
78
} from '../../backup/index.js';
@@ -30,7 +31,7 @@ import {
3031
BackupStatusReturn,
3132
} from './types.js';
3233

33-
export const backup = (connection: Connection) => {
34+
export const backup = (connection: Connection): Backup => {
3435
const parseStatus = (res: BackupCreateStatusResponse | BackupRestoreResponse): BackupStatusReturn => {
3536
if (res.id === undefined) {
3637
throw new WeaviateUnexpectedResponseError('Backup ID is undefined in response');
@@ -197,11 +198,14 @@ export const backup = (connection: Connection) => {
197198
}
198199
return status
199200
? {
200-
...parseResponse(res),
201-
...status,
202-
}
201+
...parseResponse(res),
202+
...status,
203+
}
203204
: parseResponse(res);
204205
},
206+
list: async (backend: Backend): Promise<BackupReturn[]> => {
207+
return connection.get<BackupReturn[]>(`/backups/${backend}`);
208+
},
205209
};
206210
};
207211

@@ -251,4 +255,14 @@ export interface Backup {
251255
* @throws {WeaviateBackupCanceled} If the backup restoration is canceled.
252256
*/
253257
restore(args: BackupArgs<BackupConfigRestore>): Promise<BackupReturn>;
258+
259+
/** List existing backups (completed and in-progress) created in a given backend.
260+
*
261+
* @param {Backend} backend Backend whence to list backups.
262+
* @returns {Promise<BackupReturn[]>} The response from Weaviate.
263+
* @throws {WeaviateInvalidInputError} If the input is invalid.
264+
* @throws {WeaviateBackupFailed} If the backup restoration fails.
265+
* @throws {WeaviateBackupCanceled} If the backup restoration is canceled.
266+
* */
267+
list(backend: Backend): Promise<BackupReturn[]>;
254268
}

src/collections/backup/integration.test.ts

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-non-null-assertion */
22
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
33
/* eslint-disable no-await-in-loop */
4+
import { requireAtLeast } from '../../../test/version.js';
45
import { Backend } from '../../backup/index.js';
56
import weaviate, { Collection, WeaviateClient } from '../../index.js';
67

@@ -88,6 +89,23 @@ describe('Integration testing of backups', () => {
8889
backend: res.backend as 'filesystem',
8990
});
9091
expect(status).not.toBe('SUCCESS'); // can be 'STARTED' or 'TRANSFERRING' depending on the speed of the test machine
92+
93+
// wait to complete so that other tests can run without colliding with Weaviate's lack of simultaneous backups
94+
let wait = true;
95+
while (wait) {
96+
const { status, error } = await collection.backup.getCreateStatus({
97+
backupId: res.id as string,
98+
backend: res.backend as Backend,
99+
});
100+
if (status === 'SUCCESS') {
101+
wait = false;
102+
}
103+
if (status === 'FAILED') {
104+
throw new Error(`Backup creation failed: ${error}`);
105+
}
106+
await new Promise((resolve) => setTimeout(resolve, 1000));
107+
}
108+
91109
return collection;
92110
};
93111

@@ -98,8 +116,37 @@ describe('Integration testing of backups', () => {
98116
.then(getCollection)
99117
.then(testCollectionWaitForCompletion)
100118
.then(testCollectionNoWaitForCompletion));
101-
});
102119

103-
function randomBackupId() {
104-
return 'backup-id-' + Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
105-
}
120+
requireAtLeast(1, 32, 0).it('get all exising backups', async () => {
121+
await clientPromise.then(async (client) => {
122+
await client.collections.create({ name: 'TestListBackups' }).then((col) => col.data.insert());
123+
124+
const wantBackups: string[] = [];
125+
for (let i = 0; i < 3; i++) {
126+
wantBackups.push(
127+
await client.backup.create({
128+
backupId: randomBackupId(),
129+
backend: 'filesystem',
130+
includeCollections: ['TestListBackups'],
131+
waitForCompletion: true,
132+
})
133+
.then((res) => {
134+
return res.id;
135+
})
136+
);
137+
}
138+
139+
const gotBackups: string[] = await client.backup
140+
.list('filesystem')
141+
.then((res) => res.map((bu) => bu.id));
142+
143+
// There may be other backups created in other tests;
144+
expect(gotBackups.length).toBeGreaterThanOrEqual(wantBackups.length);
145+
expect(gotBackups).toEqual(expect.arrayContaining(wantBackups));
146+
});
147+
});
148+
149+
function randomBackupId() {
150+
return 'backup-id-' + Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
151+
}
152+
});

0 commit comments

Comments
 (0)