feat: delete d1 init

This commit is contained in:
shinya
2025-07-14 12:33:34 +08:00
parent 7ee0edb419
commit 590ef4dd6e
2 changed files with 75 additions and 103 deletions

View File

@@ -44,117 +44,14 @@ function getD1Database(): D1Database {
export class D1Storage implements IStorage {
private db: D1Database | null = null;
private initialized = false;
private async getDatabase(): Promise<D1Database> {
if (!this.db) {
this.db = getD1Database();
if (!this.initialized) {
await this.initDatabase();
this.initialized = true;
}
}
return this.db;
}
private async initDatabase() {
try {
if (!this.db) {
throw new Error('D1 database instance not available');
}
console.log('Executing D1 database initialization SQL...');
// 将初始化SQL分解为单独的语句
const statements = [
`CREATE TABLE IF NOT EXISTS users (
username TEXT PRIMARY KEY,
password TEXT NOT NULL,
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now'))
)`,
`CREATE TABLE IF NOT EXISTS play_records (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
key TEXT NOT NULL,
title TEXT NOT NULL,
source_name TEXT NOT NULL,
cover TEXT NOT NULL,
year TEXT NOT NULL,
index_episode INTEGER NOT NULL,
total_episodes INTEGER NOT NULL,
play_time INTEGER NOT NULL,
total_time INTEGER NOT NULL,
save_time INTEGER NOT NULL,
search_title TEXT,
UNIQUE(username, key)
)`,
`CREATE TABLE IF NOT EXISTS favorites (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
key TEXT NOT NULL,
title TEXT NOT NULL,
source_name TEXT NOT NULL,
cover TEXT NOT NULL,
year TEXT NOT NULL,
total_episodes INTEGER NOT NULL,
save_time INTEGER NOT NULL,
UNIQUE(username, key)
)`,
`CREATE TABLE IF NOT EXISTS search_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
keyword TEXT NOT NULL,
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),
UNIQUE(username, keyword)
)`,
`CREATE TABLE IF NOT EXISTS admin_config (
id INTEGER PRIMARY KEY DEFAULT 1,
config TEXT NOT NULL,
updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now'))
)`,
// 基本索引
`CREATE INDEX IF NOT EXISTS idx_play_records_username ON play_records(username)`,
`CREATE INDEX IF NOT EXISTS idx_favorites_username ON favorites(username)`,
`CREATE INDEX IF NOT EXISTS idx_search_history_username ON search_history(username)`,
// 复合索引
`CREATE INDEX IF NOT EXISTS idx_play_records_username_key ON play_records(username, key)`,
`CREATE INDEX IF NOT EXISTS idx_play_records_username_save_time ON play_records(username, save_time DESC)`,
`CREATE INDEX IF NOT EXISTS idx_favorites_username_key ON favorites(username, key)`,
`CREATE INDEX IF NOT EXISTS idx_favorites_username_save_time ON favorites(username, save_time DESC)`,
`CREATE INDEX IF NOT EXISTS idx_search_history_username_keyword ON search_history(username, keyword)`,
`CREATE INDEX IF NOT EXISTS idx_search_history_username_created_at ON search_history(username, created_at DESC)`,
`CREATE INDEX IF NOT EXISTS idx_search_history_username_id_created_at ON search_history(username, id, created_at DESC)`,
];
// 逐个执行每个SQL语句
for (let i = 0; i < statements.length; i++) {
const statement = statements[i];
try {
console.log(
`Executing SQL statement ${i + 1}/${statements.length}:`,
statement.substring(0, 50) + '...'
);
await this.db.prepare(statement).run();
} catch (err) {
console.error(`Failed to execute statement ${i + 1}:`, statement);
console.error('Error:', err);
throw err;
}
}
console.log('D1 database initialization completed successfully');
} catch (err) {
console.error('Failed to initialize D1 database:', err);
throw err;
}
}
// 播放记录相关
async getPlayRecord(
userName: string,