CREATE TABLE IF NOT EXISTS account_message_restrictions ( user_id bigint PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE, restricted boolean NOT NULL DEFAULT false, version bigint NOT NULL DEFAULT 0, restricted_since timestamptz, restricted_until timestamptz, reason text NOT NULL DEFAULT '', actor text NOT NULL DEFAULT '', command_id text NOT NULL DEFAULT '', updated_at timestamptz NOT NULL DEFAULT now(), CONSTRAINT account_message_restrictions_user_id_check CHECK (user_id > 0), CONSTRAINT account_message_restrictions_shape_check CHECK ( (restricted AND restricted_since IS NOT NULL AND (restricted_until IS NULL OR restricted_until > restricted_since) AND (restricted_until IS NULL OR restricted_until <= to_timestamp(2147483647))) OR (NOT restricted AND restricted_since IS NULL AND restricted_until IS NULL) ) ); CREATE INDEX IF NOT EXISTS account_message_restrictions_restricted_idx ON account_message_restrictions (user_id) WHERE restricted;