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:
@@ -0,0 +1,86 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTeamsSchemas(t *testing.T) {
|
||||
if !OnPostgres() {
|
||||
t.Skip("teams are Postgres's")
|
||||
}
|
||||
d, err := Open(filepath.Join(t.TempDir(), "app.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer d.Close()
|
||||
teams, err := d.Teams()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer teams.Close()
|
||||
if _, err := teams.Open(1); err == nil {
|
||||
t.Error("opened a team that wasn't created")
|
||||
}
|
||||
for _, id := range []int64{1, 2} {
|
||||
if err := teams.Create(id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
a, err := teams.Open(1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, _ := teams.Open(2)
|
||||
if a2, _ := teams.Open(1); a2 != a {
|
||||
t.Error("a second pool")
|
||||
}
|
||||
if v, err := a.SchemaVersion(); err != nil || v != Schema() {
|
||||
t.Errorf("team schema at %d: %v", v, err)
|
||||
}
|
||||
// Each team's own tables.
|
||||
if err := a.PutJSON("company", "Acme"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := b.PutJSON("company", "Bolt"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got string
|
||||
a.GetJSON("company", &got)
|
||||
if got != "Acme" {
|
||||
t.Errorf("team 1 sees %q", got)
|
||||
}
|
||||
b.GetJSON("company", &got)
|
||||
if got != "Bolt" {
|
||||
t.Errorf("team 2 sees %q", got)
|
||||
}
|
||||
var n int
|
||||
d.QueryRow(`SELECT count(*) FROM settings WHERE key = 'company'`).Scan(&n)
|
||||
if n != 0 {
|
||||
t.Error("a team's setting in the main schema")
|
||||
}
|
||||
// The shared tables: only in the main schema, reached from a team's.
|
||||
for _, tbl := range PlatformTables {
|
||||
var inTeam bool
|
||||
d.QueryRow(`SELECT to_regclass($1) IS NOT NULL`, teams.Schema(1)+"."+tbl).Scan(&inTeam)
|
||||
if inTeam {
|
||||
t.Errorf("%s is in the team's schema", tbl)
|
||||
}
|
||||
}
|
||||
if _, err := d.Exec(`INSERT INTO teams (name, created_at) VALUES ('Acme', 'now')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := a.QueryRow(`SELECT count(*) FROM teams`).Scan(&n); err != nil || n != 1 {
|
||||
t.Errorf("the shared teams table from a team: %d %v", n, err)
|
||||
}
|
||||
// Creating again only migrates; dropping removes it all.
|
||||
if err := teams.Create(1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := teams.Drop(2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := teams.Open(2); err == nil {
|
||||
t.Error("a dropped team opens")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user