refactor(server, init-db): 调整数据库模型同步逻辑与顺序
- 补充新增的业务模型同步配置 - 按依赖层级重构初始化脚本的同步顺序 - 调整server.js中业务模型同步的调用时机 - 更新同步完成的日志信息
This commit is contained in:
@@ -7,6 +7,8 @@
|
|||||||
* 如需单独运行:node backend/scripts/init-database.js
|
* 如需单独运行:node backend/scripts/init-database.js
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
require('dotenv').config({ path: require('path').join(__dirname, '..', '.env') });
|
||||||
|
|
||||||
const { sequelize } = require('../db');
|
const { sequelize } = require('../db');
|
||||||
const SystemSetting = require('../models/SystemSetting');
|
const SystemSetting = require('../models/SystemSetting');
|
||||||
const Consumable = require('../models/Consumable');
|
const Consumable = require('../models/Consumable');
|
||||||
@@ -22,6 +24,22 @@ const FaultCategory = require('../models/FaultCategory');
|
|||||||
const InventoryPlan = require('../models/InventoryPlan');
|
const InventoryPlan = require('../models/InventoryPlan');
|
||||||
const InventoryTask = require('../models/InventoryTask');
|
const InventoryTask = require('../models/InventoryTask');
|
||||||
const InventoryRecord = require('../models/InventoryRecord');
|
const InventoryRecord = require('../models/InventoryRecord');
|
||||||
|
const Warehouse = require('../models/Warehouse');
|
||||||
|
const Cable = require('../models/Cable');
|
||||||
|
const DeviceField = require('../models/DeviceField');
|
||||||
|
const DevicePort = require('../models/DevicePort');
|
||||||
|
const NetworkCard = require('../models/NetworkCard');
|
||||||
|
const PendingDevice = require('../models/PendingDevice');
|
||||||
|
const Business = require('../models/Business');
|
||||||
|
const DeviceBusiness = require('../models/DeviceBusiness');
|
||||||
|
const Role = require('../models/Role');
|
||||||
|
const Permission = require('../models/Permission');
|
||||||
|
const UserRole = require('../models/UserRole');
|
||||||
|
const Ticket = require('../models/Ticket');
|
||||||
|
const TicketField = require('../models/TicketField');
|
||||||
|
const TicketOperationRecord = require('../models/TicketOperationRecord');
|
||||||
|
const BackupLog = require('../models/BackupLog');
|
||||||
|
const OperationLog = require('../models/OperationLog');
|
||||||
|
|
||||||
// 默认系统设置(包含中文描述)
|
// 默认系统设置(包含中文描述)
|
||||||
const defaultSettings = [
|
const defaultSettings = [
|
||||||
@@ -224,30 +242,47 @@ async function initDatabase() {
|
|||||||
await sequelize.authenticate();
|
await sequelize.authenticate();
|
||||||
console.log('数据库连接成功');
|
console.log('数据库连接成功');
|
||||||
|
|
||||||
// 同步所有模型(按依赖顺序)
|
// 同步所有模型(按依赖层级顺序)
|
||||||
|
// 第0层:无外键依赖的独立模型
|
||||||
console.log('开始同步模型...');
|
console.log('开始同步模型...');
|
||||||
|
|
||||||
// 基础模型
|
|
||||||
await User.sync({ alter: true });
|
await User.sync({ alter: true });
|
||||||
await Room.sync({ alter: true });
|
await Room.sync({ alter: true });
|
||||||
await Rack.sync({ alter: true });
|
await Warehouse.sync({ alter: true });
|
||||||
await Device.sync({ alter: true });
|
await Role.sync({ alter: true });
|
||||||
await FaultCategory.sync({ alter: true });
|
await Permission.sync({ alter: true });
|
||||||
|
await Business.sync({ alter: true });
|
||||||
// 耗材相关模型
|
|
||||||
await ConsumableCategory.sync({ alter: true });
|
await ConsumableCategory.sync({ alter: true });
|
||||||
await Consumable.sync({ alter: true });
|
await Consumable.sync({ alter: true });
|
||||||
await ConsumableLog.sync({ alter: true });
|
await FaultCategory.sync({ alter: true });
|
||||||
await ConsumableRecord.sync({ alter: true });
|
await DeviceField.sync({ alter: true });
|
||||||
await ConsumableLogArchive.sync({ alter: true });
|
await TicketField.sync({ alter: true });
|
||||||
|
|
||||||
// 盘点相关模型
|
|
||||||
await InventoryPlan.sync({ alter: true });
|
|
||||||
await InventoryTask.sync({ alter: true });
|
|
||||||
await InventoryRecord.sync({ alter: true });
|
|
||||||
|
|
||||||
// 系统设置
|
|
||||||
await SystemSetting.sync({ alter: true });
|
await SystemSetting.sync({ alter: true });
|
||||||
|
await BackupLog.sync({ alter: true });
|
||||||
|
await ConsumableLogArchive.sync({ alter: true });
|
||||||
|
await OperationLog.sync({ alter: true });
|
||||||
|
|
||||||
|
// 第1层:仅依赖第0层
|
||||||
|
await Rack.sync({ alter: true });
|
||||||
|
await UserRole.sync({ alter: true });
|
||||||
|
await InventoryPlan.sync({ alter: true });
|
||||||
|
await ConsumableRecord.sync({ alter: true });
|
||||||
|
|
||||||
|
// 第2层:依赖第0-1层
|
||||||
|
await Device.sync({ alter: true });
|
||||||
|
await InventoryTask.sync({ alter: true });
|
||||||
|
|
||||||
|
// 第3层:依赖第0-2层
|
||||||
|
await Cable.sync({ alter: true });
|
||||||
|
await NetworkCard.sync({ alter: true });
|
||||||
|
await DevicePort.sync({ alter: true });
|
||||||
|
await DeviceBusiness.sync({ alter: true });
|
||||||
|
await Ticket.sync({ alter: true });
|
||||||
|
await PendingDevice.sync({ alter: true });
|
||||||
|
|
||||||
|
// 第4层:依赖第0-3层
|
||||||
|
await TicketOperationRecord.sync({ alter: true });
|
||||||
|
await InventoryRecord.sync({ alter: true });
|
||||||
|
await ConsumableLog.sync({ alter: true });
|
||||||
|
|
||||||
console.log('数据库表结构同步完成');
|
console.log('数据库表结构同步完成');
|
||||||
|
|
||||||
|
|||||||
+34
-8
@@ -95,18 +95,44 @@ async function syncBusinessModels() {
|
|||||||
const Business = require('./models/Business');
|
const Business = require('./models/Business');
|
||||||
const DeviceBusiness = require('./models/DeviceBusiness');
|
const DeviceBusiness = require('./models/DeviceBusiness');
|
||||||
const Warehouse = require('./models/Warehouse');
|
const Warehouse = require('./models/Warehouse');
|
||||||
|
const Cable = require('./models/Cable');
|
||||||
|
const DevicePort = require('./models/DevicePort');
|
||||||
|
const NetworkCard = require('./models/NetworkCard');
|
||||||
|
const PendingDevice = require('./models/PendingDevice');
|
||||||
|
const Role = require('./models/Role');
|
||||||
|
const Permission = require('./models/Permission');
|
||||||
|
const UserRole = require('./models/UserRole');
|
||||||
|
const Ticket = require('./models/Ticket');
|
||||||
|
const TicketOperationRecord = require('./models/TicketOperationRecord');
|
||||||
|
|
||||||
// 仅在开发环境使用 sync({ alter: true }),生产环境应使用 migrations
|
|
||||||
if (process.env.NODE_ENV !== 'production') {
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
await Business.sync({ alter: true });
|
|
||||||
await DeviceBusiness.sync({ alter: true });
|
|
||||||
await Warehouse.sync({ alter: true });
|
await Warehouse.sync({ alter: true });
|
||||||
logger.info('业务/设备关联/库房模型同步完成(alter mode)');
|
await Business.sync({ alter: true });
|
||||||
|
await Role.sync({ alter: true });
|
||||||
|
await Permission.sync({ alter: true });
|
||||||
|
await UserRole.sync({ alter: true });
|
||||||
|
await Cable.sync({ alter: true });
|
||||||
|
await DevicePort.sync({ alter: true });
|
||||||
|
await NetworkCard.sync({ alter: true });
|
||||||
|
await DeviceBusiness.sync({ alter: true });
|
||||||
|
await PendingDevice.sync({ alter: true });
|
||||||
|
await Ticket.sync({ alter: true });
|
||||||
|
await TicketOperationRecord.sync({ alter: true });
|
||||||
|
logger.info('业务/库房/工单等扩展模型同步完成(alter mode)');
|
||||||
} else {
|
} else {
|
||||||
await Business.sync();
|
|
||||||
await DeviceBusiness.sync();
|
|
||||||
await Warehouse.sync();
|
await Warehouse.sync();
|
||||||
logger.info('业务/设备关联/库房模型同步完成(safe mode)');
|
await Business.sync();
|
||||||
|
await Role.sync();
|
||||||
|
await Permission.sync();
|
||||||
|
await UserRole.sync();
|
||||||
|
await Cable.sync();
|
||||||
|
await DevicePort.sync();
|
||||||
|
await NetworkCard.sync();
|
||||||
|
await DeviceBusiness.sync();
|
||||||
|
await PendingDevice.sync();
|
||||||
|
await Ticket.sync();
|
||||||
|
await TicketOperationRecord.sync();
|
||||||
|
logger.info('业务/库房/工单等扩展模型同步完成(safe mode)');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,6 +239,7 @@ async function initAutoBackupScheduler() {
|
|||||||
async function initializeApp() {
|
async function initializeApp() {
|
||||||
try {
|
try {
|
||||||
await syncDatabase();
|
await syncDatabase();
|
||||||
|
await syncBusinessModels();
|
||||||
await initDeviceFields();
|
await initDeviceFields();
|
||||||
await initTicketFields();
|
await initTicketFields();
|
||||||
await initTicketModels();
|
await initTicketModels();
|
||||||
@@ -221,7 +248,6 @@ async function initializeApp() {
|
|||||||
await syncInventoryModels();
|
await syncInventoryModels();
|
||||||
await syncBackupLogModel();
|
await syncBackupLogModel();
|
||||||
await syncOperationLogModel();
|
await syncOperationLogModel();
|
||||||
await syncBusinessModels();
|
|
||||||
await initDefaultSystemSettings();
|
await initDefaultSystemSettings();
|
||||||
await initFaultCategories();
|
await initFaultCategories();
|
||||||
await initAutoBackupScheduler();
|
await initAutoBackupScheduler();
|
||||||
|
|||||||
Reference in New Issue
Block a user