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
+34 -7
View File
@@ -37,7 +37,7 @@ func OpenPostgres(url string) (*DB, error) {
sqldb.Close()
return nil, fmt.Errorf("opening Postgres: %w", err)
}
d := &DB{SQL: sqldb, Dialect: Postgres}
d := &DB{SQL: sqldb, Dialect: Postgres, url: url}
if err := d.migratePostgres(); err != nil {
sqldb.Close()
return nil, err
@@ -53,7 +53,12 @@ var AppVersion = "dev"
// transaction: a failure leaves the database as it was. One copy of the
// app at a time does it (an advisory lock), so several can start
// together. Each step is recorded in schema_migrations.
func (d *DB) migratePostgres() error {
func (d *DB) migratePostgres() error { return d.migrateSchema(false) }
// migrateSchema migrates the schema the connection's search_path names
// first: the main one, or (team) a team's, which skips the platform's
// migrations and never has the platform's tables.
func (d *DB) migrateSchema(team bool) error {
if err := checkMigrations(); err != nil {
return err
}
@@ -62,7 +67,7 @@ func (d *DB) migratePostgres() error {
return err
}
defer tx.Rollback()
if _, err := tx.Exec(`SELECT pg_advisory_xact_lock(4708421)`); err != nil { // any fixed number: "the schema"
if _, err := tx.Exec(`SELECT pg_advisory_xact_lock(4708421, hashtext(current_schema()))`); err != nil { // any fixed number: "the schema", then which
return err
}
if _, err := tx.Exec(`CREATE TABLE IF NOT EXISTS schema_migrations (
@@ -77,8 +82,10 @@ func (d *DB) migratePostgres() error {
_, err := tx.Exec(`INSERT INTO schema_migrations (version, note, app) VALUES ($1, $2, $3)`, version, note, AppVersion)
return err
}
// In this schema itself: a team's search_path falls back to the main
// schema, which has its own.
var exists bool
if err := tx.QueryRow(`SELECT to_regclass('schema_version') IS NOT NULL`).Scan(&exists); err != nil {
if err := tx.QueryRow(`SELECT to_regclass(quote_ident(current_schema()) || '.schema_version') IS NOT NULL`).Scan(&exists); err != nil {
return err
}
version := pgBaseline
@@ -109,8 +116,13 @@ func (d *DB) migratePostgres() error {
}
for i := version; i < Schema(); i++ {
m, q := postgresStep(i)
if _, err := tx.Exec(q); err != nil {
return fmt.Errorf("migration %d (%s): %w", i+1, m.Note, err)
if team && m.Platform {
q = "" // the platform's tables aren't in a team's schema
}
if q != "" {
if _, err := tx.Exec(q); err != nil {
return fmt.Errorf("migration %d (%s): %w", i+1, m.Note, err)
}
}
if _, err := tx.Exec(`UPDATE schema_version SET version = $1`, i+1); err != nil {
return err
@@ -174,7 +186,22 @@ func openTestSchema(base string) (*DB, error) {
d.SQL.SetMaxOpenConns(4)
d.closeHook = func() error {
defer admin.Close()
_, err := admin.Exec(`DROP SCHEMA ` + schema + ` CASCADE`)
// Its teams' schemas too (Teams names them after it).
rows, err := admin.Query(`SELECT nspname FROM pg_namespace WHERE nspname LIKE $1`, schema+`\_team\_%`)
if err == nil {
var teams []string
for rows.Next() {
var n string
if rows.Scan(&n) == nil {
teams = append(teams, n)
}
}
rows.Close()
for _, n := range teams {
admin.Exec(`DROP SCHEMA ` + n + ` CASCADE`)
}
}
_, err = admin.Exec(`DROP SCHEMA ` + schema + ` CASCADE`)
return err
}
return d, nil