|
1 | 1 | import fs from 'node:fs' |
2 | 2 | import { faker } from '@faker-js/faker' |
3 | 3 | import bcrypt from 'bcryptjs' |
4 | | -import Database from 'better-sqlite3' |
5 | 4 | import { UniqueEnforcer } from 'enforce-unique' |
6 | 5 |
|
7 | 6 | const uniqueUsernameEnforcer = new UniqueEnforcer() |
@@ -114,64 +113,3 @@ export async function img({ |
114 | 113 | blob: await fs.promises.readFile(filepath), |
115 | 114 | } |
116 | 115 | } |
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