oms/backend/database/migrations/20260911000100-create-files.js
liufumingren 8597cf5ac9 feat(backend): 用户鉴权、菜单权限、主体与系统基础模块
- 登录/邮箱验证码/密码流程(controller/auth.js、lib/emailCode.js、middleware/jwt_auth.js)
- 主体(principal)隔离与主体用户、对外接口配置模块及 principalCheck 中间件
- 菜单、用户、文件上传、快递公司字典(express_companies)等基础模块
- 通用工具:分页(lib/pagination.js)、缓存(service/cache.js)
- 全量接口契约(contract/index.js)与路由注册(router.js)、config.default.js 配置
- 相关迁移:菜单组件调整与菜单种子、创建 files 表
2026-09-13 02:23:34 +08:00

48 lines
2.3 KiB
JavaScript
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.

'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');
},
};