'use strict'; /** * 协议 / 协议版本 / 用户协议同意记录 * * 协议内容没有管理界面:新增版本直接走 SQL(见 backend/README「用户协议」一节), * 这里只负责三张表的结构与索引。 */ module.exports = { async up(queryInterface, Sequelize) { const { BIGINT, STRING, TINYINT, INTEGER, TEXT, DATE } = Sequelize; await queryInterface.createTable('agreements', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false }, code: { type: STRING(64), allowNull: false, unique: true, comment: '协议标识(user_agreement 等)' }, name: { type: STRING(128), allowNull: false, comment: '协议名称(展示用)' }, description: { type: STRING(255), allowNull: true, comment: '简介' }, required: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '登录是否必须同意:1是 0否' }, status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1启用 0停用' }, sort_no: { type: INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '展示排序' }, // 不建外键:agreements ↔ agreement_versions 互相引用,建表顺序会互相依赖 current_version_id: { type: BIGINT.UNSIGNED, allowNull: true, comment: '当前生效版本 agreement_versions.id' }, 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('agreements', [ 'status' ], { name: 'idx_agreements_status' }); await queryInterface.createTable('agreement_versions', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false }, agreement_id: { type: BIGINT.UNSIGNED, allowNull: false, references: { model: 'agreements', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'RESTRICT', comment: 'agreements.id', }, version: { type: STRING(32), allowNull: false, comment: '版本号(1.0.0)' }, title: { type: STRING(128), allowNull: false, comment: '版本标题' }, summary: { type: STRING(255), allowNull: true, comment: '变更摘要' }, content: { type: TEXT('long'), allowNull: false, comment: '协议正文' }, content_type: { type: STRING(16), allowNull: false, defaultValue: 'text', comment: '正文类型:text / html' }, status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:0草稿 1已发布 2已归档' }, published_at: { type: DATE, allowNull: true, comment: '发布时间' }, effective_at: { type: DATE, allowNull: true, 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: '协议版本表' }); await queryInterface.addIndex('agreement_versions', [ 'agreement_id', 'version' ], { name: 'uk_agreement_versions_agreement_version', unique: true, }); await queryInterface.addIndex('agreement_versions', [ 'status' ], { name: 'idx_agreement_versions_status' }); await queryInterface.createTable('user_agreements', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false }, user_id: { type: BIGINT.UNSIGNED, allowNull: false, references: { model: 'users', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'CASCADE', comment: 'users.id', }, agreement_id: { type: BIGINT.UNSIGNED, allowNull: false, references: { model: 'agreements', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'RESTRICT', comment: 'agreements.id', }, version_id: { type: BIGINT.UNSIGNED, allowNull: false, references: { model: 'agreement_versions', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'RESTRICT', comment: 'agreement_versions.id', }, version: { type: STRING(32), allowNull: false, comment: '同意时的版本号快照' }, source: { type: STRING(32), allowNull: true, comment: '来源:mini_program / web / h5' }, ip: { type: STRING(64), allowNull: true, comment: '同意时的客户端 IP' }, user_agent: { type: STRING(255), allowNull: true, comment: '同意时的 User-Agent' }, agreed_at: { type: DATE, allowNull: false, 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: '用户协议同意记录表' }); await queryInterface.addIndex('user_agreements', [ 'user_id', 'agreement_id', 'version_id' ], { name: 'uk_user_agreements_user_agreement_version', unique: true, }); await queryInterface.addIndex('user_agreements', [ 'user_id' ], { name: 'idx_user_agreements_user' }); await queryInterface.addIndex('user_agreements', [ 'agreement_id' ], { name: 'idx_user_agreements_agreement' }); }, async down(queryInterface) { await queryInterface.dropTable('user_agreements'); await queryInterface.dropTable('agreement_versions'); await queryInterface.dropTable('agreements'); }, };