Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 移动数据前先关闭数据库连接 #512

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 });
};