d1数据迁移
This commit is contained in:
@@ -152,7 +152,18 @@ async function getUserPasswordV2(username: string): Promise<string | null> {
|
|||||||
const storage = (db as any).storage;
|
const storage = (db as any).storage;
|
||||||
if (!storage) return null;
|
if (!storage) return null;
|
||||||
|
|
||||||
// 直接调用hGetAll获取完整用户信息(包括密码)
|
// 检查存储类型
|
||||||
|
const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage';
|
||||||
|
|
||||||
|
// D1 存储:使用 getUserPasswordHash 方法
|
||||||
|
if (storageType === 'd1') {
|
||||||
|
if (typeof storage.getUserPasswordHash === 'function') {
|
||||||
|
return await storage.getUserPasswordHash(username);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Redis 存储:直接调用hGetAll获取完整用户信息(包括密码)
|
||||||
const userInfoKey = `user:${username}:info`;
|
const userInfoKey = `user:${username}:info`;
|
||||||
|
|
||||||
if (typeof storage.withRetry === 'function' && storage.client?.hgetall) {
|
if (typeof storage.withRetry === 'function' && storage.client?.hgetall) {
|
||||||
|
|||||||
@@ -104,6 +104,7 @@ export async function POST(req: NextRequest) {
|
|||||||
// 导入用户数据和user:info
|
// 导入用户数据和user:info
|
||||||
const userData = importData.data.userData;
|
const userData = importData.data.userData;
|
||||||
const storage = (db as any).storage;
|
const storage = (db as any).storage;
|
||||||
|
// 使用前面已声明的 storageType 变量
|
||||||
const usersV2Map = new Map((importData.data.usersV2 || []).map((u: any) => [u.username, u]));
|
const usersV2Map = new Map((importData.data.usersV2 || []).map((u: any) => [u.username, u]));
|
||||||
|
|
||||||
const userCount = Object.keys(userData).length;
|
const userCount = Object.keys(userData).length;
|
||||||
@@ -127,44 +128,69 @@ export async function POST(req: NextRequest) {
|
|||||||
|
|
||||||
const createdAt = userV2?.created_at || Date.now();
|
const createdAt = userV2?.created_at || Date.now();
|
||||||
|
|
||||||
// 直接设置用户信息(不经过createUserV2,避免密码被再次hash)
|
// 根据存储类型使用不同的导入方法
|
||||||
const userInfoKey = `user:${username}:info`;
|
if (storageType === 'd1') {
|
||||||
const userInfo: Record<string, string> = {
|
// D1 存储:使用 createUserWithHashedPassword 方法
|
||||||
role,
|
try {
|
||||||
banned: String(userV2?.banned || false),
|
if (typeof storage.createUserWithHashedPassword === 'function') {
|
||||||
password: user.passwordV2, // 已经是hash过的密码,直接使用
|
await storage.createUserWithHashedPassword(
|
||||||
created_at: createdAt.toString(),
|
username,
|
||||||
};
|
user.passwordV2, // 已经是hash过的密码
|
||||||
|
role,
|
||||||
|
createdAt,
|
||||||
|
userV2?.tags,
|
||||||
|
userV2?.oidcSub,
|
||||||
|
userV2?.enabledApis,
|
||||||
|
userV2?.banned
|
||||||
|
);
|
||||||
|
importedCount++;
|
||||||
|
console.log(`用户 ${username} 导入成功 (D1)`);
|
||||||
|
} else {
|
||||||
|
console.error(`D1 storage 缺少 createUserWithHashedPassword 方法`);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`导入用户 ${username} 失败:`, err);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Redis 存储:直接设置用户信息
|
||||||
|
const userInfoKey = `user:${username}:info`;
|
||||||
|
const userInfo: Record<string, string> = {
|
||||||
|
role,
|
||||||
|
banned: String(userV2?.banned || false),
|
||||||
|
password: user.passwordV2, // 已经是hash过的密码,直接使用
|
||||||
|
created_at: createdAt.toString(),
|
||||||
|
};
|
||||||
|
|
||||||
if (userV2?.tags && userV2.tags.length > 0) {
|
if (userV2?.tags && userV2.tags.length > 0) {
|
||||||
userInfo.tags = JSON.stringify(userV2.tags);
|
userInfo.tags = JSON.stringify(userV2.tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userV2?.oidcSub) {
|
||||||
|
userInfo.oidcSub = userV2.oidcSub;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userV2?.enabledApis && userV2.enabledApis.length > 0) {
|
||||||
|
userInfo.enabledApis = JSON.stringify(userV2.enabledApis);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用storage.withRetry直接设置用户信息
|
||||||
|
await storage.withRetry(() => storage.client.hSet(userInfoKey, userInfo));
|
||||||
|
|
||||||
|
// 添加到用户列表
|
||||||
|
await storage.withRetry(() => storage.client.zAdd('user:list', {
|
||||||
|
score: createdAt,
|
||||||
|
value: username,
|
||||||
|
}));
|
||||||
|
|
||||||
|
// 如果有oidcSub,创建映射
|
||||||
|
if (userV2?.oidcSub) {
|
||||||
|
const oidcSubKey = `oidc:sub:${userV2.oidcSub}`;
|
||||||
|
await storage.withRetry(() => storage.client.set(oidcSubKey, username));
|
||||||
|
}
|
||||||
|
|
||||||
|
importedCount++;
|
||||||
|
console.log(`用户 ${username} 导入成功 (Redis)`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userV2?.oidcSub) {
|
|
||||||
userInfo.oidcSub = userV2.oidcSub;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (userV2?.enabledApis && userV2.enabledApis.length > 0) {
|
|
||||||
userInfo.enabledApis = JSON.stringify(userV2.enabledApis);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 使用storage.withRetry直接设置用户信息
|
|
||||||
await storage.withRetry(() => storage.client.hSet(userInfoKey, userInfo));
|
|
||||||
|
|
||||||
// 添加到用户列表
|
|
||||||
await storage.withRetry(() => storage.client.zAdd('user:list', {
|
|
||||||
score: createdAt,
|
|
||||||
value: username,
|
|
||||||
}));
|
|
||||||
|
|
||||||
// 如果有oidcSub,创建映射
|
|
||||||
if (userV2?.oidcSub) {
|
|
||||||
const oidcSubKey = `oidc:sub:${userV2.oidcSub}`;
|
|
||||||
await storage.withRetry(() => storage.client.set(oidcSubKey, username));
|
|
||||||
}
|
|
||||||
|
|
||||||
importedCount++;
|
|
||||||
console.log(`用户 ${username} 导入成功`);
|
|
||||||
} else {
|
} else {
|
||||||
console.log(`跳过用户 ${username}:没有passwordV2`);
|
console.log(`跳过用户 ${username}:没有passwordV2`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,13 @@
|
|||||||
* 注意:此模块仅在服务端使用,通过 webpack 配置排除客户端打包
|
* 注意:此模块仅在服务端使用,通过 webpack 配置排除客户端打包
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Cloudflare D1 Database 接口
|
||||||
|
export interface D1Database {
|
||||||
|
prepare(query: string): D1PreparedStatement;
|
||||||
|
batch(statements: any[]): Promise<D1Result[]>;
|
||||||
|
exec(query: string): Promise<D1Result>;
|
||||||
|
}
|
||||||
|
|
||||||
// D1 PreparedStatement 接口
|
// D1 PreparedStatement 接口
|
||||||
export interface D1PreparedStatement {
|
export interface D1PreparedStatement {
|
||||||
bind(...values: any[]): D1PreparedStatement;
|
bind(...values: any[]): D1PreparedStatement;
|
||||||
|
|||||||
+67
-1
@@ -14,8 +14,8 @@ import {
|
|||||||
DanmakuFilterConfig,
|
DanmakuFilterConfig,
|
||||||
Notification,
|
Notification,
|
||||||
MovieRequest,
|
MovieRequest,
|
||||||
AdminConfig,
|
|
||||||
} from './types';
|
} from './types';
|
||||||
|
import { AdminConfig } from './admin.types';
|
||||||
import { DatabaseAdapter } from './d1-adapter';
|
import { DatabaseAdapter } from './d1-adapter';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -735,6 +735,72 @@ export class D1Storage implements IStorage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取用户密码哈希(用于数据导出)
|
||||||
|
async getUserPasswordHash(userName: string): Promise<string | null> {
|
||||||
|
try {
|
||||||
|
const user = await this.db
|
||||||
|
.prepare('SELECT password_hash FROM users WHERE username = ?')
|
||||||
|
.bind(userName)
|
||||||
|
.first();
|
||||||
|
|
||||||
|
return user ? (user.password_hash as string) : null;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('D1Storage.getUserPasswordHash error:', err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 直接设置用户密码哈希(用于数据导入,不进行二次哈希)
|
||||||
|
async setUserPasswordHash(userName: string, passwordHash: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
await this.db
|
||||||
|
.prepare('UPDATE users SET password_hash = ? WHERE username = ?')
|
||||||
|
.bind(passwordHash, userName)
|
||||||
|
.run();
|
||||||
|
} catch (err) {
|
||||||
|
console.error('D1Storage.setUserPasswordHash error:', err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 直接创建用户(用于数据导入,密码已经是哈希值)
|
||||||
|
async createUserWithHashedPassword(
|
||||||
|
userName: string,
|
||||||
|
passwordHash: string,
|
||||||
|
role: 'owner' | 'admin' | 'user',
|
||||||
|
createdAt: number,
|
||||||
|
tags?: string[],
|
||||||
|
oidcSub?: string,
|
||||||
|
enabledApis?: string[],
|
||||||
|
banned?: boolean
|
||||||
|
): Promise<void> {
|
||||||
|
try {
|
||||||
|
await this.db
|
||||||
|
.prepare(`
|
||||||
|
INSERT INTO users (
|
||||||
|
username, password_hash, role, banned, tags, oidc_sub,
|
||||||
|
enabled_apis, created_at, playrecord_migrated,
|
||||||
|
favorite_migrated, skip_migrated
|
||||||
|
)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1, 1, 1)
|
||||||
|
`)
|
||||||
|
.bind(
|
||||||
|
userName,
|
||||||
|
passwordHash,
|
||||||
|
role,
|
||||||
|
banned ? 1 : 0,
|
||||||
|
tags ? JSON.stringify(tags) : null,
|
||||||
|
oidcSub || null,
|
||||||
|
enabledApis ? JSON.stringify(enabledApis) : null,
|
||||||
|
createdAt
|
||||||
|
)
|
||||||
|
.run();
|
||||||
|
} catch (err) {
|
||||||
|
console.error('D1Storage.createUserWithHashedPassword error:', err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async getUserEmail?(userName: string): Promise<string | null> {
|
async getUserEmail?(userName: string): Promise<string | null> {
|
||||||
try {
|
try {
|
||||||
const result = await this.db
|
const result = await this.db
|
||||||
|
|||||||
+24
-25
@@ -49,41 +49,40 @@ function getD1Adapter(): any {
|
|||||||
// 动态导入适配器以避免客户端打包
|
// 动态导入适配器以避免客户端打包
|
||||||
const { CloudflareD1Adapter, SQLiteAdapter } = require('./d1-adapter');
|
const { CloudflareD1Adapter, SQLiteAdapter } = require('./d1-adapter');
|
||||||
|
|
||||||
|
// 检查是否为 Cloudflare 构建
|
||||||
|
const isCloudflare = process.env.CF_PAGES === '1' || process.env.BUILD_TARGET === 'cloudflare';
|
||||||
|
|
||||||
// 生产环境:Cloudflare Workers/Pages
|
// 生产环境:Cloudflare Workers/Pages
|
||||||
if (typeof process !== 'undefined' && (process as any).env?.DB) {
|
if (isCloudflare && typeof process !== 'undefined' && (process as any).env?.DB) {
|
||||||
console.log('Using Cloudflare D1 database');
|
console.log('Using Cloudflare D1 database');
|
||||||
return new CloudflareD1Adapter((process as any).env.DB);
|
return new CloudflareD1Adapter((process as any).env.DB);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 开发环境:better-sqlite3
|
// 开发环境:better-sqlite3
|
||||||
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
|
try {
|
||||||
try {
|
const Database = require('better-sqlite3');
|
||||||
const Database = require('better-sqlite3');
|
const path = require('path');
|
||||||
const path = require('path');
|
const fs = require('fs');
|
||||||
const fs = require('fs');
|
|
||||||
|
|
||||||
const dbPath = path.join(process.cwd(), '.data', 'moontv.db');
|
const dbPath = path.join(process.cwd(), '.data', 'moontv.db');
|
||||||
|
|
||||||
// 检查数据库文件是否存在
|
// 检查数据库文件是否存在
|
||||||
if (!fs.existsSync(dbPath)) {
|
if (!fs.existsSync(dbPath)) {
|
||||||
console.error('❌ SQLite database not found. Please run: npm run init:sqlite');
|
console.error('❌ SQLite database not found. Please run: npm run init:sqlite');
|
||||||
throw new Error('SQLite database not initialized');
|
throw new Error('SQLite database not initialized');
|
||||||
}
|
|
||||||
|
|
||||||
const db = new Database(dbPath);
|
|
||||||
db.pragma('journal_mode = WAL'); // 启用 WAL 模式提升性能
|
|
||||||
|
|
||||||
console.log('Using SQLite database (development mode)');
|
|
||||||
console.log('Database location:', dbPath);
|
|
||||||
|
|
||||||
return new SQLiteAdapter(db);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Failed to initialize SQLite:', err);
|
|
||||||
throw err;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error('D1 database not available. Set NEXT_PUBLIC_STORAGE_TYPE to another option or configure D1.');
|
const db = new Database(dbPath);
|
||||||
|
db.pragma('journal_mode = WAL'); // 启用 WAL 模式提升性能
|
||||||
|
|
||||||
|
console.log('Using SQLite database (development mode)');
|
||||||
|
console.log('Database location:', dbPath);
|
||||||
|
|
||||||
|
return new SQLiteAdapter(db);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to initialize SQLite:', err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 单例存储实例
|
// 单例存储实例
|
||||||
|
|||||||
Reference in New Issue
Block a user