Skip to content
3 changes: 2 additions & 1 deletion src/app/api/admin/config_file/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { NextRequest, NextResponse } from 'next/server';

import { getAuthInfoFromCookie } from '@/lib/auth';
import { getConfig, refineConfig } from '@/lib/config';
import { getConfig, refineConfig, setCachedConfig } from '@/lib/config';
import { db } from '@/lib/db';

export const runtime = 'nodejs';
Expand Down Expand Up @@ -79,6 +79,7 @@ export async function POST(request: NextRequest) {
adminConfig = refineConfig(adminConfig);
// 更新配置文件
await db.saveAdminConfig(adminConfig);
await setCachedConfig(adminConfig);

// 清除短剧视频源缓存(因为配置文件可能包含新的视频源)
try {
Expand Down
12 changes: 10 additions & 2 deletions src/lib/d1.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,20 @@ import { userInfoCache } from './user-cache';
*/
export class D1Storage implements IStorage {
private db: DatabaseAdapter;
private schemaReady: Promise<void>;
private _schemaReady: Promise<void> | null = null;
private get schemaReady(): Promise<void> {
if (!this._schemaReady) {
this._schemaReady = this.ensureMangaShelfColumns().catch((err) => {
this._schemaReady = null;
throw err;
});
}
return this._schemaReady;
}
Comment on lines +45 to +53

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

由于 ensureMangaShelfColumns 方法内部捕获了所有异常且未向上抛出,因此 schemaReady getter 中的 .catch 分支实际上永远不会被触发。如果数据库在首次请求时由于临时网络抖动或未就绪而导致列添加失败,_schemaReady 仍会被缓存为一个已成功解析的 Promise。这会导致后续所有对漫画书架的访问都默认列已存在,从而引发持续的数据库错误,且无法自动重试。\n\n建议修改 ensureMangaShelfColumns,使其在遇到非预期错误(如连接失败等非“列已存在”的错误)时向上抛出异常,以便 lazy getter 能够正确重置 _schemaReady 并在下一次请求时重试。

public adapter: RedisHashAdapter;

constructor(adapter: DatabaseAdapter) {
this.db = adapter;
this.schemaReady = this.ensureMangaShelfColumns();
// 创建 Redis Hash 兼容适配器用于设备管理
this.adapter = new RedisHashAdapter(adapter);
}
Expand Down
12 changes: 10 additions & 2 deletions src/lib/postgres.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@ import {
*/
export class PostgresStorage implements IStorage {
private db: DatabaseAdapter;
private schemaReady: Promise<void>;
private _schemaReady: Promise<void> | null = null;
private get schemaReady(): Promise<void> {
if (!this._schemaReady) {
this._schemaReady = this.ensureMangaShelfColumns().catch((err) => {
this._schemaReady = null;
throw err;
});
}
return this._schemaReady;
}
Comment on lines +46 to +54

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

由于 ensureMangaShelfColumns 方法内部捕获了所有异常且未向上抛出,因此 schemaReady getter 中的 .catch 分支实际上永远不会被触发。如果数据库在首次请求时由于临时网络抖动或未就绪而导致列添加失败,_schemaReady 仍会被缓存为一个已成功解析的 Promise。这会导致后续所有对漫画书架的访问都默认列已存在,从而引发持续的数据库错误,且无法自动重试。\n\n建议修改 ensureMangaShelfColumns,使其在遇到非预期错误(如连接失败等非“列已存在”的错误)时向上抛出异常,以便 lazy getter 能够正确重置 _schemaReady 并在下一次请求时重试。

public adapter: any; // 用于兼容

constructor(adapter: DatabaseAdapter) {
this.db = adapter;
this.schemaReady = this.ensureMangaShelfColumns();
// 创建一个简单的适配器用于设备管理
this.adapter = new PostgresRedisHashAdapter(adapter);
}
Expand Down