Skip to content

Commit 8d2be4c

Browse files
committed
improve error logging
1 parent 99af7cd commit 8d2be4c

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

tests/db-utils.ts

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -120,40 +120,37 @@ 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.trim())
135-
.filter(Boolean)
136-
.map((statement) => `${statement};`)
137-
138-
return sql
139-
})
140-
141123
try {
142124
// Disable FK constraints to avoid relation conflicts during deletion
143125
await prisma.$executeRawUnsafe(`PRAGMA foreign_keys = OFF`)
144-
await prisma.$transaction([
145-
// Delete all tables except the ones that are excluded above
146-
...tables.map(({ name }) =>
147-
prisma.$executeRawUnsafe(`DROP TABLE "${name}"`),
148-
),
149-
])
150-
151-
// Run the migrations sequentially
152-
for (const migration of migrations) {
153-
await prisma.$transaction([
154-
// Run each sql statement in the migration
155-
...migration.map((sql) => prisma.$executeRawUnsafe(sql)),
156-
])
126+
127+
// Delete tables except the ones that are excluded above
128+
for (const { name } of tables) {
129+
await prisma.$executeRawUnsafe(`DROP TABLE IF EXISTS "${name}"`)
130+
}
131+
132+
const migrationPaths = fs
133+
.readdirSync('prisma/migrations')
134+
.filter((dir) => dir !== 'migration_lock.toml')
135+
.map((dir) => `prisma/migrations/${dir}/migration.sql`)
136+
137+
// Run each migration
138+
for (const path of migrationPaths) {
139+
const sql = fs.readFileSync(path, 'utf8')
140+
const statements = sql
141+
.split(';')
142+
.map((statement) => statement.trim())
143+
.filter(Boolean)
144+
145+
// Run each sql statement in the migration
146+
for (const statement of statements) {
147+
try {
148+
await prisma.$executeRawUnsafe(`${statement};`)
149+
} catch (error) {
150+
console.warn(`Failed to execute statement: ${statement}`, error)
151+
// Continue with the next statement
152+
}
153+
}
157154
}
158155
} catch (error) {
159156
console.error('Error cleaning up database:', error)

0 commit comments

Comments
 (0)