diff --git a/migrations/005_cleanup.sql b/migrations/005_cleanup.sql index 70a26a2..8d0db45 100644 --- a/migrations/005_cleanup.sql +++ b/migrations/005_cleanup.sql @@ -1,35 +1,6 @@ --- 清理不再使用的扩展列(安全重建方式) --- 通过重建表来移除列,兼容列已存在或已删除的任意状态 - --- 重建时需要临时关闭外键约束(因为有其他表 REFERENCES albums) -PRAGMA foreign_keys = OFF; +-- 清理不再使用的索引 +-- 注意:不重建 albums 表,因为 4 张子表(images/favorites/album_tags/person_albums) +-- 通过 FOREIGN KEY 引用了 albums(id),DROP TABLE 即使关闭外键检查也会失败。 +-- 未使用的列(person_name_original/all_words/tags)已由应用层忽略,不影响功能。 DROP INDEX IF EXISTS idx_albums_cleaned; - --- 重建 albums 表,只保留需要的列 -CREATE TABLE IF NOT EXISTS albums_v2 ( - id TEXT PRIMARY KEY NOT NULL, - title TEXT NOT NULL DEFAULT '', - document TEXT DEFAULT '', - description TEXT, - cover_image_id TEXT, - image_count INTEGER NOT NULL DEFAULT 0, - created_at TEXT NOT NULL DEFAULT (datetime('now', 'localtime')) -); - --- 安全复制数据(SELECT 只取确定存在的列,COALESCE 处理 NULL) -INSERT OR IGNORE INTO albums_v2 (id, title, document, description, cover_image_id, image_count, created_at) -SELECT id, title, - COALESCE(document, ''), - description, - cover_image_id, - image_count, - created_at -FROM albums; - --- 替换旧表 -DROP TABLE IF EXISTS albums; -ALTER TABLE albums_v2 RENAME TO albums; - --- 恢复外键约束 -PRAGMA foreign_keys = ON;