Files
xiaoxia-saas/migrations/009_merge_title_library_into_scripts.sql
T

42 lines
2.0 KiB
SQL

-- #1894 废弃标题库整合到文案库
-- scripts 表新增 title_text / title_category / title_config 字段
-- 从 title_libraries 迁移现有数据到 scripts
-- 创建时间: 2026-09-15
-- 1. scripts 表新增字段
ALTER TABLE scripts ADD COLUMN IF NOT EXISTS title_text VARCHAR(500) NOT NULL DEFAULT '';
ALTER TABLE scripts ADD COLUMN IF NOT EXISTS title_category VARCHAR(50) NOT NULL DEFAULT 'default';
ALTER TABLE scripts ADD COLUMN IF NOT EXISTS title_config JSON NOT NULL DEFAULT '{}';
CREATE INDEX IF NOT EXISTS idx_scripts_title_category ON scripts(title_category);
-- 2. 数据迁移:将 title_libraries 中的活跃数据迁移到 scripts
-- 映射关系:
-- title_libraries.name → scripts.title (文案库标题)
-- title_libraries.text → scripts.title_text (标题文本)
-- title_libraries.category → scripts.title_category
-- title_libraries.extra_meta → scripts.title_config (标题样式配置)
-- title_libraries.tags → scripts.tags
-- scripts.content → 空字符串(纯标题无口播内容)
-- scripts.segments → 空数组
--
-- 使用 'title_library_migration' 标签标记迁移来源,便于追溯
INSERT INTO scripts (id, user_id, title, content, segments, tags, title_text, title_category, title_config, created_at, updated_at)
SELECT
tl.id,
tl.user_id,
tl.name, -- title = 标题库的 name
'', -- content 留空
'[]'::jsonb, -- segments 留空
COALESCE(tl.tags, '[]'::jsonb) || '["title_library_migration"]'::jsonb, -- 追加迁移标签
COALESCE(tl.text, ''), -- title_text
COALESCE(tl.category, 'default'), -- title_category
COALESCE(tl.metadata, '{}'::jsonb), -- title_config (原 extra_meta)
tl.created_at,
tl.updated_at
FROM title_libraries tl
WHERE tl.is_active = TRUE
AND NOT EXISTS (
SELECT 1 FROM scripts s WHERE s.id = tl.id
);