You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.1 KiB
41 lines
1.1 KiB
|
3 months ago
|
const { Sequelize } = require('sequelize');
|
||
|
|
require('dotenv').config();
|
||
|
|
|
||
|
|
// 创建数据库连接
|
||
|
|
const sequelize = new Sequelize(
|
||
|
|
process.env.DB_DATABASE || 'wechat_app',
|
||
|
|
process.env.DB_USER || 'root',
|
||
|
|
process.env.DB_PASSWORD === undefined ? null : process.env.DB_PASSWORD,
|
||
|
|
{
|
||
|
|
host: process.env.DB_HOST || 'localhost',
|
||
|
|
port: process.env.DB_PORT || 3306,
|
||
|
|
dialect: 'mysql',
|
||
|
|
timezone: '+08:00'
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
// 添加department字段到usermanagements表
|
||
|
|
async function addDepartmentColumn() {
|
||
|
|
try {
|
||
|
|
// 连接数据库
|
||
|
|
await sequelize.authenticate();
|
||
|
|
console.log('✅ 数据库连接成功');
|
||
|
|
|
||
|
|
// 使用queryInterface添加字段
|
||
|
|
await sequelize.getQueryInterface().addColumn('usermanagements', 'department', {
|
||
|
|
type: Sequelize.STRING(255),
|
||
|
|
defaultValue: null,
|
||
|
|
comment: '部门信息'
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('✅ 成功添加department字段到usermanagements表');
|
||
|
|
} catch (error) {
|
||
|
|
console.error('❌ 添加字段失败:', error.message);
|
||
|
|
} finally {
|
||
|
|
// 关闭数据库连接
|
||
|
|
await sequelize.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 执行函数
|
||
|
|
addDepartmentColumn();
|