Add hierarchical region data to candidate and center records
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { pbkdf2Sync, randomBytes } from 'node:crypto';
|
||||
import { rm } from 'node:fs/promises';
|
||||
import { isAbsolute, join, relative, resolve } from 'node:path';
|
||||
import { createDatabase } from '../database.mjs';
|
||||
import { createSeedDatabase } from '../src/data/seed.mjs';
|
||||
|
||||
const root = resolve(process.cwd());
|
||||
const databasePath = join(root, 'data', 'exam.sqlite');
|
||||
const relativePath = relative(root, databasePath);
|
||||
if (!relativePath || relativePath.startsWith('..') || isAbsolute(relativePath)) throw new Error('拒绝重建工作区以外的数据库');
|
||||
|
||||
for (const path of [databasePath, `${databasePath}-shm`, `${databasePath}-wal`]) await rm(path, { force: true });
|
||||
|
||||
function hashPassword(password, salt = randomBytes(16).toString('hex')) {
|
||||
const hash = pbkdf2Sync(password, salt, 120000, 32, 'sha256').toString('hex');
|
||||
return `${salt}:${hash}`;
|
||||
}
|
||||
|
||||
process.env.DATABASE_CLIENT = 'sqlite';
|
||||
process.env.SQLITE_PATH = databasePath;
|
||||
const database = await createDatabase({
|
||||
root,
|
||||
seed: () => createSeedDatabase({ nowIso: () => new Date().toISOString(), hashPassword })
|
||||
});
|
||||
const state = await database.read();
|
||||
await database.close();
|
||||
console.log(`Rebuilt ${databasePath} (schema ${state.meta.version}, ${state.candidateProfiles.length} candidates, ${state.testCenters.length} centers)`);
|
||||
Reference in New Issue
Block a user