53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
const mysql = require('mysql2/promise');
|
|
|
|
async function addFields() {
|
|
try {
|
|
// 连接到数据库
|
|
const connection = await mysql.createConnection({
|
|
host: '8.152.207.41',
|
|
user: 'rentease',
|
|
password: 'Wxx@123!',
|
|
database: 'rentease'
|
|
});
|
|
|
|
console.log('成功连接到数据库');
|
|
|
|
// 检查 subStatus 字段是否存在
|
|
const [subStatusResult] = await connection.query(`
|
|
SHOW COLUMNS FROM rooms LIKE 'subStatus'
|
|
`);
|
|
|
|
// 如果 subStatus 字段不存在,则添加
|
|
if (subStatusResult.length === 0) {
|
|
await connection.query(`
|
|
ALTER TABLE rooms ADD COLUMN subStatus ENUM('normal', 'soon_expire', 'expired') NOT NULL DEFAULT 'normal'
|
|
`);
|
|
console.log('添加 subStatus 字段成功');
|
|
} else {
|
|
console.log('subStatus 字段已存在');
|
|
}
|
|
|
|
// 检查 otherStatus 字段是否存在
|
|
const [otherStatusResult] = await connection.query(`
|
|
SHOW COLUMNS FROM rooms LIKE 'otherStatus'
|
|
`);
|
|
|
|
// 如果 otherStatus 字段不存在,则添加
|
|
if (otherStatusResult.length === 0) {
|
|
await connection.query(`
|
|
ALTER TABLE rooms ADD COLUMN otherStatus ENUM('', 'cleaning', 'maintenance') NOT NULL DEFAULT ''
|
|
`);
|
|
console.log('添加 otherStatus 字段成功');
|
|
} else {
|
|
console.log('otherStatus 字段已存在');
|
|
}
|
|
|
|
// 关闭连接
|
|
await connection.end();
|
|
console.log('数据库连接已关闭');
|
|
} catch (error) {
|
|
console.error('添加字段时出错:', error);
|
|
}
|
|
}
|
|
|
|
addFields(); |