因为去年到今年初和一些DBA做了一些大型项目,深刻的了解了数据库操作的运维与可靠性操作是非常消耗精力和时间的。所以这篇文章用来记录一些数据库的操作,方便后续直接使用。
因为今年一直在做Hermes和OpenClaw以及一些自己的开源项目,所以用到SQLite 比较多,由于深刻的认识到传统的大型多并发数据库要做好是需要非常大的精力去操作的,所以后续一些个人文章非大型场景且专题数据库的操作,都会用SQLite来进行。
重新建表,保留表中原来数据,主要为重新增加主键问题
bash
-- 直接在文件层面复制 english_learning.db
-- 建新表(带 domain,主键含领域)
CREATE TABLE MemoryNodes_new (
user_id INTEGER NOT NULL,
word_id INTEGER NOT NULL,
domain TEXT NOT NULL DEFAULT 'web_programming',
review_count INTEGER DEFAULT 1,
ease_factor REAL DEFAULT 2.5,
interval_days INTEGER DEFAULT 1,
memory_strength REAL DEFAULT 0.5,
forgetting_curve REAL DEFAULT 1.0,
next_review_at DATETIME,
memory_status TEXT DEFAULT 'learning',
difficulty_score REAL DEFAULT 0.5,
priority_score REAL DEFAULT 0.5,
is_active INTEGER DEFAULT 1,
word_root_score REAL DEFAULT 0.5,
word_affix_score REAL DEFAULT 0.5,
etymology_score REAL DEFAULT 0.5,
spelling_score REAL DEFAULT 0.5,
pos_score REAL DEFAULT 0.5,
context_score REAL DEFAULT 0.5,
frequency_score REAL DEFAULT 0.5,
consecutive_correct INTEGER DEFAULT 0,
consecutive_wrong INTEGER DEFAULT 0,
static_priority_score REAL DEFAULT 0.0,
last_reviewed_at DATETIME DEFAULT CURRENT_TIMESTAMP,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_id, domain, word_id),
FOREIGN KEY (user_id) REFERENCES Users(user_id) ON DELETE CASCADE,
FOREIGN KEY (word_id) REFERENCES Words(word_id) ON DELETE CASCADE
);
-- 迁移旧数据(存量行归到默认领域 web_programming)
INSERT INTO MemoryNodes_new
(user_id, word_id, domain, review_count, ease_factor, interval_days,
memory_strength, forgetting_curve, next_review_at, memory_status,
difficulty_score, priority_score, is_active, word_root_score, word_affix_score,
etymology_score, spelling_score, pos_score, context_score, frequency_score,
consecutive_correct, consecutive_wrong, static_priority_score,
last_reviewed_at, created_at, updated_at)
SELECT
user_id, word_id, 'web_programming', review_count, ease_factor, interval_days,
memory_strength, forgetting_curve, next_review_at, memory_status,
difficulty_score, priority_score, is_active, word_root_score, word_affix_score,
etymology_score, spelling_score, pos_score, context_score, frequency_score,
consecutive_correct, consecutive_wrong, static_priority_score,
last_reviewed_at, created_at, updated_at
FROM MemoryNodes;
-- 换表
DROP TABLE MemoryNodes;
ALTER TABLE MemoryNodes_new RENAME TO MemoryNodes;
-- 重建索引(原 idx_memory_user 已随旧表删除)
CREATE INDEX IF NOT EXISTS idx_memory_user ON MemoryNodes(user_id);
CREATE INDEX IF NOT EXISTS idx_memory_user_domain ON MemoryNodes(user_id, domain);