Skip to content

Commit 76648c1

Browse files
committed
run custom db reset in cleanupDb
1 parent 17341ec commit 76648c1

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

tests/db-utils.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,42 @@ export async function cleanupDb(prisma: PrismaClient) {
120120
{ name: string }[]
121121
>`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '_prisma_migrations';`
122122

123+
const migrationPaths = fs
124+
.readdirSync('prisma/migrations')
125+
.filter((dir) => dir !== 'migration_lock.toml')
126+
.map((dir) => `prisma/migrations/${dir}/migration.sql`)
127+
128+
const migrations = migrationPaths.map((path) => {
129+
// Parse the sql into individual statements
130+
const sql = fs
131+
.readFileSync(path)
132+
.toString()
133+
.split(';')
134+
.map((statement) => (statement += ';'))
135+
136+
// Remove empty last line
137+
sql.pop()
138+
139+
return sql
140+
})
141+
123142
try {
124143
// Disable FK constraints to avoid relation conflicts during deletion
125144
await prisma.$executeRawUnsafe(`PRAGMA foreign_keys = OFF`)
126145
await prisma.$transaction([
127-
// Delete all rows from each table, preserving table structures
146+
// Delete all tables except the ones that are excluded above
128147
...tables.map(({ name }) =>
129-
prisma.$executeRawUnsafe(`DELETE from "${name}"`),
148+
prisma.$executeRawUnsafe(`DROP TABLE "${name}"`),
130149
),
131150
])
151+
152+
// Run the migrations sequentially
153+
for (const migration of migrations) {
154+
await prisma.$transaction([
155+
// Run each sql statement in the migration
156+
...migration.map((sql) => prisma.$executeRawUnsafe(sql)),
157+
])
158+
}
132159
} catch (error) {
133160
console.error('Error cleaning up database:', error)
134161
} finally {

0 commit comments

Comments
 (0)