@@ -181,11 +181,7 @@ const clonedPostgresContainer = async ({}, use: Use<StartedPostgreSqlContainer>)
181181 const baseUri = container . getConnectionUri ( ) ;
182182 const cloneDb = `test_${ pgCloneCounter ++ } ` ;
183183
184- const admin = new PrismaClient ( {
185- datasources : { db : { url : postgresUriWithDatabase ( baseUri , "postgres" ) } } ,
186- } ) ;
187- await admin . $executeRawUnsafe ( `CREATE DATABASE "${ cloneDb } " TEMPLATE "${ POSTGRES_TEMPLATE_DB } "` ) ;
188- await admin . $disconnect ( ) ;
184+ await createDatabaseFromTemplate ( baseUri , cloneDb ) ;
189185
190186 const cloneUri = postgresUriWithDatabase ( baseUri , cloneDb ) ;
191187 const view = new Proxy ( container , {
@@ -200,19 +196,36 @@ const clonedPostgresContainer = async ({}, use: Use<StartedPostgreSqlContainer>)
200196 try {
201197 await use ( view ) ;
202198 } finally {
203- // Best-effort drop so clones don't pile up in the worker's pg over a long suite. WITH (FORCE)
204- // terminates any lingering backends (pg 13+). A failed drop is harmless - the whole container is
205- // reaped on worker exit - so we never let cleanup fail the test.
206- const cleanup = new PrismaClient ( {
207- datasources : { db : { url : postgresUriWithDatabase ( baseUri , "postgres" ) } } ,
208- } ) ;
209- try {
210- await cleanup . $executeRawUnsafe ( `DROP DATABASE IF EXISTS "${ cloneDb } " WITH (FORCE)` ) ;
211- } catch {
212- // ignore - reaped with the container anyway
213- } finally {
214- await cleanup . $disconnect ( ) ;
215- }
199+ await dropCloneDatabase ( baseUri , cloneDb ) ;
200+ }
201+ } ;
202+
203+ const createDatabaseFromTemplate = async ( baseUri : string , cloneDb : string ) => {
204+ const admin = new PrismaClient ( {
205+ datasources : { db : { url : postgresUriWithDatabase ( baseUri , "postgres" ) } } ,
206+ } ) ;
207+ try {
208+ await admin . $executeRawUnsafe (
209+ `CREATE DATABASE "${ cloneDb } " TEMPLATE "${ POSTGRES_TEMPLATE_DB } "`
210+ ) ;
211+ } finally {
212+ await admin . $disconnect ( ) ;
213+ }
214+ } ;
215+
216+ // Best-effort drop so clones don't pile up in the worker's pg over a long suite. WITH (FORCE)
217+ // terminates any lingering backends (pg 13+). A failed drop is harmless - the whole container is
218+ // reaped on worker exit - so we never let cleanup fail the test.
219+ const dropCloneDatabase = async ( baseUri : string , cloneDb : string ) => {
220+ const cleanup = new PrismaClient ( {
221+ datasources : { db : { url : postgresUriWithDatabase ( baseUri , "postgres" ) } } ,
222+ } ) ;
223+ try {
224+ await cleanup . $executeRawUnsafe ( `DROP DATABASE IF EXISTS "${ cloneDb } " WITH (FORCE)` ) ;
225+ } catch {
226+ // ignore - reaped with the container anyway
227+ } finally {
228+ await cleanup . $disconnect ( ) ;
216229 }
217230} ;
218231
@@ -224,11 +237,7 @@ const schemaOnlyPrismaFixture = async ({}: {}, use: Use<PrismaClient>) => {
224237 const baseUri = container . getConnectionUri ( ) ;
225238 const cloneDb = `schema_only_${ pgCloneCounter ++ } ` ;
226239
227- const admin = new PrismaClient ( {
228- datasources : { db : { url : postgresUriWithDatabase ( baseUri , "postgres" ) } } ,
229- } ) ;
230- await admin . $executeRawUnsafe ( `CREATE DATABASE "${ cloneDb } " TEMPLATE "${ POSTGRES_TEMPLATE_DB } "` ) ;
231- await admin . $disconnect ( ) ;
240+ await createDatabaseFromTemplate ( baseUri , cloneDb ) ;
232241
233242 const prisma = new PrismaClient ( {
234243 datasources : { db : { url : postgresUriWithDatabase ( baseUri , cloneDb ) } } ,
@@ -237,18 +246,7 @@ const schemaOnlyPrismaFixture = async ({}: {}, use: Use<PrismaClient>) => {
237246 await use ( prisma ) ;
238247 } finally {
239248 await logCleanup ( "schemaOnlyPrisma" , prisma . $disconnect ( ) ) ;
240- // Best-effort drop, mirroring clonedPostgresContainer cleanup - the container is reaped on
241- // worker exit anyway, so never let cleanup fail the test.
242- const cleanup = new PrismaClient ( {
243- datasources : { db : { url : postgresUriWithDatabase ( baseUri , "postgres" ) } } ,
244- } ) ;
245- try {
246- await cleanup . $executeRawUnsafe ( `DROP DATABASE IF EXISTS "${ cloneDb } " WITH (FORCE)` ) ;
247- } catch {
248- // ignore - reaped with the container anyway
249- } finally {
250- await cleanup . $disconnect ( ) ;
251- }
249+ await dropCloneDatabase ( baseUri , cloneDb ) ;
252250 }
253251} ;
254252
0 commit comments