apskel-pos-backend/migrations/000049_create_customer_tokens_table.up.sql
2025-09-17 19:30:17 +07:00

26 lines
985 B
SQL

CREATE TABLE customer_tokens (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
customer_id UUID NOT NULL,
token_type VARCHAR(50) NOT NULL,
balance BIGINT DEFAULT 0 NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
CONSTRAINT fk_customer_tokens_customer
FOREIGN KEY (customer_id)
REFERENCES customers(id)
ON DELETE CASCADE,
CONSTRAINT chk_customer_tokens_balance_non_negative
CHECK (balance >= 0),
CONSTRAINT chk_customer_tokens_type_valid
CHECK (token_type IN ('SPIN', 'RAFFLE', 'MINIGAME'))
);
CREATE INDEX idx_customer_tokens_customer_id ON customer_tokens(customer_id);
CREATE INDEX idx_customer_tokens_token_type ON customer_tokens(token_type);
CREATE INDEX idx_customer_tokens_updated_at ON customer_tokens(updated_at);
CREATE UNIQUE INDEX idx_customer_tokens_unique_customer_type ON customer_tokens(customer_id, token_type);