'use strict'; /** * 文件资源表 files: * 1) 记录上传文件的大小、原始名称、内容 hash 与存储路径,便于按 hash 去重与审计 * 2) 文件按主体区分:同一主体下 hash 唯一(uk_files_principal_hash),不同主体互不影响 * 3) 磁盘目录按「主体ID/年/月/日」分层,由上传时间决定(见 app/controller/file.js) */ module.exports = { async up(queryInterface, Sequelize) { await queryInterface.createTable( 'files', { id: { type: Sequelize.BIGINT.UNSIGNED, autoIncrement: true, primaryKey: true, comment: '主键' }, principal_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID', references: { model: 'principals', key: 'id' }, onDelete: 'CASCADE', }, hash: { type: Sequelize.STRING(64), allowNull: false, comment: '文件内容 SHA256,用于去重' }, name: { type: Sequelize.STRING(255), allowNull: false, comment: '原始文件名' }, ext: { type: Sequelize.STRING(20), allowNull: false, comment: '扩展名(小写,含点,如 .png)' }, mime: { type: Sequelize.STRING(100), comment: 'MIME 类型' }, size: { type: Sequelize.INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '文件大小(字节)' }, path: { type: Sequelize.STRING(255), allowNull: false, comment: '相对存储路径:主体ID/年/月/日/hash.ext' }, url: { type: Sequelize.STRING(500), allowNull: false, comment: '访问地址' }, deleted_at: { type: Sequelize.DATE, comment: '软删除时间' }, created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') }, updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP') }, }, { charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '文件资源' } ); // 主体内按内容去重:同一份文件重复上传时直接复用已有记录与 URL await queryInterface.addIndex('files', { fields: [ 'principal_id', 'hash' ], unique: true, name: 'uk_files_principal_hash', }); }, async down(queryInterface) { await queryInterface.dropTable('files'); }, };