'use strict'; /** * 上传文件登记表(file_records) * * 每次上传成功都会登记一行:COS 对象键、访问地址、大小、类型、上传者。 * **引用关系不单独存表**:管理端查询时实时判断(用户头像 / 文档正文), * 这样业务侧改动(改头像、删文档)不需要同步维护引用行,也不会出现脏引用。 * * 扩展新的引用来源时,只需在 `service/fileStorage` 的 REFERENCE_SOURCES 里加一条 EXISTS 片段。 */ module.exports = { async up(queryInterface, Sequelize) { const { BIGINT, STRING, DATE } = Sequelize; await queryInterface.createTable('file_records', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false }, object_key: { type: STRING(512), allowNull: false, comment: 'COS 对象键(相对路径,唯一)' }, url: { type: STRING(512), allowNull: false, comment: '对外访问地址(CDN / COS 域名 + 对象键)' }, file_name: { type: STRING(255), allowNull: false, defaultValue: '', comment: '上传时的原始文件名' }, size: { type: BIGINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '文件大小(字节)' }, mime_type: { type: STRING(100), allowNull: false, defaultValue: '', comment: 'MIME 类型' }, hash: { type: STRING(64), allowNull: false, defaultValue: '', comment: '内容 SHA256(校验 / 排查重复)' }, category: { type: STRING(32), allowNull: false, defaultValue: '', comment: '业务分类:avatar 头像 / doc 文档图片' }, uploader_id: { type: BIGINT.UNSIGNED, allowNull: true, comment: '上传者 users.id' }, uploader_name: { type: STRING(64), allowNull: false, defaultValue: '', comment: '上传者快照(昵称/用户名)' }, 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: '上传文件登记表(COS 对象与引用追踪)' }); await queryInterface.addIndex('file_records', [ 'object_key' ], { name: 'uk_file_records_key', unique: true, }); await queryInterface.addIndex('file_records', [ 'category', 'created_at' ], { name: 'idx_file_records_category_created', }); await queryInterface.addIndex('file_records', [ 'uploader_id' ], { name: 'idx_file_records_uploader' }); }, async down(queryInterface) { await queryInterface.dropTable('file_records'); }, };