Files
pangolin/server/migrations/mysql/000015_per_device_usage.up.sql
T
wangjia 636a3bbf2f feat(server/stats): stats-overhaul Phase2 — 每设备归因 + GB 综合配额
服务端记账从「账户」细到「每设备」(每设备独立 dp_uuid),配额单位分钟→GB
按账户综合卡控。000015_per_device_usage 迁移(mysql+sqlite 双份):devices.dp_uuid
+ usage_device_daily 表 + plans.daily_mb。handler_grpc 按 dp_uuid 回映射
(user_id,device_id) 双写账户+每设备;usage 服务/handler 暴露 /v1/usage(/devices)。
含 sqlite_per_device / usage handler 测试。

注:此迁移 prod 已 migrate up 运行、部署二进制已内嵌;本次补提交使 git 与线上一致。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-28 18:12:00 +08:00

27 lines
1.3 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 每设备归因 + GB 综合配额(todo #5 Phase 2
--
-- ① devices.dp_uuid:每设备独立的数据面凭证(替代账户级 users.dp_uuid 作为出网凭证)。
-- 唯一索引而非内联 UNIQUE,与 SQLite 迁移对齐(NULL 不冲突,存量设备待回填)。
-- ② usage_device_daily:按设备的每日用量;usage_daily 仍作账户综合 rollup。
-- ③ plans.daily_mb:每日综合流量配额(MB,与 daily_minutes 并存,按账户综合卡)。
-- 用 MB 而非 GB 以支持免费版 500MB。free 500 / pro 100GB / team 200GB(可调)。
ALTER TABLE devices ADD COLUMN dp_uuid CHAR(36) NULL;
CREATE UNIQUE INDEX idx_devices_dp_uuid ON devices (dp_uuid);
CREATE TABLE usage_device_daily (
user_id BIGINT UNSIGNED NOT NULL,
device_id BIGINT UNSIGNED NOT NULL,
date DATE NOT NULL,
bytes_up BIGINT UNSIGNED NOT NULL DEFAULT 0,
bytes_down BIGINT UNSIGNED NOT NULL DEFAULT 0,
minutes_used INT NOT NULL DEFAULT 0,
PRIMARY KEY (device_id, date),
INDEX idx_usage_device_user_date (user_id, date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE plans ADD COLUMN daily_mb INT NULL;
UPDATE plans SET daily_mb = 500 WHERE code = 'free';
UPDATE plans SET daily_mb = 102400 WHERE code = 'pro';
UPDATE plans SET daily_mb = 204800 WHERE code = 'team';