-- V19__Add_audit_fields.sql
-- 添加审计字段用于追踪数据变更责任人

-- 为 activities 表添加审计字段
ALTER TABLE activities ADD COLUMN IF NOT EXISTS created_by BIGINT;
ALTER TABLE activities ADD COLUMN IF NOT EXISTS updated_by BIGINT;

-- 为 api_keys 表添加审计字段
ALTER TABLE api_keys ADD COLUMN IF NOT EXISTS created_by BIGINT;
ALTER TABLE api_keys ADD COLUMN IF NOT EXISTS updated_by BIGINT;

-- 为 user_invites 表添加审计字段
ALTER TABLE user_invites ADD COLUMN IF NOT EXISTS created_by BIGINT;
ALTER TABLE user_invites ADD COLUMN IF NOT EXISTS updated_by BIGINT;

-- 为 short_links 表添加审计字段
ALTER TABLE short_links ADD COLUMN IF NOT EXISTS created_by BIGINT;
ALTER TABLE short_links ADD COLUMN IF NOT EXISTS updated_by BIGINT;

-- 为 daily_activity_stats 表添加审计字段
ALTER TABLE daily_activity_stats ADD COLUMN IF NOT EXISTS created_by BIGINT;
ALTER TABLE daily_activity_stats ADD COLUMN IF NOT EXISTS updated_by BIGINT;

-- 添加索引以优化查询
CREATE INDEX IF NOT EXISTS idx_activities_created_by ON activities(created_by);
CREATE INDEX IF NOT EXISTS idx_activities_updated_by ON activities(updated_by);
CREATE INDEX IF NOT EXISTS idx_api_keys_created_by ON api_keys(created_by);
CREATE INDEX IF NOT EXISTS idx_user_invites_created_by ON user_invites(created_by);

-- 注意: 启用Spring Data JPA审计需要在主类上添加 @EnableJpaAuditing
-- 并使用 @CreatedBy 和 @LastModifiedBy 注解
