@@ -2,6 +2,7 @@ import fs from 'node:fs'
22import { faker } from '@faker-js/faker'
33import { type PrismaClient } from '@prisma/client'
44import bcrypt from 'bcryptjs'
5+ import Database from 'better-sqlite3'
56import { UniqueEnforcer } from 'enforce-unique'
67
78const uniqueUsernameEnforcer = new UniqueEnforcer ( )
@@ -115,45 +116,63 @@ export async function img({
115116 }
116117}
117118
118- export async function cleanupDb ( prisma : PrismaClient ) {
119- const tables = await prisma . $queryRaw <
120- { name : string } [ ]
121- > `SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '_prisma_migrations';`
119+ let _migrationSqls : Array < Array < string > > | undefined
120+ async function getMigrationSqls ( ) {
121+ if ( _migrationSqls ) return _migrationSqls
122+
123+ const migrationSqls : Array < Array < string > > = [ ]
124+ const migrationPaths = ( await fs . promises . readdir ( 'prisma/migrations' ) )
125+ . filter ( ( dir ) => dir !== 'migration_lock.toml' )
126+ . map ( ( dir ) => `prisma/migrations/${ dir } /migration.sql` )
127+
128+ for ( const path of migrationPaths ) {
129+ const sql = await fs . promises . readFile ( path , 'utf8' )
130+ const statements = sql
131+ . split ( ';' )
132+ . map ( ( statement ) => statement . trim ( ) )
133+ . filter ( Boolean )
134+ migrationSqls . push ( statements )
135+ }
136+
137+ _migrationSqls = migrationSqls
138+
139+ return migrationSqls
140+ }
141+
142+ export async function cleanupDb ( ) {
143+ const db = new Database ( process . env . DATABASE_URL ! . replace ( 'file:' , '' ) )
122144
123145 try {
124146 // Disable FK constraints to avoid relation conflicts during deletion
125- await prisma . $executeRawUnsafe ( `PRAGMA foreign_keys = OFF` )
147+ db . exec ( 'PRAGMA foreign_keys = OFF' )
148+
149+ // Get all table names
150+ const tables = db
151+ . prepare (
152+ `
153+ SELECT name FROM sqlite_master
154+ WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '_prisma_migrations'
155+ ` ,
156+ )
157+ . all ( ) as { name : string } [ ]
126158
127159 // Delete tables except the ones that are excluded above
128- await prisma . $transaction ( [
129- ...tables . map ( ( { name } ) =>
130- prisma . $executeRawUnsafe ( `DROP TABLE IF EXISTS "${ name } "` ) ,
131- ) ,
132- ] )
133-
134- const migrationPaths = fs
135- . readdirSync ( 'prisma/migrations' )
136- . filter ( ( dir ) => dir !== 'migration_lock.toml' )
137- . map ( ( dir ) => `prisma/migrations/${ dir } /migration.sql` )
138-
139- // Run each migration
140- for ( const path of migrationPaths ) {
141- const sql = fs . readFileSync ( path , 'utf8' )
142- const statements = sql
143- . split ( ';' )
144- . map ( ( statement ) => statement . trim ( ) )
145- . filter ( Boolean )
160+ for ( const { name } of tables ) {
161+ db . exec ( `DROP TABLE IF EXISTS "${ name } "` )
162+ }
146163
164+ // Get migration SQLs and run each migration
165+ const migrationSqls = await getMigrationSqls ( )
166+ for ( const statements of migrationSqls ) {
147167 // Run each sql statement in the migration
148- await prisma . $ transaction( [
149- ... statements . map ( ( statement ) =>
150- prisma . $executeRawUnsafe ( ` ${ statement } ` ) ,
151- ) ,
152- ] )
168+ db . transaction ( ( ) => {
169+ for ( const statement of statements ) {
170+ db . exec ( statement )
171+ }
172+ } ) ( )
153173 }
154- } catch ( error ) {
155- console . error ( 'Error cleaning up database:' , error )
156174 } finally {
157- await prisma . $executeRawUnsafe ( `PRAGMA foreign_keys = ON` )
175+ db . exec ( 'PRAGMA foreign_keys = ON' )
176+ db . close ( )
158177 }
159178}
0 commit comments