Skip to content

Commit 403367e

Browse files
committed
✨ feat: 配置系统支持迁移
1 parent 2959737 commit 403367e

3 files changed

Lines changed: 91 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import type { SettingState } from "../setting";
2+
import { defaultAMLLDbServer } from "@/utils/meta";
3+
4+
/**
5+
* 当前设置 Schema 版本号
6+
*/
7+
export const CURRENT_SETTING_SCHEMA_VERSION = 1;
8+
9+
/**
10+
* 迁移函数类型
11+
* 迁移函数只需返回需要更新的字段,系统会自动合并到原有状态
12+
*/
13+
export type MigrationFunction = (state: Partial<SettingState>) => Partial<SettingState>;
14+
15+
/**
16+
* 迁移脚本映射表
17+
* key: 目标版本号
18+
* value: 从上一版本迁移到该版本的函数
19+
*/
20+
export const settingMigrations: Record<number, MigrationFunction> = {
21+
/**
22+
* 迁移到版本 1
23+
*/
24+
1: () => {
25+
return {
26+
schemaVersion: 1,
27+
// amllDbServer 同步为新的默认值
28+
amllDbServer: defaultAMLLDbServer,
29+
};
30+
},
31+
};
32+
33+
/**
34+
* 执行迁移
35+
* @param state 当前状态
36+
* @param fromVersion 当前版本号(如果不存在则为 0)
37+
* @param toVersion 目标版本号
38+
* @returns 迁移后的状态
39+
*/
40+
export const migrateSettingState = (
41+
state: Partial<SettingState>,
42+
fromVersion: number,
43+
toVersion: number,
44+
): Partial<SettingState> => {
45+
let migratedState = { ...state };
46+
47+
// 按版本顺序执行迁移
48+
for (let version = fromVersion + 1; version <= toVersion; version++) {
49+
const migration = settingMigrations[version];
50+
if (migration) {
51+
// 迁移函数返回需要更新的字段,自动合并到原有状态
52+
const updates = migration(migratedState);
53+
migratedState = { ...migratedState, ...updates };
54+
} else {
55+
console.warn(`[Setting Migration] 未找到版本 ${version} 的迁移脚本,跳过`);
56+
}
57+
}
58+
59+
return migratedState;
60+
};

src/stores/setting.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ import { keywords, regexes } from "@/assets/data/exclude";
33
import { SongUnlockServer } from "@/utils/songManager";
44
import type { SongLevelType } from "@/types/main";
55
import { defaultAMLLDbServer } from "@/utils/meta";
6+
import {
7+
CURRENT_SETTING_SCHEMA_VERSION,
8+
migrateSettingState,
9+
} from "./migrations/settingMigrations";
610

711
export interface SettingState {
12+
/** Schema 版本号(可选,用于数据迁移) */
13+
schemaVersion?: number;
814
/** 明暗模式 */
915
themeMode: "light" | "dark" | "auto";
1016
/** 主题类别 */
@@ -243,6 +249,7 @@ export interface SettingState {
243249

244250
export const useSettingStore = defineStore("setting", {
245251
state: (): SettingState => ({
252+
schemaVersion: CURRENT_SETTING_SCHEMA_VERSION,
246253
themeMode: "auto",
247254
themeColorType: "default",
248255
themeCustomColor: "#fe7971",
@@ -371,6 +378,27 @@ export const useSettingStore = defineStore("setting", {
371378
},
372379
},
373380
actions: {
381+
/**
382+
* 检查并执行数据迁移
383+
* 应在应用启动时调用
384+
*/
385+
checkAndMigrate() {
386+
const currentVersion = this.schemaVersion ?? 0;
387+
const targetVersion = CURRENT_SETTING_SCHEMA_VERSION;
388+
389+
if (currentVersion !== targetVersion) {
390+
console.log(`[Setting Migration] 检测到版本差异: ${currentVersion} -> ${targetVersion}`);
391+
// 保存当前完整状态
392+
const currentState = { ...this.$state } as Partial<SettingState>;
393+
// 执行迁移,保留所有原有字段,只更新需要的字段
394+
const migratedState = migrateSettingState(currentState, currentVersion, targetVersion);
395+
// 应用迁移后的状态
396+
Object.assign(this, migratedState);
397+
// 确保版本号已更新
398+
this.schemaVersion = targetVersion;
399+
console.log(`[Setting Migration] 迁移完成,已更新到版本 ${targetVersion}`);
400+
}
401+
},
374402
// 更换明暗模式
375403
setThemeMode(mode?: "auto" | "light" | "dark") {
376404
// 若未传入

src/utils/init.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const init = async () => {
1616
const settingStore = useSettingStore();
1717
const shortcutStore = useShortcutStore();
1818

19+
// 检查并执行设置迁移
20+
settingStore.checkAndMigrate();
21+
1922
printVersion();
2023

2124
// 用户协议

0 commit comments

Comments
 (0)