Skip to content

Commit 3c975b9

Browse files
committed
try switching to better-sqlite3
1 parent 39c2702 commit 3c975b9

File tree

3 files changed

+52
-34
lines changed

3 files changed

+52
-34
lines changed

prisma/seed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async function seed() {
1717
console.time(`🌱 Database has been seeded`)
1818

1919
console.time('🧹 Cleaned up the database...')
20-
await cleanupDb(prisma)
20+
await cleanupDb()
2121
console.timeEnd('🧹 Cleaned up the database...')
2222

2323
const totalUsers = 5

tests/db-utils.ts

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'node:fs'
22
import { faker } from '@faker-js/faker'
33
import { type PrismaClient } from '@prisma/client'
44
import bcrypt from 'bcryptjs'
5+
import Database from 'better-sqlite3'
56
import { UniqueEnforcer } from 'enforce-unique'
67

78
const 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
}

tests/setup/db-setup.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ beforeAll(async () => {
1515
// we *must* use dynamic imports here so the process.env.DATABASE_URL is set
1616
// before prisma is imported and initialized
1717
afterEach(async () => {
18-
const { prisma } = await import('#app/utils/db.server.ts')
19-
await cleanupDb(prisma)
18+
await cleanupDb()
2019
})
2120

2221
afterAll(async () => {

0 commit comments

Comments
 (0)