-- 数据库迁移脚本：为users表添加created_at字段
-- 如果users表不存在created_at字段，则添加它

-- 检查字段是否存在，如果不存在则添加
SET @sql = IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
     WHERE TABLE_SCHEMA = DATABASE() 
     AND TABLE_NAME = 'users' 
     AND COLUMN_NAME = 'created_at') = 0,
    'ALTER TABLE users ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP',
    'SELECT "created_at column already exists" as message'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 为现有用户设置创建时间（如果为NULL）
UPDATE users SET created_at = CURRENT_TIMESTAMP WHERE created_at IS NULL;
