ALTER TABLE gateway.api_keys
    ADD COLUMN IF NOT EXISTS requests_per_minute integer NOT NULL DEFAULT 0,
    ADD COLUMN IF NOT EXISTS monthly_request_quota bigint NOT NULL DEFAULT 0;

ALTER TABLE gateway.api_keys
    DROP CONSTRAINT IF EXISTS api_keys_requests_per_minute_check,
    DROP CONSTRAINT IF EXISTS api_keys_monthly_request_quota_check;

ALTER TABLE gateway.api_keys
    ADD CONSTRAINT api_keys_requests_per_minute_check
        CHECK (requests_per_minute BETWEEN 0 AND 1000000),
    ADD CONSTRAINT api_keys_monthly_request_quota_check
        CHECK (monthly_request_quota BETWEEN 0 AND 1000000000000);

COMMENT ON COLUMN gateway.api_keys.requests_per_minute IS
    'Atomic Redis-enforced request limit per UTC minute. Zero means unlimited.';
COMMENT ON COLUMN gateway.api_keys.monthly_request_quota IS
    'Atomic Redis-enforced accepted request quota per UTC calendar month. Zero means unlimited.';
