自动升级
This commit is contained in:
parent
0223db88b7
commit
0fb285b045
15
src/db.ts
15
src/db.ts
@ -57,11 +57,22 @@ export function openDatabase(): Promise<void> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function tableExists(name: string): Promise<boolean> {
|
||||||
|
const rows = await dbAll<{ cnt: number }>(
|
||||||
|
"SELECT COUNT(*) AS cnt FROM sqlite_master WHERE type='table' AND name=?",
|
||||||
|
[name],
|
||||||
|
);
|
||||||
|
return (rows[0]?.cnt ?? 0) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
export async function initDatabase(): Promise<void> {
|
export async function initDatabase(): Promise<void> {
|
||||||
fs.mkdirSync(MOCK_DIR, { recursive: true });
|
fs.mkdirSync(MOCK_DIR, { recursive: true });
|
||||||
fs.mkdirSync(DATA_DIR, { recursive: true });
|
fs.mkdirSync(DATA_DIR, { recursive: true });
|
||||||
if (!fs.existsSync(DB_FILE) && fs.existsSync(LEGACY_DB_FILE)) {
|
if (!fs.existsSync(DB_FILE) && fs.existsSync(LEGACY_DB_FILE)) {
|
||||||
fs.copyFileSync(LEGACY_DB_FILE, DB_FILE);
|
fs.copyFileSync(LEGACY_DB_FILE, DB_FILE);
|
||||||
|
console.log(
|
||||||
|
`[DB] 已从旧路径迁移数据库: ${LEGACY_DB_FILE} -> ${DB_FILE}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
await openDatabase();
|
await openDatabase();
|
||||||
await dbRun(`
|
await dbRun(`
|
||||||
@ -132,6 +143,7 @@ export async function initDatabase(): Promise<void> {
|
|||||||
value TEXT NOT NULL
|
value TEXT NOT NULL
|
||||||
)
|
)
|
||||||
`);
|
`);
|
||||||
|
const hadIpRules = await tableExists("ip_rules");
|
||||||
await dbRun(`
|
await dbRun(`
|
||||||
CREATE TABLE IF NOT EXISTS ip_rules (
|
CREATE TABLE IF NOT EXISTS ip_rules (
|
||||||
ip TEXT PRIMARY KEY,
|
ip TEXT PRIMARY KEY,
|
||||||
@ -145,6 +157,9 @@ export async function initDatabase(): Promise<void> {
|
|||||||
target_https INTEGER
|
target_https INTEGER
|
||||||
)
|
)
|
||||||
`);
|
`);
|
||||||
|
if (!hadIpRules) {
|
||||||
|
console.log("[DB] 数据库已自动升级: 新增 IP 管理(ip_rules)表");
|
||||||
|
}
|
||||||
// 清理不该出现在 mock 列表中的系统文件
|
// 清理不该出现在 mock 列表中的系统文件
|
||||||
await dbRun(
|
await dbRun(
|
||||||
"DELETE FROM mock_files WHERE file_path = ? OR file_path LIKE ?",
|
"DELETE FROM mock_files WHERE file_path = ? OR file_path LIKE ?",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user