()
| 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 | } |
no test coverage detected