-- 给 tb_package_usage 添加 package_name 快照字段
-- 用于展示套餐名称，避免每次展示时都需要 JOIN tb_package 表
-- 与 data_reset_cycle 字段保持一致的快照设计

ALTER TABLE tb_package_usage
ADD COLUMN IF NOT EXISTS package_name VARCHAR(255) NOT NULL DEFAULT '';

COMMENT ON COLUMN tb_package_usage.package_name IS '套餐名称快照（创建时从 tb_package 复制，用于历史记录展示）';

-- 回填历史数据：从 tb_package 表取套餐名称
UPDATE tb_package_usage pu
SET package_name = COALESCE(p.package_name, '')
FROM tb_package p
WHERE pu.package_id = p.id
  AND pu.package_name = '';
