@@ -3,8 +3,14 @@ import { keywords, regexes } from "@/assets/data/exclude";
33import { SongUnlockServer } from "@/utils/songManager" ;
44import type { SongLevelType } from "@/types/main" ;
55import { defaultAMLLDbServer } from "@/utils/meta" ;
6+ import {
7+ CURRENT_SETTING_SCHEMA_VERSION ,
8+ migrateSettingState ,
9+ } from "./migrations/settingMigrations" ;
610
711export interface SettingState {
12+ /** Schema 版本号(可选,用于数据迁移) */
13+ schemaVersion ?: number ;
814 /** 明暗模式 */
915 themeMode : "light" | "dark" | "auto" ;
1016 /** 主题类别 */
@@ -243,6 +249,7 @@ export interface SettingState {
243249
244250export 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 // 若未传入
0 commit comments