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
+24 -3
View File
@@ -87,7 +87,7 @@ func (s *Server) sessions(next http.Handler) http.Handler {
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), viewerKey, localOwner)))
return
}
if !publicPath(r.URL.Path) || strings.HasPrefix(r.URL.Path, "/signin") {
if !s.multi() && (!publicPath(r.URL.Path) || strings.HasPrefix(r.URL.Path, "/signin")) {
if done, err := s.hasOwner(); err != nil {
s.serverError(w, err)
return
@@ -119,11 +119,11 @@ func (s *Server) lookupSession(r *http.Request) (*viewer, error) {
if err != nil {
return nil, users.ErrNoSession
}
sess, u, err := users.Lookup(s.DB, c.Value, time.Now())
sess, u, err := users.Lookup(s.platform(), c.Value, time.Now())
if err != nil {
return nil, err
}
m, err := users.MembershipOf(s.DB, users.Team, u.ID)
m, err := users.MembershipOf(s.platform(), sess.Team, u.ID)
if errors.Is(err, users.ErrNotFound) {
return nil, users.ErrNoSession // no longer in the team
}
@@ -319,3 +319,24 @@ func withCSRF(r *http.Request, exec func(*bytes.Buffer) error) ([]byte, error) {
}
return rewriteForms(buf.Bytes(), csrfToken(r), r.URL.Path, v), nil
}
// teamServer is the Server for the request's team: the viewer's (the
// team their session works in), else this one.
func (s *Server) teamServer(r *http.Request) (*Server, error) {
if !s.multi() {
return s, nil
}
v := viewerOf(r)
if v.Session == nil {
return s, nil
}
return s.forTeam(v.Session.Team)
}
// team is the team this Server works for.
func (s *Server) team() int64 {
if s.Team == 0 {
return users.Team
}
return s.Team
}