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
+36 -5
View File
@@ -17,8 +17,18 @@ type Migration struct {
Note string // what it does, for the record (Postgres' schema_migrations)
SQLite string
Postgres string // empty: PostgresSQL(SQLite)
// Platform marks a migration that only touches the tables every team
// shares (people, sessions, keys, locks): on the multi-tenant edition
// it runs in the main schema, never in a team's (see Teams). A
// migration is one or the other, never both.
Platform bool
}
// PlatformTables are the tables every team shares on the multi-tenant
// edition, in the main schema; every other table is a team's, in its own
// schema.
var PlatformTables = []string{"instances", "locks", "users", "memberships", "sessions", "links", "throttle", "agent_keys", "teams"}
// both are the migrations after schema 29, on both databases: schema 30
// is both[0]. (migrations, before them, are SQLite's own; Postgres starts
// from postgres.sql, which is schema 29 as a whole.)
@@ -37,7 +47,8 @@ CREATE TABLE secrets (
},
// 31
{
Note: "copies of the app, and the locks that let only one of them do a job",
Note: "copies of the app, and the locks that let only one of them do a job",
Platform: true,
SQLite: `
CREATE TABLE instances (
id TEXT PRIMARY KEY, -- a copy of the app, while it runs
@@ -56,7 +67,8 @@ CREATE TABLE locks (
},
// 32
{
Note: "users, their memberships of the team, and their sign-in sessions (server and multi-tenant editions)",
Note: "users, their memberships of the team, and their sign-in sessions (server and multi-tenant editions)",
Platform: true,
SQLite: `
CREATE TABLE users (
id INTEGER PRIMARY KEY,
@@ -94,7 +106,8 @@ CREATE INDEX sessions_user ON sessions(user_id);
},
// 33
{
Note: "one-time links: setting up the first owner, password resets, invitations",
Note: "one-time links: setting up the first owner, password resets, invitations",
Platform: true,
SQLite: `
CREATE TABLE links (
id TEXT PRIMARY KEY, -- the SHA-256 of the link's random token
@@ -108,7 +121,8 @@ CREATE INDEX links_user ON links(user_id, purpose);
},
// 34
{
Note: "signing in: failed attempts and locks, two-factor sign-in, and limits on tries",
Note: "signing in: failed attempts and locks, two-factor sign-in, and limits on tries",
Platform: true,
SQLite: `
ALTER TABLE users ADD COLUMN failed_signins INTEGER NOT NULL DEFAULT 0; -- in a row, since the last sign-in
ALTER TABLE users ADD COLUMN locked_at TEXT NOT NULL DEFAULT ''; -- locked after too many; '' when not
@@ -169,7 +183,8 @@ ALTER TABLE outreach ADD COLUMN created_by INTEGER NOT NULL DEFAULT 0;
},
// 37
{
Note: "agents' keys, and a description for each agent",
Note: "agents' keys, and a description for each agent",
Platform: true,
SQLite: `
CREATE TABLE agent_keys (
id INTEGER PRIMARY KEY,
@@ -240,6 +255,22 @@ CREATE TABLE agent_calls (
ms INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX agent_calls_agent ON agent_calls(agent_id, at);
`,
},
// 40
{
Note: "teams, and which team each sign-in session works in (multi-tenant edition)",
Platform: true,
SQLite: `
CREATE TABLE teams (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
domain TEXT NOT NULL DEFAULT '', -- the one website domain it works on, such as example.com
status TEXT NOT NULL DEFAULT 'active', -- active, suspended or deleting
created_at TEXT NOT NULL,
delete_at TEXT NOT NULL DEFAULT '' -- deleting: when its data goes for good
);
ALTER TABLE sessions ADD COLUMN team_id INTEGER NOT NULL DEFAULT 1;
`,
},
}