Files
OverCooked-Installer/s-migrations/000024_daily_checkins.up.sql
T

21 lines
1.0 KiB
SQL

ALTER TABLE point_ledger DROP CONSTRAINT point_ledger_source_type_check;
ALTER TABLE point_ledger ADD CONSTRAINT point_ledger_source_type_check
CHECK (source_type IN ('manual','playback_session','media_feedback','redemption','daily_checkin'));
INSERT INTO point_rules(rule_key,name,points,description) VALUES
('daily_checkin.base','每日签到',5,'每天首次签到获得的积分'),
('daily_checkin.streak_7','连续签到 7 天',10,'连续签到第 7 天的额外积分'),
('daily_checkin.streak_30','连续签到 30 天',30,'连续签到第 30 天的额外积分')
ON CONFLICT(rule_key) DO NOTHING;
CREATE TABLE daily_checkins (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
checkin_date date NOT NULL,
streak integer NOT NULL CHECK(streak > 0),
reward_points integer NOT NULL CHECK(reward_points >= 0),
created_at timestamptz NOT NULL DEFAULT now(),
UNIQUE(user_id,checkin_date)
);
CREATE INDEX daily_checkins_user_time_idx ON daily_checkins(user_id,checkin_date DESC);