-- NIP-PL effective lease state and durable wake outbox. Every key is led by -- community_id: client-provided origin is confirmation only, never routing. CREATE TABLE push_leases ( community_id UUID NOT NULL REFERENCES communities(id), author BYTEA NOT NULL CHECK (length(author) = 32), installation_id TEXT NOT NULL CHECK (octet_length(installation_id) BETWEEN 1 AND 64), source_event_id BYTEA NOT NULL CHECK (length(source_event_id) = 32), source_created_at BIGINT NOT NULL, generation BIGINT NOT NULL CHECK (generation > 0), active BOOLEAN NOT NULL, endpoint_enabled BOOLEAN NOT NULL DEFAULT true, endpoint_hash BYTEA CHECK (endpoint_hash IS NULL OR length(endpoint_hash) = 32), endpoint_grant TEXT, max_class TEXT CHECK (max_class IS NULL OR max_class IN ('silent','default','time_sensitive','urgent')), subscriptions JSONB, expires_at BIGINT NOT NULL, updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), PRIMARY KEY (community_id, author, installation_id), UNIQUE (community_id, source_event_id), CHECK ((active AND endpoint_hash IS NOT NULL AND endpoint_grant IS NOT NULL AND max_class IS NOT NULL AND subscriptions IS NOT NULL) OR (NOT active AND endpoint_hash IS NULL AND endpoint_grant IS NULL AND max_class IS NULL AND subscriptions IS NULL)) ); CREATE UNIQUE INDEX push_leases_endpoint_unique ON push_leases (community_id, author, endpoint_hash) WHERE active; CREATE INDEX push_leases_expiry ON push_leases (community_id, expires_at) WHERE active;