@@ -5,44 +5,32 @@ import fs from 'node:fs'
5
5
6
6
const config = getConfig ( )
7
7
8
- async function connectAndMigrate (
9
- databaseUrl : string | undefined ,
10
- migrationsDirectory : string ,
11
- logOnError = false
12
- ) {
8
+ async function connectAndMigrate ( client : Client , migrationsDirectory : string , logOnError = false ) {
13
9
if ( ! fs . existsSync ( migrationsDirectory ) ) {
14
10
console . log ( `Migrations directory ${ migrationsDirectory } not found, skipping` )
15
11
return
16
12
}
17
13
18
- const dbConfig = {
19
- connectionString : databaseUrl ,
20
- connectionTimeoutMillis : 10_000 ,
21
- }
22
14
const optionalConfig = {
23
15
schemaName : config . SCHEMA ,
24
16
tableName : 'migrations' ,
25
17
}
26
18
27
- const client = new Client ( dbConfig )
28
19
try {
29
- await client . connect ( )
30
20
await migrate ( { client } , migrationsDirectory , optionalConfig )
31
21
} catch ( error ) {
32
22
if ( logOnError && error instanceof Error ) {
33
23
console . error ( 'Migration error:' , error . message )
34
24
} else {
35
25
throw error
36
26
}
37
- } finally {
38
- await client . end ( )
39
27
}
40
28
}
41
29
42
- export async function runMigrations ( ) : Promise < void > {
30
+ export async function runMigrations ( client : Client ) : Promise < void > {
43
31
try {
44
32
console . log ( 'Running migrations' )
45
- await connectAndMigrate ( config . DATABASE_URL , './db/migrations' )
33
+ await connectAndMigrate ( client , './db/migrations' )
46
34
} catch ( error ) {
47
35
throw error
48
36
} finally {
0 commit comments