-- +goose Up -- Invitation usage remains keyed by its stable invitation ID for the -- organization's lifetime, so a retry cannot count it again after the -- rolling window. The policy and trust columns record the basis of the -- original reservation. CREATE TABLE company_invitation_quota_usage ( organization_id uuid NOT NULL, invitation_id uuid NOT NULL, reserved_at timestamptz NOT NULL, policy_version_id uuid NOT NULL, verification_level text NOT NULL, verification_basis text NOT NULL, verification_changed_at timestamptz NOT NULL, quota_tier text NOT NULL, CONSTRAINT company_invitation_quota_usage_pkey PRIMARY KEY ( organization_id, invitation_id ), CONSTRAINT company_invitation_quota_usage_organization_id_fkey FOREIGN KEY ( organization_id ) REFERENCES organizations (id) ON DELETE CASCADE, CONSTRAINT company_invitation_quota_usage_policy_version_id_fkey FOREIGN KEY ( policy_version_id ) REFERENCES policy_versions (id) ON DELETE RESTRICT, CONSTRAINT company_invitation_quota_usage_verification_evidence CHECK ( ( verification_level = 'e0' AND verification_basis = 'confirmed_email' ) OR ( verification_level = 'e1' AND verification_basis = 'corporate_domain' ) OR ( verification_level = 'e2' AND verification_basis = 'requisites_and_authority' ) ), CONSTRAINT company_invitation_quota_usage_tier_known CHECK ( quota_tier IN ('e0', 'e1', 'e2') ), CONSTRAINT company_invitation_quota_usage_tier_matches_verification CHECK ( quota_tier = verification_level ), CONSTRAINT company_invitation_quota_usage_verification_not_future CHECK ( verification_changed_at <= reserved_at ) ); CREATE INDEX company_invitation_quota_usage_window_idx ON company_invitation_quota_usage (organization_id, reserved_at, invitation_id); -- Card-view rows are a rolling-window counter, not a full access history. -- Repeats update last_seen_at; expired rows may be deleted in bounded batches -- only after a successful reservation. This is logical window expiry, not a -- promise of deletion at an exact wall-clock time. CREATE TABLE company_card_view_quota_usage ( organization_id uuid NOT NULL, candidate_id uuid NOT NULL, last_seen_at timestamptz NOT NULL, policy_version_id uuid NOT NULL, verification_level text NOT NULL, verification_basis text NOT NULL, verification_changed_at timestamptz NOT NULL, quota_tier text NOT NULL, CONSTRAINT company_card_view_quota_usage_pkey PRIMARY KEY ( organization_id, candidate_id ), CONSTRAINT company_card_view_quota_usage_organization_id_fkey FOREIGN KEY ( organization_id ) REFERENCES organizations (id) ON DELETE CASCADE, CONSTRAINT company_card_view_quota_usage_candidate_id_fkey FOREIGN KEY ( candidate_id ) REFERENCES users (id) ON DELETE CASCADE, CONSTRAINT company_card_view_quota_usage_policy_version_id_fkey FOREIGN KEY ( policy_version_id ) REFERENCES policy_versions (id) ON DELETE RESTRICT, CONSTRAINT company_card_view_quota_usage_verification_evidence CHECK ( ( verification_level = 'e0' AND verification_basis = 'confirmed_email' ) OR ( verification_level = 'e1' AND verification_basis = 'corporate_domain' ) OR ( verification_level = 'e2' AND verification_basis = 'requisites_and_authority' ) ), CONSTRAINT company_card_view_quota_usage_tier_known CHECK ( quota_tier IN ('e0', 'e1', 'e2') ), CONSTRAINT company_card_view_quota_usage_tier_matches_verification CHECK ( quota_tier = verification_level ), CONSTRAINT company_card_view_quota_usage_verification_not_future CHECK ( verification_changed_at <= last_seen_at ) ); CREATE INDEX company_card_view_quota_usage_window_idx ON company_card_view_quota_usage (organization_id, last_seen_at, candidate_id); CREATE INDEX company_card_view_quota_usage_candidate_idx ON company_card_view_quota_usage (candidate_id); -- +goose Down DROP TABLE company_card_view_quota_usage; DROP TABLE company_invitation_quota_usage;