'use strict'; module.exports = { up: async (queryInterface, Sequelize) => { await queryInterface.createTable('audit_logs', { id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false }, timestamp: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.NOW }, level: { type: Sequelize.ENUM('INFO', 'WARNING', 'ERROR', 'CRITICAL'), allowNull: false }, action: { type: Sequelize.STRING(100), allowNull: false, comment: 'The action performed (e.g., logo_upload, logo_removal)' }, message: { type: Sequelize.TEXT, allowNull: false, comment: 'Human-readable description of the event' }, user_id: { type: Sequelize.INTEGER, allowNull: true, comment: 'ID of the user who performed the action', references: { model: 'users', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'SET NULL' }, username: { type: Sequelize.STRING(255), allowNull: true, comment: 'Username of the user who performed the action' }, tenant_id: { type: Sequelize.INTEGER, allowNull: true, comment: 'ID of the tenant affected by the action', references: { model: 'tenants', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'SET NULL' }, tenant_slug: { type: Sequelize.STRING(255), allowNull: true, comment: 'Slug of the tenant affected by the action' }, ip_address: { type: Sequelize.STRING(45), allowNull: true, comment: 'IP address of the user (supports IPv6)' }, user_agent: { type: Sequelize.TEXT, allowNull: true, comment: 'User agent string from the request' }, path: { type: Sequelize.STRING(500), allowNull: true, comment: 'Request path that triggered the action' }, metadata: { type: Sequelize.JSON, allowNull: true, comment: 'Additional metadata about the event' }, success: { type: Sequelize.BOOLEAN, allowNull: true, comment: 'Whether the action was successful' } }); // Add indexes for performance await queryInterface.addIndex('audit_logs', ['timestamp']); await queryInterface.addIndex('audit_logs', ['action']); await queryInterface.addIndex('audit_logs', ['user_id']); await queryInterface.addIndex('audit_logs', ['tenant_id']); await queryInterface.addIndex('audit_logs', ['level']); await queryInterface.addIndex('audit_logs', ['timestamp', 'action']); await queryInterface.addIndex('audit_logs', ['tenant_id', 'timestamp']); }, down: async (queryInterface, Sequelize) => { await queryInterface.dropTable('audit_logs'); } };