33/* eslint-disable no-await-in-loop */
44import { Backend } from '../../backup/index.js' ;
55import weaviate , { Collection , WeaviateClient } from '../../index.js' ;
6+ import { requireAtLeast } from '../../../test/version' ;
7+ import { WeaviateBackupFailed } from '../../errors.js' ;
68
79// These must run sequentially because Weaviate is not capable of running multiple backups at the same time
810describe ( 'Integration testing of backups' , ( ) => {
@@ -88,6 +90,23 @@ describe('Integration testing of backups', () => {
8890 backend : res . backend as 'filesystem' ,
8991 } ) ;
9092 expect ( status ) . not . toBe ( 'SUCCESS' ) ; // can be 'STARTED' or 'TRANSFERRING' depending on the speed of the test machine
93+
94+ // wait to complete so that other tests can run without colliding with Weaviate's lack of simultaneous backups
95+ let wait = true ;
96+ while ( wait ) {
97+ const { status, error } = await collection . backup . getCreateStatus ( {
98+ backupId : res . id as string ,
99+ backend : res . backend as Backend ,
100+ } ) ;
101+ if ( status === 'SUCCESS' ) {
102+ wait = false ;
103+ }
104+ if ( status === 'FAILED' ) {
105+ throw new Error ( `Backup creation failed: ${ error } ` ) ;
106+ }
107+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
108+ }
109+
91110 return collection ;
92111 } ;
93112
@@ -98,6 +117,87 @@ describe('Integration testing of backups', () => {
98117 . then ( getCollection )
99118 . then ( testCollectionWaitForCompletion )
100119 . then ( testCollectionNoWaitForCompletion ) ) ;
120+
121+ requireAtLeast ( 1 , 32 , 3 ) . describe ( 'overwrite alias' , ( ) => {
122+
123+ test ( 'overwriteAlias=true' , async ( ) => {
124+ const client = await clientPromise ;
125+
126+ const things = await client . collections . create ( { name : "ThingsTrue" } ) ;
127+ await client . alias . create ( { collection : things . name , alias : `${ things . name } Alias` } ) ;
128+
129+ const backup = await client . backup . create ( {
130+ backend : 'filesystem' ,
131+ backupId : randomBackupId ( ) ,
132+ includeCollections : [ things . name ] ,
133+ waitForCompletion : true ,
134+ } ) ;
135+
136+ await client . collections . delete ( things . name ) ;
137+ await client . alias . delete ( `${ things . name } Alias` ) ;
138+
139+ // Change alias to point to a different collection
140+ const inventory = await client . collections . create ( { name : "InventoryTrue" } ) ;
141+ await client . alias . create ( { collection : inventory . name , alias : `${ things . name } Alias` } ) ;
142+
143+
144+ // Restore backup with overwriteAlias=true
145+ await client . backup . restore ( {
146+ backend : 'filesystem' ,
147+ backupId : backup . id ,
148+ includeCollections : [ things . name ] ,
149+ waitForCompletion : true ,
150+ config : { overwriteAlias : true } ,
151+ } ) ;
152+
153+ // Assert: alias points to the original collection
154+ const alias = await client . alias . get ( `${ things . name } Alias` ) ;
155+ expect ( alias . collection ) . toEqual ( things . name ) ;
156+ } ) ;
157+
158+ test ( 'overwriteAlias=false' , async ( ) => {
159+ const client = await clientPromise ;
160+
161+ const things = await client . collections . create ( { name : "ThingsFalse" } ) ;
162+ await client . alias . create ( { collection : things . name , alias : `${ things . name } Alias` } ) ;
163+
164+ const backup = await client . backup . create ( {
165+ backend : 'filesystem' ,
166+ backupId : randomBackupId ( ) ,
167+ includeCollections : [ things . name ] ,
168+ waitForCompletion : true ,
169+ } ) ;
170+
171+ await client . collections . delete ( things . name ) ;
172+ await client . alias . delete ( `${ things . name } Alias` ) ;
173+
174+ // Change alias to point to a different collection
175+ const inventory = await client . collections . create ( { name : "InventoryFalse" } ) ;
176+ await client . alias . create ( { collection : inventory . name , alias : `${ things . name } Alias` } ) ;
177+
178+
179+ // Restore backup with overwriteAlias=true
180+ const restored = client . backup . restore ( {
181+ backend : 'filesystem' ,
182+ backupId : backup . id ,
183+ includeCollections : [ things . name ] ,
184+ waitForCompletion : true ,
185+ config : { overwriteAlias : false } ,
186+ } ) ;
187+
188+ // Assert: fails with "alias already exists" error
189+ await expect ( restored ) . rejects . toThrowError ( WeaviateBackupFailed ) ;
190+ } ) ;
191+
192+ it ( 'cleanup' , async ( ) => {
193+ await clientPromise . then ( async c => {
194+ await Promise . all ( [ "ThingsTrue" , "ThingsFalse" , "InventoryTrue" , "InventoryFalse" ]
195+ . map ( name => c . collections . delete ( name ) . catch ( e => { } ) ) ) ;
196+ await c . alias . delete ( "ThingsFalseAlias" ) . catch ( e => { } ) ;
197+ await c . alias . delete ( "ThingsTrueAlias" ) . catch ( e => { } ) ;
198+ } ) ;
199+ } )
200+ } ) ;
101201} ) ;
102202
103203function randomBackupId ( ) {
0 commit comments