feat: 修复 TVBox 配置生成问题,更新数据库表名并添加迁移脚本
This commit is contained in:
@@ -0,0 +1,49 @@
|
|||||||
|
# TVBox 配置生成问题修复
|
||||||
|
|
||||||
|
## 问题描述
|
||||||
|
|
||||||
|
用户反馈 TVBox 配置生成失败,错误信息:
|
||||||
|
```
|
||||||
|
{"error":"TVBox配置生成失败","details":"D1_ERROR: no such table: admin_config: SQLITE_ERROR"}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 问题原因
|
||||||
|
|
||||||
|
这是一个数据库表名不一致的问题:
|
||||||
|
|
||||||
|
1. **SQL初始化脚本** (`scripts/d1-init.sql`):创建的表名是 `admin_configs`(复数)
|
||||||
|
2. **应用代码** (`src/lib/d1.db.ts`):查询的表名是 `admin_config`(单数)
|
||||||
|
|
||||||
|
## 修复方案
|
||||||
|
|
||||||
|
### 1. 代码修复
|
||||||
|
已修改 `src/lib/d1.db.ts` 中的 `getAdminConfig()` 和 `setAdminConfig()` 方法,使其使用正确的表名 `admin_configs`。
|
||||||
|
|
||||||
|
### 2. 数据迁移
|
||||||
|
创建了迁移脚本 `scripts/d1-migrate-admin-config.sql` 来处理现有数据。
|
||||||
|
|
||||||
|
## 部署步骤
|
||||||
|
|
||||||
|
### 对于新部署用户
|
||||||
|
直接使用最新版本部署即可,无需额外操作。
|
||||||
|
|
||||||
|
### 对于现有用户
|
||||||
|
需要运行数据迁移脚本:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 运行迁移脚本
|
||||||
|
wrangler d1 execute your-database-name --file=./scripts/d1-migrate-admin-config.sql
|
||||||
|
```
|
||||||
|
|
||||||
|
## 验证修复
|
||||||
|
修复后,TVBox 配置生成应该能正常工作:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 测试 TVBox 配置 API
|
||||||
|
curl "https://your-domain.pages.dev/api/tvbox?format=json"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 影响范围
|
||||||
|
- 仅影响使用 Cloudflare Pages + D1 部署的用户
|
||||||
|
- 其他部署方式(Docker + Redis、Vercel + Upstash 等)不受影响
|
||||||
|
- 不影响其他功能(用户认证、播放记录、收藏等)
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
-- D1 数据库迁移脚本:修复 admin_config 表名问题
|
||||||
|
-- 将旧的 admin_config 表数据迁移到新的 admin_configs 表结构
|
||||||
|
|
||||||
|
-- 首先确保新的 admin_configs 表存在
|
||||||
|
CREATE TABLE IF NOT EXISTS admin_configs (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
config_key TEXT UNIQUE NOT NULL,
|
||||||
|
config_value TEXT,
|
||||||
|
description TEXT,
|
||||||
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
|
||||||
|
-- 检查是否存在旧的 admin_config 表
|
||||||
|
-- 如果存在,迁移数据到新表
|
||||||
|
INSERT OR IGNORE INTO admin_configs (config_key, config_value, description)
|
||||||
|
SELECT
|
||||||
|
'main_config' as config_key,
|
||||||
|
config as config_value,
|
||||||
|
'从旧表迁移的主要管理员配置' as description
|
||||||
|
FROM admin_config
|
||||||
|
WHERE id = 1;
|
||||||
|
|
||||||
|
-- 插入默认管理员配置(如果不存在)
|
||||||
|
INSERT OR IGNORE INTO admin_configs (config_key, config_value, description) VALUES
|
||||||
|
('site_name', 'KatelyaTV', '站点名称'),
|
||||||
|
('site_description', '高性能影视播放平台', '站点描述'),
|
||||||
|
('enable_register', 'true', '是否允许用户注册'),
|
||||||
|
('max_users', '100', '最大用户数量'),
|
||||||
|
('cache_ttl', '3600', '缓存时间(秒)');
|
||||||
|
|
||||||
|
-- 可选:删除旧表(请谨慎使用,建议先备份数据)
|
||||||
|
-- DROP TABLE IF EXISTS admin_config;
|
||||||
+4
-3
@@ -447,7 +447,8 @@ export class D1Storage implements IStorage {
|
|||||||
try {
|
try {
|
||||||
const db = await this.getDatabase();
|
const db = await this.getDatabase();
|
||||||
const result = await db
|
const result = await db
|
||||||
.prepare('SELECT config FROM admin_config WHERE id = 1')
|
.prepare('SELECT config_value as config FROM admin_configs WHERE config_key = ? LIMIT 1')
|
||||||
|
.bind('main_config')
|
||||||
.first<{ config: string }>();
|
.first<{ config: string }>();
|
||||||
|
|
||||||
if (!result) return null;
|
if (!result) return null;
|
||||||
@@ -464,9 +465,9 @@ export class D1Storage implements IStorage {
|
|||||||
const db = await this.getDatabase();
|
const db = await this.getDatabase();
|
||||||
await db
|
await db
|
||||||
.prepare(
|
.prepare(
|
||||||
'INSERT OR REPLACE INTO admin_config (id, config) VALUES (1, ?)'
|
'INSERT OR REPLACE INTO admin_configs (config_key, config_value, description) VALUES (?, ?, ?)'
|
||||||
)
|
)
|
||||||
.bind(JSON.stringify(config))
|
.bind('main_config', JSON.stringify(config), '主要管理员配置')
|
||||||
.run();
|
.run();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to set admin config:', err);
|
console.error('Failed to set admin config:', err);
|
||||||
|
|||||||
Reference in New Issue
Block a user