Files
OverCooked-Installer/s-migrations/000034_membership_limits_points_red_packets.up.sql
T

55 lines
2.6 KiB
SQL

ALTER TABLE site_brand_settings
ADD COLUMN IF NOT EXISTS points_name text NOT NULL DEFAULT '积分'
CHECK (char_length(btrim(points_name)) BETWEEN 1 AND 20);
UPDATE memberships
SET ends_at = timestamptz '9999-12-31 23:59:59+00'
WHERE ends_at > timestamptz '9999-12-31 23:59:59+00';
UPDATE card_batches SET duration_days = 365000 WHERE duration_days > 365000;
UPDATE membership_plans SET duration_days = 365000 WHERE duration_days > 365000;
ALTER TABLE card_batches
ADD CONSTRAINT card_batches_duration_days_limit CHECK (duration_days BETWEEN 1 AND 365000);
ALTER TABLE membership_plans
ADD CONSTRAINT membership_plans_duration_days_limit CHECK (duration_days BETWEEN 1 AND 365000);
ALTER TABLE point_ledger DROP CONSTRAINT IF EXISTS 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','membership_purchase','telegram_red_packet'));
CREATE TABLE telegram_red_packets (
id uuid PRIMARY KEY,
chat_id bigint NOT NULL,
creator_user_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
creator_telegram_id bigint NOT NULL,
allocation_mode text NOT NULL CHECK (allocation_mode IN ('normal','random')),
claim_mode text NOT NULL CHECK (claim_mode IN ('button','password')),
total_points integer NOT NULL CHECK (total_points BETWEEN 1 AND 100000),
packet_count integer NOT NULL CHECK (packet_count BETWEEN 1 AND 1000),
remaining_points integer NOT NULL CHECK (remaining_points >= 0),
remaining_count integer NOT NULL CHECK (remaining_count >= 0),
password_hash text NOT NULL DEFAULT '',
message text NOT NULL DEFAULT '' CHECK (char_length(message) <= 200),
status text NOT NULL DEFAULT 'open' CHECK (status IN ('open','expired','closed')),
message_id bigint NOT NULL DEFAULT 0,
expires_at timestamptz NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CHECK (remaining_count <= packet_count),
CHECK (remaining_points <= total_points)
);
CREATE INDEX telegram_red_packets_chat_open_idx ON telegram_red_packets(chat_id, expires_at DESC) WHERE status = 'open';
CREATE TABLE telegram_red_packet_claims (
id bigserial PRIMARY KEY,
packet_id uuid NOT NULL REFERENCES telegram_red_packets(id) ON DELETE CASCADE,
user_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
telegram_user_id bigint NOT NULL,
amount integer NOT NULL CHECK (amount > 0),
claimed_at timestamptz NOT NULL DEFAULT now(),
UNIQUE(packet_id, user_id),
UNIQUE(packet_id, telegram_user_id)
);