'use strict'; /** * 文档站(简易 CMS):doc_categories / doc_sections / doc_articles * * 层级:分类(顶部 tab)→ 栏目(侧边栏分组)→ 内容页(Markdown)。 * 内容保存后由 `service/docSite` 渲染成 `/doc/**` 下的**静态 HTML**,并维护 `/sitemap.txt`。 * * 迁移里先放一页「欢迎使用」,保证文档站一上线就有内容(也方便部署后自测)。 */ module.exports = { async up(queryInterface, Sequelize) { const { BIGINT, STRING, TINYINT, INTEGER, TEXT, DATE } = Sequelize; await queryInterface.createTable('doc_categories', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false }, code: { type: STRING(32), allowNull: false, unique: true, comment: 'URL 片段(/doc//)' }, name: { type: STRING(64), allowNull: false, comment: '分类名称(tab 文案)' }, description: { type: STRING(255), allowNull: true, comment: '分类说明(可选)' }, sort: { type: INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '排序(小的在前)' }, status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1 展示 0 隐藏' }, created_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') }, updated_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') }, deleted_at: { type: DATE, allowNull: true }, }, { comment: '文档分类表' }); await queryInterface.addIndex('doc_categories', [ 'status', 'sort' ], { name: 'idx_doc_categories_status_sort' }); await queryInterface.createTable('doc_sections', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false }, category_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属分类 doc_categories.id' }, name: { type: STRING(64), allowNull: false, comment: '栏目名称(侧边栏分组标题)' }, sort: { type: INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '排序(小的在前)' }, status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1 展示 0 隐藏' }, created_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') }, updated_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') }, deleted_at: { type: DATE, allowNull: true }, }, { comment: '文档栏目表' }); await queryInterface.addIndex('doc_sections', [ 'category_id', 'sort' ], { name: 'idx_doc_sections_category_sort' }); await queryInterface.createTable('doc_articles', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false }, category_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属分类(冗余,列表用)' }, section_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属栏目 doc_sections.id' }, title: { type: STRING(128), allowNull: false, comment: '标题' }, slug: { type: STRING(64), allowNull: false, comment: 'URL 片段' }, summary: { type: STRING(255), allowNull: true, comment: '摘要' }, content: { type: TEXT('medium'), allowNull: true, comment: '正文(Markdown)' }, sort: { type: INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '栏目内排序(小的在前)' }, status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1 发布 0 草稿' }, created_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') }, updated_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') }, deleted_at: { type: DATE, allowNull: true }, }, { comment: '文档内容页表' }); await queryInterface.addIndex('doc_articles', [ 'category_id', 'section_id', 'sort' ], { name: 'idx_doc_articles_tree' }); await queryInterface.addIndex('doc_articles', [ 'category_id', 'slug' ], { name: 'idx_doc_articles_slug' }); const now = new Date(); await queryInterface.bulkInsert('doc_categories', [{ code: 'guide', name: '使用指南', description: '从创建主体到看板查看,一步步把系统跑起来', sort: 10, status: 1, created_at: now, updated_at: now, }]); const [ categories ] = await queryInterface.sequelize.query( 'SELECT id FROM doc_categories WHERE code = ? LIMIT 1', { replacements: [ 'guide' ] }, ); const categoryId = categories && categories[0] ? categories[0].id : null; if (!categoryId) return; await queryInterface.bulkInsert('doc_sections', [{ category_id: categoryId, name: '快速开始', sort: 10, status: 1, created_at: now, updated_at: now, }]); const [ sections ] = await queryInterface.sequelize.query( 'SELECT id FROM doc_sections WHERE category_id = ? ORDER BY id ASC LIMIT 1', { replacements: [ categoryId ] }, ); const sectionId = sections && sections[0] ? sections[0].id : null; if (!sectionId) return; await queryInterface.bulkInsert('doc_articles', [{ category_id: categoryId, section_id: sectionId, title: '欢迎使用云仓视频溯源', slug: 'welcome', summary: '文档站已经跑起来了,这里的内容都能在「平台管理 → 文档管理」里维护。', content: [ '# 欢迎使用云仓视频溯源', '', '这里是由**平台管理 → 文档管理**维护的静态文档站:', '', '- 顶部 tab 是「分类」,点一下就能切换;', '- 左侧是分类下的「栏目 → 内容页」;', '- 文章保存后会自动重新生成 HTML 与目录,并把新链接追加到 `/sitemap.txt`。', '', '## 下一步', '', '1. 在「文档管理」里新建分类 / 栏目 / 内容页;', '2. 回到本站刷新,就能看到刚发布的内容;', '3. 需要撤下某篇时,把状态改成「草稿」即可(页面和 sitemap 都会移除)。', ].join('\n'), sort: 10, status: 1, created_at: now, updated_at: now, }]); }, async down(queryInterface) { await queryInterface.dropTable('doc_articles'); await queryInterface.dropTable('doc_sections'); await queryInterface.dropTable('doc_categories'); }, };