feat(db): tables for sharing pages and cloud workspaces with people and teams

Merged
SA
1mo ago
+14721-2

Adds the tables for sharing pages and cloud workspaces with people and teams. Nothing reads or writes them yet; the API and UI ship in #8318 so either can be reverted alone.

Schema (migration 0152_sharing_people_and_teams)

  • page_shares and cloud_workspace_shares: one row per grantee (a member, a team, or a pending org invitation), enforced by a num_nonnulls(...) = 1 check, unique per resource and grantee.
  • page_share_role enum (view, comment); page_shares.role and pages.organization_role (what general access allows), both defaulting to comment so existing pages keep today's behaviour.
  • cloud_workspace_activity_event gains shared and unshared; activity rows gain target_user_id, target_team_id, target_email.
  • Indexes on the columns that SET NULL on delete (shared_by_user_id, target_user_id, target_team_id), added while the tables are empty.

Migration safety (.agents/skills/db-migrations)

  • Production sizes (read-only): cloud_workspace_activity ~480 rows, pages ~6.8k, cloud_workspaces ~280, auth.users ~64k, auth.teams ~66k, auth.invitations ~450. No type changes or rewrites; pages.organization_role is a metadata-only ADD COLUMN ... DEFAULT.

  • Hand-ordered (rule 3). The generated order adds columns to pages and cloud_workspace_activity (ACCESS EXCLUSIVE) before the new FKs take SHARE ROW EXCLUSIVE on auth.users/auth.teams, so one open writer on the auth tables stalls page reads. The migration now locks the auth tables first, while it holds only its own new tables; the cloud_workspace_activity changes follow, and pages comes last. A SQL comment marks each moved block; regenerating would restore the generated order.

  • Rehearsed under load on fresh production-child branches, applying with PGOPTIONS="-c lock_timeout=5s -c statement_timeout=60s" while 8 readers hit pages/auth.users and writers held 200 ms transactions on auth.users, auth.teams, and pages:

    OrderAppliedPage reads, maxAuth writes, max
    Generated3/3, first attempt, ~2.6 s955 / 1010 / 1046 ms~1.0–1.1 s
    Hand-ordered (this PR)3/3, first attempt, ~2.5 s236 / 74 / 65 ms~1.1–1.2 s

    Page-read p99 stayed at ~21 ms in every run. Writes to the auth tables still wait for the migration to commit in either order.

  • No tRPC input or output changes in this PR. Tables passed through by findMany without columns gain the new columns as extra output fields only.

  • No relations() for the new tables: #8318 never reads them through db.query with with.

Summary by CodeRabbit

  • New Features
    • Cloud workspaces can now be shared with individuals, teams, or invited people.
    • Pages can be shared with individuals, teams, or invited people with view or comment access.
    • Pages now support an organization-wide access level.
    • Workspace activity records when a workspace is shared or unshared, including the share target.