🐛 修复数据库初始化异常

This commit is contained in:
目棃
2024-10-16 18:41:07 +08:00
parent be24bdc7ce
commit f8121d504c
2 changed files with 38 additions and 1 deletions

View File

@@ -100,6 +100,7 @@ onMounted(async () => {
function listenOnInit(): void {
console.info("[App][listenOnInit] 监听初始化事件!");
event.listen("initApp", async () => {
await checkAppLoad();
await checkDeviceFp();
try {
await checkUserLoad();
@@ -112,6 +113,19 @@ function listenOnInit(): void {
});
}
async function checkAppLoad(): Promise<void> {
let checkDB = false;
try {
checkDB = await TGSqlite.check();
} catch (error) {
if (error instanceof Error) {
await TGLogger.Error(`[App][checkAppLoad] ${error.name}: ${error.message}`);
} else console.error(error);
}
if (!checkDB) await TGSqlite.update();
else await TGLogger.Info("[App][checkAppLoad] 数据库已成功加载!");
}
// 检测 deviceFp
async function checkDeviceFp(): Promise<void> {
const appData = await TGSqlite.getAppData();

View File

@@ -1,12 +1,14 @@
/**
* @file plugins/Sqlite/index.ts
* @description Sqlite 数据库操作类
* @since Beta v0.6.0
* @since Beta v0.6.1
*/
import { app } from "@tauri-apps/api";
import Database from "@tauri-apps/plugin-sql";
import TGLogger from "../../utils/TGLogger.js";
import initDataSql from "./sql/initData.js";
import { insertAppData } from "./sql/insertData.js";
@@ -55,6 +57,27 @@ class Sqlite {
return this.db;
}
/**
* @description 检测是否需要创建数据库
* @since Beta v0.6.1
* @returns {Promise<boolean>}
*/
public async check(): Promise<boolean> {
try {
const db = await this.getDB();
let isVerified = false;
const sqlT = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;";
const res: Array<{ name: string }> = await db.select(sqlT);
if (this.tables.every((item) => res.map((i) => i.name).includes(item))) {
isVerified = true;
}
return isVerified;
} catch (e) {
await TGLogger.Error(JSON.stringify(e));
return false;
}
}
/**
* @description 初始化数据库
* @since Beta v0.4.5