Skip to content

Commit d52803a

Browse files
committed
remove cleanup db util
1 parent 3507073 commit d52803a

File tree

5 files changed

+47
-69
lines changed

5 files changed

+47
-69
lines changed

.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ jobs:
118118

119119
- name: 🌱 Seed Database
120120
if: steps.db-cache.outputs.cache-hit != 'true'
121-
run: npx prisma db seed
121+
run: npx prisma migrate reset
122122

123123
- name: 🏗 Build
124124
run: npm run build
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Remove Cleanup DB
2+
3+
Date: 2024-10-28
4+
5+
Status: accepted
6+
7+
## Context
8+
9+
We have a utility called `cleanupDb` that removes all tables from the database
10+
except for prisma migration tables. The reference to prisma migration tables is
11+
unfortunate because those are an implementation detail that we should not have
12+
to think about.
13+
14+
The goal of `cleanupDb` was to make it easy for tests to reset the database
15+
without having to run `prisma migrate reset` which is too slow for lower level
16+
tests.
17+
18+
We also used `cleanupDb` in the seed file to reset the database before seeding
19+
it.
20+
21+
However, after a lot of work on the tests, we found a much simpler solution to
22+
resetting the database between tests: simply copy/paste the `base.db` file
23+
(which is a fresh database) to `test.db` before each test. We were already doing
24+
this before all the tests. It takes nanoseconds and is much simpler.
25+
26+
For the seed script, it's nice to have the database be completely reset when
27+
running `prisma db seed` (in fact, our seed expects the database to be empty),
28+
but you can get the same behavior as our current `seed` with a fresh database by
29+
running `prisma migrate reset` (which runs the seed script after resetting the
30+
database).
31+
32+
It would be nice to ditch the implementation detail of prisma's tables, so we'd
33+
like to remove this utility.
34+
35+
## Decision
36+
37+
Remove the `cleanupDb` utility and update our CI to run `prisma migrate reset`
38+
instead of `prisma db seed`.
39+
40+
## Consequences
41+
42+
Running `prisma db seed` will fail because the seed script expects the database
43+
to be empty. We could address this by using upsert or something, but really
44+
people should just run `prisma migrate reset` to seed the database (which is
45+
effectively what we used to do before removing `cleanupDb`).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"prisma:studio": "prisma studio",
2424
"format": "prettier --write .",
2525
"lint": "eslint .",
26-
"setup": "npm run build && prisma generate && prisma migrate deploy && prisma db seed && playwright install",
26+
"setup": "npm run build && prisma generate && prisma migrate reset && playwright install",
2727
"start": "cross-env NODE_ENV=production node .",
2828
"start:mocks": "cross-env NODE_ENV=production MOCKS=true tsx .",
2929
"test": "vitest",

prisma/seed.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { promiseHash } from 'remix-utils/promise'
33
import { prisma } from '#app/utils/db.server.ts'
44
import { MOCK_CODE_GITHUB } from '#app/utils/providers/constants'
55
import {
6-
cleanupDb,
76
createPassword,
87
createUser,
98
getNoteImages,
@@ -16,10 +15,6 @@ async function seed() {
1615
console.log('🌱 Seeding...')
1716
console.time(`🌱 Database has been seeded`)
1817

19-
console.time('🧹 Cleaned up the database...')
20-
await cleanupDb()
21-
console.timeEnd('🧹 Cleaned up the database...')
22-
2318
const totalUsers = 5
2419
console.time(`👤 Created ${totalUsers} users...`)
2520
const noteImages = await getNoteImages()

tests/db-utils.ts

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fs from 'node:fs'
22
import { faker } from '@faker-js/faker'
33
import bcrypt from 'bcryptjs'
4-
import Database from 'better-sqlite3'
54
import { UniqueEnforcer } from 'enforce-unique'
65

76
const uniqueUsernameEnforcer = new UniqueEnforcer()
@@ -114,64 +113,3 @@ export async function img({
114113
blob: await fs.promises.readFile(filepath),
115114
}
116115
}
117-
118-
let _migrationSqls: Array<Array<string>> | undefined
119-
async function getMigrationSqls() {
120-
if (_migrationSqls) return _migrationSqls
121-
122-
const migrationSqls: Array<Array<string>> = []
123-
const migrationPaths = (await fs.promises.readdir('prisma/migrations'))
124-
.filter((dir) => dir !== 'migration_lock.toml')
125-
.map((dir) => `prisma/migrations/${dir}/migration.sql`)
126-
127-
for (const path of migrationPaths) {
128-
const sql = await fs.promises.readFile(path, 'utf8')
129-
const statements = sql
130-
.split(';')
131-
.map((statement) => statement.trim())
132-
.filter(Boolean)
133-
migrationSqls.push(statements)
134-
}
135-
136-
_migrationSqls = migrationSqls
137-
138-
return migrationSqls
139-
}
140-
141-
export async function cleanupDb() {
142-
const db = new Database(process.env.DATABASE_URL!.replace('file:', ''))
143-
144-
try {
145-
// Disable FK constraints to avoid relation conflicts during deletion
146-
db.exec('PRAGMA foreign_keys = OFF')
147-
148-
// Get all table names
149-
const tables = db
150-
.prepare(
151-
`
152-
SELECT name FROM sqlite_master
153-
WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '_prisma_migrations'
154-
`,
155-
)
156-
.all() as { name: string }[]
157-
158-
// Delete tables except the ones that are excluded above
159-
for (const { name } of tables) {
160-
db.exec(`DROP TABLE IF EXISTS "${name}"`)
161-
}
162-
163-
// Get migration SQLs and run each migration
164-
const migrationSqls = await getMigrationSqls()
165-
for (const statements of migrationSqls) {
166-
// Run each sql statement in the migration
167-
db.transaction(() => {
168-
for (const statement of statements) {
169-
db.exec(statement)
170-
}
171-
})()
172-
}
173-
} finally {
174-
db.exec('PRAGMA foreign_keys = ON')
175-
db.close()
176-
}
177-
}

0 commit comments

Comments
 (0)