Files
competition-website/database/migrations/001-create-contestants.js

105 lines
2.5 KiB
JavaScript
Raw Normal View History

'use strict';
/**
* 迁移文件:创建选手表(contestants)
* 包含选手基本信息、身份认证、组别和成绩字段
*/
module.exports = {
/**
* 执行迁移:创建 contestants 表
* @param {import('sequelize').QueryInterface} queryInterface
* @param {import('sequelize').Sequelize} Sequelize
*/
up: async (queryInterface, Sequelize) => {
const { INTEGER, STRING, BOOLEAN, DATE } = Sequelize;
await queryInterface.createTable('contestants', {
// 主键 ID,自增
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '选手唯一标识',
},
// 身份证号,唯一,18位
id_number: {
type: STRING(18),
allowNull: false,
unique: true,
comment: '身份证号',
},
// 选手姓名
name: {
type: STRING(50),
allowNull: false,
comment: '选手姓名',
},
// 手机号码
phone: {
type: STRING(20),
allowNull: false,
comment: '手机号码',
},
// 参赛组别
group: {
type: STRING(20),
allowNull: false,
comment: '参赛组别(zhongzhi/gaozhi/benke/jiaoshi)',
},
// 登录密码
password: {
type: STRING(100),
allowNull: false,
defaultValue: '',
comment: '登录密码',
},
// 账号激活状态
activated: {
type: BOOLEAN,
allowNull: false,
defaultValue: false,
comment: '账号激活状态',
},
// 总分
score: {
type: INTEGER,
allowNull: true,
defaultValue: null,
comment: '选手总分',
},
// 创建时间
created_at: {
type: DATE,
allowNull: false,
comment: '创建时间',
},
// 更新时间
updated_at: {
type: DATE,
allowNull: false,
comment: '更新时间',
},
});
// 为 id_number 创建唯一索引
await queryInterface.addIndex('contestants', ['id_number'], {
unique: true,
name: 'idx_contestants_id_number',
});
// 为 group 创建索引
await queryInterface.addIndex('contestants', ['group'], {
name: 'idx_contestants_group',
});
},
/**
* 回滚迁移:删除 contestants 表
* @param {import('sequelize').QueryInterface} queryInterface
*/
down: async (queryInterface) => {
await queryInterface.dropTable('contestants');
},
};