-- Database Maintenance Scripts
-- Run with: npx prisma db execute --file ./scripts/database-maintenance.sql

-- Add indexes for better performance
CREATE INDEX IF NOT EXISTS idx_monthly_attendance_user_year ON monthly_attendance(userId, year);
CREATE INDEX IF NOT EXISTS idx_monthly_performance_user_year ON monthly_performance(userId, year);
CREATE INDEX IF NOT EXISTS idx_logged_hours_user_date ON logged_hours(userId, date);

-- Update any missing data
UPDATE users SET isActive = true WHERE isActive IS NULL;

-- Clean up orphaned records (if any)
-- DELETE FROM monthly_attendance WHERE userId NOT IN (SELECT id FROM users);

-- Add any custom business logic
-- Example: Update department names
-- UPDATE users SET department = 'Engineering' WHERE department = 'Dev';

-- Performance optimization
ANALYZE TABLE users;
ANALYZE TABLE monthly_performance;
ANALYZE TABLE monthly_attendance;
ANALYZE TABLE logged_hours; 