200 lines
6.4 KiB
JavaScript
200 lines
6.4 KiB
JavaScript
require('dotenv').config();
|
|
const database = require('../config/database');
|
|
const logger = require('../utils/logger');
|
|
|
|
async function runMigrations() {
|
|
try {
|
|
await database.connect();
|
|
logger.info('Starting database migrations...');
|
|
|
|
// Create users table
|
|
await database.query(`
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
username VARCHAR(50) UNIQUE NOT NULL,
|
|
email VARCHAR(100) UNIQUE NOT NULL,
|
|
password_hash VARCHAR(255) NOT NULL,
|
|
role ENUM('admin', 'operator', 'viewer') DEFAULT 'viewer',
|
|
status ENUM('active', 'inactive') DEFAULT 'active',
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
// Create devices table
|
|
await database.query(`
|
|
CREATE TABLE IF NOT EXISTS devices (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
device_id VARCHAR(100) UNIQUE NOT NULL,
|
|
name VARCHAR(100) NOT NULL,
|
|
device_type VARCHAR(50) NOT NULL,
|
|
status ENUM('online', 'offline') DEFAULT 'offline',
|
|
ai_model_config JSON,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
// Create device_data table
|
|
await database.query(`
|
|
CREATE TABLE IF NOT EXISTS device_data (
|
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
device_id VARCHAR(100) NOT NULL,
|
|
raw_data JSON NOT NULL,
|
|
timestamp DATETIME NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
// Create alerts table
|
|
await database.query(`
|
|
CREATE TABLE IF NOT EXISTS alerts (
|
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
device_id VARCHAR(100),
|
|
type VARCHAR(50) NOT NULL,
|
|
severity ENUM('info', 'warning', 'critical') NOT NULL,
|
|
message TEXT NOT NULL,
|
|
status ENUM('active', 'resolved') DEFAULT 'active',
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
// Create alert_rules table
|
|
await database.query(`
|
|
CREATE TABLE IF NOT EXISTS alert_rules (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
name VARCHAR(100) NOT NULL,
|
|
description TEXT,
|
|
device_type VARCHAR(50),
|
|
condition_type ENUM('threshold', 'anomaly', 'pattern') NOT NULL,
|
|
condition_config JSON NOT NULL,
|
|
severity ENUM('info', 'warning', 'critical') NOT NULL,
|
|
actions JSON,
|
|
status ENUM('active', 'inactive') DEFAULT 'active',
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
// Create healing_rules table
|
|
await database.query(`
|
|
CREATE TABLE IF NOT EXISTS healing_rules (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
name VARCHAR(100) NOT NULL,
|
|
description TEXT,
|
|
trigger_condition JSON NOT NULL,
|
|
healing_actions JSON NOT NULL,
|
|
priority INT DEFAULT 1,
|
|
status ENUM('active', 'inactive') DEFAULT 'active',
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
// Create healing_actions table
|
|
await database.query(`
|
|
CREATE TABLE IF NOT EXISTS healing_actions (
|
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
rule_id INT,
|
|
device_id VARCHAR(100),
|
|
action_type VARCHAR(50) NOT NULL,
|
|
action_config JSON NOT NULL,
|
|
status ENUM('pending', 'in_progress', 'completed', 'failed') DEFAULT 'pending',
|
|
result JSON,
|
|
started_at TIMESTAMP NULL,
|
|
completed_at TIMESTAMP NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
// Create ai_suggestions table
|
|
await database.query(`
|
|
CREATE TABLE IF NOT EXISTS ai_suggestions (
|
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
device_id VARCHAR(100),
|
|
suggestion_type ENUM('maintenance', 'optimization', 'alert', 'healing') NOT NULL,
|
|
title VARCHAR(200) NOT NULL,
|
|
description TEXT NOT NULL,
|
|
priority ENUM('low', 'medium', 'high', 'critical') DEFAULT 'medium',
|
|
confidence_score DECIMAL(5,2),
|
|
status ENUM('pending', 'approved', 'rejected', 'implemented') DEFAULT 'pending',
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
// Create notifications table
|
|
await database.query(`
|
|
CREATE TABLE IF NOT EXISTS notifications (
|
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
user_id INT,
|
|
type VARCHAR(50) NOT NULL,
|
|
title VARCHAR(200) NOT NULL,
|
|
message TEXT NOT NULL,
|
|
channel ENUM('email', 'sms', 'in_app') NOT NULL,
|
|
status ENUM('pending', 'sent', 'failed') DEFAULT 'pending',
|
|
sent_at TIMESTAMP NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
// Create device_thresholds table
|
|
await database.query(`
|
|
CREATE TABLE IF NOT EXISTS device_thresholds (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
device_id VARCHAR(100) NOT NULL,
|
|
parameter_name VARCHAR(100) NOT NULL,
|
|
min_value DECIMAL(10,2),
|
|
max_value DECIMAL(10,2),
|
|
warning_min DECIMAL(10,2),
|
|
warning_max DECIMAL(10,2),
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
// Create user_sessions table
|
|
await database.query(`
|
|
CREATE TABLE IF NOT EXISTS user_sessions (
|
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
user_id INT NOT NULL,
|
|
session_token VARCHAR(255) UNIQUE NOT NULL,
|
|
expires_at TIMESTAMP NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
// Create audit_logs table
|
|
await database.query(`
|
|
CREATE TABLE IF NOT EXISTS audit_logs (
|
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
user_id INT,
|
|
action VARCHAR(100) NOT NULL,
|
|
resource_type VARCHAR(50),
|
|
resource_id VARCHAR(100),
|
|
details JSON,
|
|
ip_address VARCHAR(45),
|
|
user_agent TEXT,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
// Create system_config table
|
|
await database.query(`
|
|
CREATE TABLE IF NOT EXISTS system_config (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
config_key VARCHAR(100) UNIQUE NOT NULL,
|
|
config_value TEXT,
|
|
description TEXT,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
|
|
logger.info('Database migrations completed successfully');
|
|
} catch (error) {
|
|
logger.error('Migration failed:', error);
|
|
process.exit(1);
|
|
} finally {
|
|
await database.disconnect();
|
|
}
|
|
}
|
|
|
|
if (require.main === module) {
|
|
runMigrations();
|
|
}
|
|
|
|
module.exports = { runMigrations };
|