数据迁移支持新用户
This commit is contained in:
@@ -328,6 +328,31 @@ export async function getConfig(): Promise<AdminConfig> {
|
||||
}
|
||||
adminConfig = configSelfCheck(adminConfig);
|
||||
cachedConfig = adminConfig;
|
||||
|
||||
// 自动迁移用户(如果配置中有用户且V2存储支持)
|
||||
// 过滤掉站长后检查是否有需要迁移的用户
|
||||
const nonOwnerUsers = adminConfig.UserConfig.Users.filter(
|
||||
(u) => u.username !== process.env.USERNAME
|
||||
);
|
||||
if (!dbReadFailed && nonOwnerUsers.length > 0) {
|
||||
try {
|
||||
// 检查是否支持V2存储
|
||||
const storage = (db as any).storage;
|
||||
if (storage && typeof storage.createUserV2 === 'function') {
|
||||
console.log('检测到配置中有用户,开始自动迁移...');
|
||||
await db.migrateUsersFromConfig(adminConfig);
|
||||
// 迁移完成后,清空配置中的用户列表并保存
|
||||
adminConfig.UserConfig.Users = [];
|
||||
await db.saveAdminConfig(adminConfig);
|
||||
cachedConfig = adminConfig;
|
||||
console.log('用户自动迁移完成');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('自动迁移用户失败:', error);
|
||||
// 不影响主流程,继续执行
|
||||
}
|
||||
}
|
||||
|
||||
return cachedConfig;
|
||||
}
|
||||
|
||||
|
||||
@@ -234,6 +234,7 @@ export class DbManager {
|
||||
role: 'owner' | 'admin' | 'user';
|
||||
banned: boolean;
|
||||
tags?: string[];
|
||||
oidcSub?: string;
|
||||
enabledApis?: string[];
|
||||
created_at: number;
|
||||
}>;
|
||||
|
||||
@@ -504,6 +504,7 @@ export class UpstashRedisStorage implements IStorage {
|
||||
role: 'owner' | 'admin' | 'user';
|
||||
banned: boolean;
|
||||
tags?: string[];
|
||||
oidcSub?: string;
|
||||
enabledApis?: string[];
|
||||
created_at: number;
|
||||
}>;
|
||||
@@ -528,6 +529,7 @@ export class UpstashRedisStorage implements IStorage {
|
||||
role: 'owner' as const,
|
||||
banned: ownerInfo.banned,
|
||||
tags: ownerInfo.tags,
|
||||
oidcSub: ownerInfo.oidcSub,
|
||||
enabledApis: ownerInfo.enabledApis,
|
||||
created_at: ownerInfo.created_at,
|
||||
});
|
||||
@@ -549,6 +551,7 @@ export class UpstashRedisStorage implements IStorage {
|
||||
role: userInfo.role,
|
||||
banned: userInfo.banned,
|
||||
tags: userInfo.tags,
|
||||
oidcSub: userInfo.oidcSub,
|
||||
enabledApis: userInfo.enabledApis,
|
||||
created_at: userInfo.created_at,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user