52b5e90d92
000024 迁移重建 subscriptions 表(source CHECK 扩容)时未重新创建 idx_subs_user_exp(user_id, expires_at)索引,导致 SQLite(生产 pangolin1 实际驱动)上订阅按 user_id/expires_at 查询退化为全表扫描。up/down 均补回 CREATE INDEX,并在 TestSQLiteMigrateUpDown 里加断言防止再次静默丢失。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
51 lines
2.0 KiB
SQL
51 lines
2.0 KiB
SQL
-- users 加两列
|
|
ALTER TABLE users ADD COLUMN invite_code TEXT;
|
|
ALTER TABLE users ADD COLUMN first_paid_at DATETIME;
|
|
CREATE UNIQUE INDEX ux_users_invite_code ON users(invite_code);
|
|
|
|
-- 邀请关系(一对一绑定)
|
|
CREATE TABLE referrals (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
inviter_id INTEGER NOT NULL,
|
|
invitee_id INTEGER NOT NULL UNIQUE,
|
|
device_uuid TEXT,
|
|
status TEXT NOT NULL DEFAULT 'bound'
|
|
CHECK (status IN ('bound','reg_rewarded','paid_rewarded','rejected')),
|
|
reg_rewarded_at DATETIME,
|
|
paid_rewarded_at DATETIME,
|
|
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (inviter_id) REFERENCES users(id),
|
|
FOREIGN KEY (invitee_id) REFERENCES users(id)
|
|
);
|
|
CREATE INDEX ix_referrals_inviter ON referrals(inviter_id, created_at);
|
|
|
|
-- 通用一次性任务领取
|
|
CREATE TABLE reward_claims (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER NOT NULL,
|
|
task_key TEXT NOT NULL,
|
|
external_ref TEXT NOT NULL DEFAULT '',
|
|
granted_days INTEGER NOT NULL,
|
|
granted_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (user_id) REFERENCES users(id)
|
|
);
|
|
CREATE UNIQUE INDEX ux_claim_user_task ON reward_claims(user_id, task_key);
|
|
CREATE UNIQUE INDEX ux_claim_task_ref ON reward_claims(task_key, external_ref);
|
|
|
|
-- subscriptions.source 扩容(SQLite 需重建表)
|
|
CREATE TABLE subscriptions_new (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER NOT NULL,
|
|
plan_id INTEGER NOT NULL,
|
|
expires_at DATETIME NOT NULL,
|
|
source TEXT NOT NULL CHECK (source IN ('trial','code','pay','invite','task')),
|
|
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (user_id) REFERENCES users(id),
|
|
FOREIGN KEY (plan_id) REFERENCES plans(id)
|
|
);
|
|
INSERT INTO subscriptions_new (id,user_id,plan_id,expires_at,source,created_at)
|
|
SELECT id,user_id,plan_id,expires_at,source,created_at FROM subscriptions;
|
|
DROP TABLE subscriptions;
|
|
ALTER TABLE subscriptions_new RENAME TO subscriptions;
|
|
CREATE INDEX idx_subs_user_exp ON subscriptions (user_id, expires_at);
|