@@ -3,8 +3,14 @@ import { entries, mapKeys } from '@seedcompany/common';
3
3
import { Connection , node , type Query , relation } from 'cypher-query-builder' ;
4
4
import { LazyGetter } from 'lazy-get-decorator' ;
5
5
import { pickBy , startCase } from 'lodash' ;
6
- import { Duration } from 'luxon' ;
7
- import { defer , firstValueFrom , shareReplay , takeUntil } from 'rxjs' ;
6
+ import { DateTime , Duration } from 'luxon' ;
7
+ import {
8
+ defer ,
9
+ EmptyError ,
10
+ firstValueFrom ,
11
+ shareReplay ,
12
+ takeUntil ,
13
+ } from 'rxjs' ;
8
14
import {
9
15
DuplicateException ,
10
16
type ID ,
@@ -209,6 +215,24 @@ export class DatabaseService {
209
215
await this . runAdminCommand ( 'DROP' , dbName ) ;
210
216
}
211
217
218
+ async dropStaleTestDbs ( ) {
219
+ const info = await this . getServerInfo ( ) . catch ( ( e ) => {
220
+ if ( e instanceof EmptyError ) return null ;
221
+ throw e ;
222
+ } ) ;
223
+ const staleTestDbs = ( info ?. databases ?? [ ] ) . filter ( ( { name } ) => {
224
+ const match = / t e s t \. ( [ \d - ] + \. [ \d - ] + ) \. .+ / . exec ( name ) ;
225
+ if ( ! match ) {
226
+ return false ;
227
+ }
228
+ const dt = DateTime . fromFormat ( match [ 1 ] , 'y-MM-dd.HH-mm-ss' ) ;
229
+ return dt . diffNow ( ) . as ( 'hours' ) < - 1 ;
230
+ } ) ;
231
+ for ( const staleTestDb of staleTestDbs ) {
232
+ await this . runAdminCommand ( 'DROP' , staleTestDb . name ) ;
233
+ }
234
+ }
235
+
212
236
private async runAdminCommand ( action : 'CREATE' | 'DROP' , dbName : string ) {
213
237
// @ts -expect-error Yes this is private, but we have a special use case.
214
238
// We need to run this query with a session that's not configured to use the
0 commit comments