-- Migration: Add User Email
-- Version: 20250102_120000
-- Description: 为用户表添加邮箱字段和相关索引
-- Created: 2025-01-02 12:00:00

-- ==========================================
-- UP Migration (执行迁移)
-- ==========================================

-- 添加邮箱字段
ALTER TABLE users 
ADD COLUMN email VARCHAR(255) NULL AFTER username;

-- 添加邮箱唯一索引
ALTER TABLE users 
ADD UNIQUE INDEX idx_users_email (email);

-- 添加邮箱验证状态字段
ALTER TABLE users 
ADD COLUMN email_verified BOOLEAN DEFAULT FALSE AFTER email;

-- 添加邮箱验证时间字段
ALTER TABLE users 
ADD COLUMN email_verified_at TIMESTAMP NULL AFTER email_verified;

-- ==========================================
-- DOWN Migration (回滚迁移)
-- ==========================================

-- 删除邮箱验证时间字段
ALTER TABLE users 
DROP COLUMN email_verified_at;

-- 删除邮箱验证状态字段
ALTER TABLE users 
DROP COLUMN email_verified;

-- 删除邮箱索引
ALTER TABLE users 
DROP INDEX idx_users_email;

-- 删除邮箱字段
ALTER TABLE users 
DROP COLUMN email;

-- ==========================================
-- 验证脚本 (可选)
-- ==========================================

-- 验证邮箱字段是否添加成功
-- SELECT COUNT(*) FROM information_schema.columns 
-- WHERE table_schema = DATABASE() 
-- AND table_name = 'users' 
-- AND column_name IN ('email', 'email_verified', 'email_verified_at');
