This commit is contained in:
chenqiang 2025-08-12 11:29:50 +08:00
parent 1338a1b59d
commit 05edaccebb
3 changed files with 27 additions and 40 deletions

View File

@ -1,5 +1,6 @@
import path from 'path';
import fs from 'fs';
import { app } from 'electron';
import { getSystemDbPath, getUserDbPath } from './path.js';
// 迁移数据库文件
@ -21,14 +22,26 @@ export async function migrateDatabases() {
console.log(`创建数据目录: ${dataDir}`);
}
// 检查数据库文件是否存在
if (fs.existsSync(systemDbPath)) {
// 获取安装目录中的数据库文件路径
const appPath = app.getAppPath();
const installedSystemDbPath = path.join(appPath, 'data', 'system.db');
const installedUserDbPath = path.join(appPath, 'data', 'user.db');
// 检查并复制系统数据库文件
if (fs.existsSync(installedSystemDbPath) && !fs.existsSync(systemDbPath)) {
fs.copyFileSync(installedSystemDbPath, systemDbPath);
console.log(`复制系统数据库: ${installedSystemDbPath} -> ${systemDbPath}`);
} else if (fs.existsSync(systemDbPath)) {
console.log(`系统数据库已存在: ${systemDbPath}`);
} else {
console.log(`系统数据库不存在: ${systemDbPath}`);
}
if (fs.existsSync(userDbPath)) {
// 检查并复制用户数据库文件
if (fs.existsSync(installedUserDbPath) && !fs.existsSync(userDbPath)) {
fs.copyFileSync(installedUserDbPath, userDbPath);
console.log(`复制用户数据库: ${installedUserDbPath} -> ${userDbPath}`);
} else if (fs.existsSync(userDbPath)) {
console.log(`用户数据库已存在: ${userDbPath}`);
} else {
console.log(`用户数据库不存在: ${userDbPath}`);

View File

@ -1,42 +1,16 @@
// 将 CommonJS 导入改为 ES 模块导入
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
// 导入app模块
import { app } from 'electron';
import path from 'path';
// 获取当前文件所在目录
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// 获取用户数据目录
const userDataPath = app.getPath('userData');
const dataDir = path.join(userDataPath, 'data');
// 获取用户数据目录(跨平台解决方案)
const getUserDataPath = () => {
return app.getPath('userData');
};
// 确保数据目录存在
const ensureDataDirExists = () => {
const dataDir = path.join(getUserDataPath(), 'data');
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
return dataDir;
};
// 获取系统数据库路径
const getSystemDbPath = () => {
const dataDir = ensureDataDirExists();
// 系统数据库路径
export function getSystemDbPath() {
return path.join(dataDir, 'system.db');
};
}
// 获取用户数据库路径
const getUserDbPath = () => {
const dataDir = ensureDataDirExists();
// 用户数据库路径
export function getUserDbPath() {
return path.join(dataDir, 'user.db');
};
// 导出函数
export {
getSystemDbPath,
getUserDbPath
};
}

View File

@ -2,7 +2,7 @@
import { app, BrowserWindow, ipcMain, dialog } from "electron";
import path from "path";
import { fileURLToPath } from "url";
import { migrateDatabases } from "./db/migration.js";
import { migrateDatabases } from './db/migration.js';
import {
checkDatabaseInitialized,
initializeDatabase,