Skip to content

Commit 824fe91

Browse files
committed
delete-database endpointti
1 parent 51a5a71 commit 824fe91

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

backend/src/app.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import keywordRouter from './controllers/keyword.js';
77
import { fileURLToPath } from 'url';
88
import { runSetup } from './dbSetup.js';
99
import { getLatestStatusEntry, getAllStatusEntries, clearAllStatusEntries } from './db/models/status.js';
10-
import { addStatusRow, createTables } from './db/db.js';
10+
import { addStatusRow, createTables, dropTables } from './db/db.js';
1111
import { yearFrom, yearTo } from './util/config.js';
1212

1313
const app = express()
@@ -113,6 +113,27 @@ app.delete('/api/status', async (req: express.Request, res: express.Response): P
113113
}
114114
});
115115

116+
117+
app.delete('/api/delete-database', async (req: express.Request, res: express.Response): Promise<void> => {
118+
try {
119+
const started = Date.now();
120+
await addStatusRow({ action: 'db_clear_start', startedAt: new Date().toISOString() }, true);
121+
122+
await dropTables();
123+
console.log('All tables dropped');
124+
await createTables();
125+
console.log('Schema recreated');
126+
const deletedCount = await clearAllStatusEntries();
127+
128+
const ms = Date.now() - started;
129+
await addStatusRow({ action: 'db_clear_complete', durationMs: ms, completedAt: new Date().toISOString() }, false);
130+
res.status(200).json({ message: 'Database cleared and schema recreated', deletedStatusEntries: deletedCount, durationMs: ms });
131+
} catch (error) {
132+
console.error('Clear database endpoint error:', error);
133+
res.status(500).json({ error: 'Failed to clear database' });
134+
}
135+
});
136+
116137
app.get('/favicon.ico', (request: express.Request, response: express.Response): void => {
117138
response.status(204).end();
118139
})

0 commit comments

Comments
 (0)