#!/usr/bin/env node /** * Add authentication security tables to existing database */ const { db } = require('../src/database/db'); async function addAuthTables() { console.log('Adding authentication security tables...\n'); try { // 1. Create login_attempts table const hasLoginAttempts = await db.schema.hasTable('login_attempts'); if (!hasLoginAttempts) { await db.schema.createTable('login_attempts', table => { table.increments('id').primary(); table.string('identifier').notNullable(); table.string('ip_address', 45).notNullable(); table.text('user_agent'); table.timestamp('attempt_time').defaultTo(db.fn.now()); table.boolean('success').defaultTo(false); // Indexes for performance table.index('identifier'); table.index('attempt_time'); table.index(['identifier', 'success', 'attempt_time']); }); console.log('✓ Created login_attempts table'); } else { console.log('! login_attempts table already exists'); } // 2. Add columns to admin_users const hasPasswordChangedAt = await db.schema.hasColumn('admin_users', 'password_changed_at'); if (!hasPasswordChangedAt) { await db.schema.table('admin_users', table => { table.timestamp('password_changed_at').nullable(); }); console.log('✓ Added password_changed_at column'); } const hasLastLoginIp = await db.schema.hasColumn('admin_users', 'last_login_ip'); if (!hasLastLoginIp) { await db.schema.table('admin_users', table => { table.string('last_login_ip', 45).nullable(); }); console.log('✓ Added last_login_ip column'); } const hasTwoFactorEnabled = await db.schema.hasColumn('admin_users', 'two_factor_enabled'); if (!hasTwoFactorEnabled) { await db.schema.table('admin_users', table => { table.boolean('two_factor_enabled').defaultTo(false); }); console.log('✓ Added two_factor_enabled column'); } const hasTwoFactorSecret = await db.schema.hasColumn('admin_users', 'two_factor_secret'); if (!hasTwoFactorSecret) { await db.schema.table('admin_users', table => { table.string('two_factor_secret').nullable(); }); console.log('✓ Added two_factor_secret column'); } // 3. Verify everything console.log('\nVerifying tables...'); const loginAttemptsInfo = await db('login_attempts').columnInfo(); console.log('✓ login_attempts columns:', Object.keys(loginAttemptsInfo).join(', ')); const adminUsersInfo = await db('admin_users').columnInfo(); const securityColumns = ['password_changed_at', 'last_login_ip', 'two_factor_enabled', 'two_factor_secret']; const hasAllColumns = securityColumns.every(col => adminUsersInfo[col]); if (hasAllColumns) { console.log('✓ All security columns present in admin_users'); } else { console.log('✗ Some security columns missing from admin_users'); } console.log('\n✅ Authentication security tables ready!'); await db.destroy(); process.exit(0); } catch (error) { console.error('\n❌ Error adding auth tables:', error); await db.destroy(); process.exit(1); } } addAuthTables();