Phase 13, part 1: a Postgres schema per team

On the multi-tenant edition each team's data is in its own schema; the
tables every team shares (people, sessions, memberships, keys, locks, and
a new teams table) stay in the main one. A team's connection pool searches
its own schema first, so the app's SQL runs unchanged and can't reach
another team's tables. Each request is served by a copy of the Server for
the viewer's team, with that team's database, secrets and caches; agents
work on their own team's. The local and server editions are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 12:08:47 -07:00
co-authored by Claude Opus 5.5
parent ff5eaa9784
commit 0e695ac810
24 changed files with 1037 additions and 289 deletions
+41
View File
@@ -54,6 +54,9 @@ merged** (2026-11-25): a new `compose.server.yaml` and `install-server.sh`.
tools over MCP, approvals, limits and visibility, and the JSON API. Schema
39.
**Phase 13 (multi-tenant) is under way** on branch `phase13`, with a
Postgres schema per team (decided 2026-11-26); part 1 is built.
**Phase 12 (signing in with the Core, OIDC) is set aside for now**
(2026-11-26): before it starts, the user will share the Federated Core's
`idproviders.yml`, which shows how the Core sets up Authelia and its
@@ -2769,6 +2772,44 @@ Core's admin tooling does it.
Many teams sign in at one address, each working on its own domains. This
builds on Phases 9–12. A commit after each part.
**Decided 2026-11-26: a Postgres schema per team**, instead of the
`team_id` column on every table and row-level security that parts 1 and 2
describe below. Those would have meant changing about 611 queries in 88
files and every unique key. With a schema per team, the app's SQL runs
unchanged: a team's connection pool has the team's schema first on its
search_path, then the main schema, so its queries find the team's tables
and can't reach another team's without naming them. Parts 1 and 2 below
are built that way; their aims (every query scoped, and a second guard)
stand.
**Part 1 built 2026-11-26** on branch `phase13`.
- Schema 40 (the platform's): `teams` (name, its one website domain,
active, suspended or deleting) and `sessions.team_id`, the team a session
works in.
- The shared tables (`db.PlatformTables`: people, memberships, sessions,
links, throttle, agent keys, instances, locks, teams) are in the main
schema; every other table is a team's. Migrations say which they are
(`Platform: true` for the shared ones; a migration is one or the other).
- `db.Teams`: `Create` makes `team_<id>` and migrates it with the team's
migrations only; `Open` keeps one small pool per team (4 connections,
idle ones closed after a minute), migrating it forward the first time;
`Drop` removes the schema. Tests on Postgres name teams' schemas after
their own and drop them with it.
- The web server: each route is registered as a method expression and run
on the Server for the request's team (`forTeam`): a copy with the team's
database, secrets (the `secrets` table is a team's, so each team's keys
are its own) and caches (Reddit's and Google's tokens are per team now).
What every copy shares (templates, the jobs runner, the copies
themselves) moved into a shared `state`. MCP: an agent's key finds its
team, and its tools, `profile_get` and the Market's included, work on
that team's database.
- The local and server editions are unchanged: one team, team 1, is the
whole database. The multi-tenant edition still doesn't start: signing up
and choosing a team come in part 3.
- Checked on Postgres: two teams, each with a profile, a Market entry and a
key, see only their own, through the pages, Search, MCP and secrets; the
main schema holds none of it.
1. **Teams everywhere.**
- Every table that holds a company's data gets `team_id`, and its
unique keys include it. The settings table's key becomes