feat(数据库迁移): 为线缆表添加新字段并改进迁移脚本
添加 cableLabel、cableColor 等新字段到 cables 表 重构迁移脚本以支持多种数据库方言
This commit is contained in:
@@ -24,6 +24,8 @@ console.log(` DB_TYPE (from env): ${DB_TYPE}`);
|
|||||||
console.log(` dbDialect (actual): ${dbDialect}`);
|
console.log(` dbDialect (actual): ${dbDialect}`);
|
||||||
console.log(` sequelize.getDialect(): ${sequelize.getDialect()}`);
|
console.log(` sequelize.getDialect(): ${sequelize.getDialect()}`);
|
||||||
|
|
||||||
|
const { migrateCableFields } = require('./migrate-cable-fields');
|
||||||
|
|
||||||
const migrations = [
|
const migrations = [
|
||||||
{
|
{
|
||||||
name: 'v2.0 - 网卡和端口表',
|
name: 'v2.0 - 网卡和端口表',
|
||||||
@@ -100,6 +102,11 @@ const migrations = [
|
|||||||
description: '为 devices 表添加复合索引,优化位置冲突检测和悲观锁性能',
|
description: '为 devices 表添加复合索引,优化位置冲突检测和悲观锁性能',
|
||||||
migrate: migrateDevicePositionIndexes,
|
migrate: migrateDevicePositionIndexes,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: '线缆表新增字段',
|
||||||
|
description: '为 cables 表添加 cableLabel、cableColor 等新字段',
|
||||||
|
migrate: migrateCableFields,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: '耗材日志设备关联',
|
name: '耗材日志设备关联',
|
||||||
description: '为 consumable_logs 表添加 deviceId、deviceName、rackId、rackName、roomId、roomName 字段',
|
description: '为 consumable_logs 表添加 deviceId、deviceName、rackId、rackName、roomId、roomName 字段',
|
||||||
|
|||||||
@@ -1,13 +1,26 @@
|
|||||||
const { sequelize } = require('../db');
|
const { sequelize, dbDialect } = require('../db');
|
||||||
const Cable = require('../models/Cable');
|
|
||||||
|
async function getTableColumns(tableName) {
|
||||||
|
if (dbDialect === 'sqlite') {
|
||||||
|
const results = await sequelize.query(`PRAGMA table_info(${tableName})`, {
|
||||||
|
type: sequelize.QueryTypes.SELECT,
|
||||||
|
});
|
||||||
|
return results.map(row => row.name);
|
||||||
|
} else {
|
||||||
|
const results = await sequelize.query(`SHOW COLUMNS FROM ${tableName}`, {
|
||||||
|
type: sequelize.QueryTypes.SELECT,
|
||||||
|
});
|
||||||
|
return results.map(row => row.Field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function migrateCableFields() {
|
async function migrateCableFields() {
|
||||||
try {
|
const tableName = 'cables';
|
||||||
console.log('开始迁移 Cable 模型字段...');
|
|
||||||
|
|
||||||
// 检查字段是否已存在
|
try {
|
||||||
const [results] = await sequelize.query('PRAGMA table_info(cables)');
|
console.log(`开始迁移 ${tableName} 表新字段...`);
|
||||||
const existingColumns = results.map(row => row.name);
|
|
||||||
|
const existingColumns = await getTableColumns(tableName);
|
||||||
|
|
||||||
const newColumns = [
|
const newColumns = [
|
||||||
{ name: 'cableLabel', type: 'VARCHAR(255)' },
|
{ name: 'cableLabel', type: 'VARCHAR(255)' },
|
||||||
@@ -19,24 +32,34 @@ async function migrateCableFields() {
|
|||||||
|
|
||||||
for (const column of newColumns) {
|
for (const column of newColumns) {
|
||||||
if (!existingColumns.includes(column.name)) {
|
if (!existingColumns.includes(column.name)) {
|
||||||
console.log(`添加字段: ${column.name}`);
|
if (dbDialect === 'sqlite') {
|
||||||
await sequelize.query(`ALTER TABLE cables ADD COLUMN ${column.name} ${column.type}`);
|
await sequelize.query(`ALTER TABLE ${tableName} ADD COLUMN ${column.name} ${column.type}`);
|
||||||
} else {
|
} else {
|
||||||
console.log(`字段已存在: ${column.name}`);
|
await sequelize.query(`ALTER TABLE ${tableName} ADD COLUMN ${column.name} ${column.type}`);
|
||||||
|
}
|
||||||
|
console.log(` ✓ 添加字段: ${column.name}`);
|
||||||
|
} else {
|
||||||
|
console.log(` → 字段已存在: ${column.name}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('迁移完成!');
|
console.log(` ✓ ${tableName} 表字段迁移完成`);
|
||||||
console.log('\n新增字段:');
|
|
||||||
console.log(' - cableLabel: 线缆标签/编号');
|
|
||||||
console.log(' - cableColor: 线缆颜色(便于识别)');
|
|
||||||
console.log(' - installedBy: 安装人');
|
|
||||||
console.log(' - installedAt: 安装时间');
|
|
||||||
console.log(' - lastTestedAt: 上次测试时间');
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('迁移失败:', error);
|
console.error(` ✗ 迁移失败: ${error.message}`);
|
||||||
process.exit(1);
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
migrateCableFields();
|
if (require.main === module) {
|
||||||
|
migrateCableFields()
|
||||||
|
.then(() => {
|
||||||
|
console.log('迁移完成!');
|
||||||
|
process.exit(0);
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error('迁移执行失败:', err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { migrateCableFields };
|
||||||
Reference in New Issue
Block a user