Skip to content

Commit

Permalink
refactor: 移动数据前先关闭数据库连接
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb committed Sep 12, 2024
1 parent dd6b68a commit cdee8a6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/database/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const handlePayload = (payload: TablePayload) => {
* 初始化数据库
*/
export const initDatabase = async () => {
await db?.close();
await closeDatabase();

const appName = await getName();
const ext = isDev() ? "dev.db" : "db";
Expand Down Expand Up @@ -171,3 +171,10 @@ export const deleteSQL = async (tableName: TableName, id?: number) => {
}
}
};

/**
* 关闭数据库连接池
*/
export const closeDatabase = async () => {
await db?.close();
};
6 changes: 5 additions & 1 deletion src/pages/Backup/components/SavePath/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ProListItem from "@/components/ProListItem";
import { open } from "@tauri-apps/api/dialog";
import { emit } from "@tauri-apps/api/event";
import { join } from "@tauri-apps/api/path";
import { Button, Flex } from "antd";
import { Button, Flex, message } from "antd";
import { isString } from "antd/es/button";
import type { FC } from "react";
import { useSnapshot } from "valtio";
Expand All @@ -22,11 +22,15 @@ const SavePath: FC<{ state: State }> = (props) => {

const dirName = await moveData(getSaveDataDir(), select);

if (!dirName) return;

globalStore.env.saveDataDir = await join(select, dirName);

state.spinning = false;

emit(LISTEN_KEY.CHANGE_DATA_FILE);

message.success("更改成功");
};

return (
Expand Down
8 changes: 6 additions & 2 deletions src/plugins/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,21 @@ export const importData = async () => {

if (!path) return;

await closeDatabase();

return invoke(BACKUP_PLUGIN.IMPORT_DATA, {
dstDir: getSaveDataDir(),
path,
});
};

/**
* 移动文件夹
* 移动数据
* @param from 源文件夹
* @param to 目标文件夹
*/
export const moveData = (from: string, to: string) => {
export const moveData = async (from: string, to: string) => {
await closeDatabase();

return invoke<string>(BACKUP_PLUGIN.MOVE_DATA, { from, to });
};

0 comments on commit cdee8a6

Please sign in to comment.