const { DataTypes } = require('sequelize'); module.exports = { up: async (queryInterface, Sequelize) => { await queryInterface.createTable('security_logs', { id: { type: DataTypes.UUID, defaultValue: Sequelize.UUIDV4, primaryKey: true, allowNull: false }, tenant_id: { type: DataTypes.UUID, allowNull: true, references: { model: 'tenants', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'SET NULL' }, event_type: { type: DataTypes.STRING(50), allowNull: false }, severity: { type: DataTypes.STRING(20), allowNull: false, defaultValue: 'info' }, user_id: { type: DataTypes.UUID, allowNull: true }, username: { type: DataTypes.STRING(100), allowNull: true }, ip_address: { type: DataTypes.INET, allowNull: true }, client_ip: { type: DataTypes.INET, allowNull: true }, user_agent: { type: DataTypes.TEXT, allowNull: true }, rdns: { type: DataTypes.STRING(255), allowNull: true }, country_code: { type: DataTypes.STRING(2), allowNull: true }, country_name: { type: DataTypes.STRING(100), allowNull: true }, city: { type: DataTypes.STRING(100), allowNull: true }, is_high_risk_country: { type: DataTypes.BOOLEAN, defaultValue: false }, message: { type: DataTypes.TEXT, allowNull: false }, metadata: { type: DataTypes.JSONB, allowNull: true, defaultValue: {} }, session_id: { type: DataTypes.STRING(255), allowNull: true }, request_id: { type: DataTypes.STRING(255), allowNull: true }, endpoint: { type: DataTypes.STRING(255), allowNull: true }, method: { type: DataTypes.STRING(10), allowNull: true }, status_code: { type: DataTypes.INTEGER, allowNull: true }, alerted: { type: DataTypes.BOOLEAN, defaultValue: false }, created_at: { type: DataTypes.DATE, defaultValue: Sequelize.NOW, allowNull: false } }); // Add indexes for performance await queryInterface.addIndex('security_logs', ['tenant_id', 'created_at']); await queryInterface.addIndex('security_logs', ['event_type', 'created_at']); await queryInterface.addIndex('security_logs', ['ip_address', 'created_at']); await queryInterface.addIndex('security_logs', ['username', 'created_at']); await queryInterface.addIndex('security_logs', ['severity', 'created_at']); await queryInterface.addIndex('security_logs', ['country_code', 'is_high_risk_country']); await queryInterface.addIndex('security_logs', ['alerted', 'severity', 'created_at']); }, down: async (queryInterface, Sequelize) => { await queryInterface.dropTable('security_logs'); } };