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
+11 -5
View File
@@ -32,6 +32,7 @@ type Session struct {
Expires time.Time
IP string
UserAgent string
Team int64 // the team it works in (multi-tenant: switchable)
}
// ErrNoSession is a cookie that leads nowhere: unknown, ended or expired,
@@ -54,6 +55,11 @@ func NewToken() (string, error) {
// StartSession signs a user in: it returns the token for the cookie.
func StartSession(d *db.DB, user int64, ip, userAgent string, now time.Time) (string, *Session, error) {
return StartSessionIn(d, user, Team, ip, userAgent, now)
}
// StartSessionIn signs a user in to work in a team.
func StartSessionIn(d *db.DB, user, team int64, ip, userAgent string, now time.Time) (string, *Session, error) {
token, err := NewToken()
if err != nil {
return "", nil, err
@@ -61,9 +67,9 @@ func StartSession(d *db.DB, user int64, ip, userAgent string, now time.Time) (st
if len(userAgent) > 300 {
userAgent = userAgent[:300]
}
s := &Session{ID: hashToken(token), User: user, Created: now, Seen: now, Expires: expiry(now, now), IP: ip, UserAgent: userAgent}
_, err = d.Exec(`INSERT INTO sessions (id, user_id, created_at, seen_at, expires_at, ip, user_agent) VALUES (?, ?, ?, ?, ?, ?, ?)`,
s.ID, s.User, ts(s.Created), ts(s.Seen), ts(s.Expires), s.IP, s.UserAgent)
s := &Session{ID: hashToken(token), User: user, Created: now, Seen: now, Expires: expiry(now, now), IP: ip, UserAgent: userAgent, Team: team}
_, err = d.Exec(`INSERT INTO sessions (id, user_id, created_at, seen_at, expires_at, ip, user_agent, team_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
s.ID, s.User, ts(s.Created), ts(s.Seen), ts(s.Expires), s.IP, s.UserAgent, s.Team)
if err != nil {
return "", nil, err
}
@@ -79,12 +85,12 @@ func expiry(created, seen time.Time) time.Time {
return max
}
const sessionCols = `id, user_id, created_at, seen_at, expires_at, ip, user_agent`
const sessionCols = `id, user_id, created_at, seen_at, expires_at, ip, user_agent, team_id`
func scanSession(row interface{ Scan(...any) error }) (*Session, error) {
var s Session
var created, seen, expires string
if err := row.Scan(&s.ID, &s.User, &created, &seen, &expires, &s.IP, &s.UserAgent); err != nil {
if err := row.Scan(&s.ID, &s.User, &created, &seen, &expires, &s.IP, &s.UserAgent, &s.Team); err != nil {
return nil, err
}
s.Created, s.Seen, s.Expires = parseTS(created), parseTS(seen), parseTS(expires)