Files
tuanwei-java/sql/20261022_add_manuscript_content_format.sql
T
weicw-dshsa 1ae2ab580d feat(manuscript): 稿件正文增加 content_format 格式标记
- cms_manuscript 新增 content_format(html/markdown/text),默认 html
- Manuscript 实体加 contentFormat;保存与修改时缺省按 html 落库,下游不再靠猜
- 迁移脚本幂等,并把明显是 markdown 的历史行标出来
2026-10-22 14:37:34 +08:00

41 lines
1.6 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 稿件正文格式标记
--
-- 背景:cms_manuscript.content 是 longtext,后台 wangEditor 存 HTML,
-- 早期从 AI 结果直接粘进来的草稿存的是 markdown(`##`、`**`、`-`)。
-- 库里的正文格式没有标记,小程序只能按内容猜,猜错就把标记语言露给用户。
-- 加上 content_format 后由写入方声明格式,读取方照标记渲染。
--
-- 幂等:重复执行只会命中 SELECT 1。
SET @db = DATABASE();
SET @sql = IF(
EXISTS(
SELECT 1 FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'cms_manuscript' AND COLUMN_NAME = 'content_format'
),
'SELECT 1',
'ALTER TABLE cms_manuscript ADD COLUMN content_format VARCHAR(16) NOT NULL DEFAULT ''html'' COMMENT ''正文格式:html/markdown/text'' AFTER content'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- 把明显是 markdown 的历史行标出来(默认 html 不动)。
-- 标成 markdown 的行不需要批量改写正文:读取方都以这列为准——
-- 小程序用 mp-html 的 markdown 插件渲染,后台编辑弹窗用 bytemd 转成 HTML 再编辑,
-- 保存后自然变成 html。这列只是把「猜格式」变成「读声明」。
UPDATE cms_manuscript
SET content_format = 'markdown'
WHERE content_format = 'html'
AND content IS NOT NULL
AND TRIM(content) <> ''
AND content NOT LIKE '<%'
AND (
content REGEXP '(^|\n)[ \t]{0,3}#{1,6}[ \t]'
OR content REGEXP '(^|\n)[ \t]{0,3}[-*+][ \t]'
OR content REGEXP '(^|\n)[ \t]{0,3}[0-9]+\\.[ \t]'
OR content REGEXP '(^|\n)[ \t]{0,3}>[ \t]'
OR content LIKE '%**%'
);