feat(设备字段): 添加系统字段保护机制
为设备字段添加isSystem属性标记系统字段,防止误删核心字段 后端添加字段删除前的系统字段检查 前端在管理界面标记并禁用系统字段的删除操作 更新导入模板生成逻辑以适配动态字段配置 优化设备导入功能,支持字段名称变更
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
const { sequelize } = require('../db');
|
||||
|
||||
async function addIsSystemColumn() {
|
||||
try {
|
||||
console.log('开始添加 isSystem 列到 deviceFields 表...');
|
||||
|
||||
// 检查列是否已存在
|
||||
const tableInfo = await sequelize.query(
|
||||
"PRAGMA table_info(deviceFields)",
|
||||
{ type: sequelize.QueryTypes.SELECT }
|
||||
);
|
||||
|
||||
const hasIsSystemColumn = tableInfo.some(col => col.name === 'isSystem');
|
||||
|
||||
if (hasIsSystemColumn) {
|
||||
console.log('isSystem 列已存在,跳过添加');
|
||||
} else {
|
||||
// 添加 isSystem 列
|
||||
await sequelize.query(
|
||||
"ALTER TABLE deviceFields ADD COLUMN isSystem BOOLEAN DEFAULT 0",
|
||||
{ type: sequelize.QueryTypes.RAW }
|
||||
);
|
||||
console.log('isSystem 列添加成功');
|
||||
}
|
||||
|
||||
// 更新现有数据:将核心字段标记为系统字段
|
||||
const systemFields = [
|
||||
'deviceId', 'name', 'type', 'model', 'serialNumber',
|
||||
'rackId', 'position', 'height', 'powerConsumption',
|
||||
'status', 'purchaseDate', 'warrantyExpiry',
|
||||
'ipAddress', 'description'
|
||||
];
|
||||
|
||||
for (const fieldName of systemFields) {
|
||||
await sequelize.query(
|
||||
`UPDATE deviceFields SET isSystem = 1 WHERE fieldName = '${fieldName}'`,
|
||||
{ type: sequelize.QueryTypes.RAW }
|
||||
);
|
||||
}
|
||||
console.log('系统字段标记完成');
|
||||
|
||||
console.log('数据库迁移完成');
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error('迁移失败:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
addIsSystemColumn();
|
||||
@@ -0,0 +1,55 @@
|
||||
const { sequelize } = require('../db');
|
||||
|
||||
async function updateSystemFields() {
|
||||
try {
|
||||
console.log('开始调整系统字段标记...');
|
||||
|
||||
// 核心系统字段(数据库必填字段,不可删除)
|
||||
const coreSystemFields = [
|
||||
'deviceId', 'name', 'type', 'model', 'serialNumber',
|
||||
'rackId', 'position', 'height', 'powerConsumption',
|
||||
'status', 'purchaseDate', 'warrantyExpiry'
|
||||
];
|
||||
|
||||
// 可选字段(非系统字段,可删除)
|
||||
const optionalFields = [
|
||||
'ipAddress', 'description', 'owner', 'department', 'assetId', 'brand'
|
||||
];
|
||||
|
||||
// 先将所有字段设为非系统字段
|
||||
await sequelize.query(
|
||||
`UPDATE deviceFields SET isSystem = 0`,
|
||||
{ type: sequelize.QueryTypes.RAW }
|
||||
);
|
||||
console.log('已重置所有字段为非系统字段');
|
||||
|
||||
// 标记核心系统字段
|
||||
for (const fieldName of coreSystemFields) {
|
||||
await sequelize.query(
|
||||
`UPDATE deviceFields SET isSystem = 1 WHERE fieldName = '${fieldName}'`,
|
||||
{ type: sequelize.QueryTypes.RAW }
|
||||
);
|
||||
console.log(`标记为核心系统字段: ${fieldName}`);
|
||||
}
|
||||
|
||||
// 确保可选字段为非系统字段
|
||||
for (const fieldName of optionalFields) {
|
||||
await sequelize.query(
|
||||
`UPDATE deviceFields SET isSystem = 0 WHERE fieldName = '${fieldName}'`,
|
||||
{ type: sequelize.QueryTypes.RAW }
|
||||
);
|
||||
console.log(`标记为可选字段: ${fieldName}`);
|
||||
}
|
||||
|
||||
console.log('\n系统字段调整完成!');
|
||||
console.log('核心系统字段(不可删除):', coreSystemFields.join(', '));
|
||||
console.log('可选字段(可删除):', optionalFields.join(', '));
|
||||
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error('更新失败:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
updateSystemFields();
|
||||
@@ -0,0 +1,32 @@
|
||||
const { sequelize } = require('../db');
|
||||
|
||||
async function updateSystemFields() {
|
||||
try {
|
||||
console.log('开始更新系统字段标记...');
|
||||
|
||||
// 系统字段列表
|
||||
const systemFields = [
|
||||
'deviceId', 'name', 'type', 'model', 'serialNumber',
|
||||
'rackId', 'position', 'height', 'powerConsumption',
|
||||
'status', 'purchaseDate', 'warrantyExpiry',
|
||||
'ipAddress', 'description'
|
||||
];
|
||||
|
||||
// 更新系统字段标记
|
||||
for (const fieldName of systemFields) {
|
||||
const [result] = await sequelize.query(
|
||||
`UPDATE deviceFields SET isSystem = 1 WHERE fieldName = '${fieldName}'`,
|
||||
{ type: sequelize.QueryTypes.RAW }
|
||||
);
|
||||
console.log(`标记系统字段: ${fieldName}`);
|
||||
}
|
||||
|
||||
console.log('系统字段标记更新完成');
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error('更新失败:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
updateSystemFields();
|
||||
Reference in New Issue
Block a user