Skip to content

[M2.3b1] Persist organizations and tenant-bound resources - #77

Merged
samuelmbabhazi merged 1 commit into
mainfrom
feat/m2.3b1-tenant-schema
Nov 5, 2026
Merged

samuelmbabhazi merged 1 commit into
mainfrom
feat/m2.3b1-tenant-schema

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Task

M2.3b1, stacked on #76 (feat/m2.3a-rbac-policy). This slice adds storage constraints; M2.3b2 adds RLS and last-owner database guards before tenant routes are exposed.

What changed and why

Add organizations, memberships, projects, environments and role bindings to Prisma and SQL. Composite foreign keys include organization and parent project identifiers, rejecting cross-tenant and sibling-project references even outside application validation. A NULLS NOT DISTINCT index prevents duplicate project-wide bindings as well as duplicate environment bindings. User deletion is restricted while memberships remain.

Constrain roles, configuration-write modes, environment kinds, failover policies, names and slugs. Projects default to pull-request writes; environments default to protected with no automatic failover. Existing fleet references are tracked separately in M2.3b3 so orphan records are not silently assigned to an arbitrary organization.

Validation

  • Six real PostgreSQL tests cover migration reapplication, scoped uniqueness, cross-tenant insert/update rejection, sibling-project rejection, null-scope uniqueness, invalid states, safe defaults and cascade boundaries.
  • All 84 existing PostgreSQL integration tests passed with the migration. The first full run exposed an ambiguous fixture (a duplicate-name error preceded the intended foreign-key rejection); the fixture now uses a distinct name and all six new tests pass on rerun.
  • Prisma formatting/generation, API/web unit tests and builds, lint and typechecks pass. Shared domain tests, Go tests and buf lint passed in this session; their sources are unchanged by this slice.

Security considerations

No new endpoint, permission grant or runtime tenant query is exposed. Foreign-key isolation is not a substitute for RLS; tenant read/write policies and concurrent ownership protection remain required in M2.3b2 before HTTP access. No domain state changes are initiated by application code in this slice, so no new audit event path is needed. No dependencies, secrets or application shell calls are added.

Checklist

  • M2.3b1 checked; remaining isolation/integration work explicit in PLAN
  • PostgreSQL constraints and malicious cross-resource references tested
  • Lint, typechecks, unit tests and builds pass
  • No new secret exposure or application shell execution
  • SPEC/PLAN updated; no runtime mutation to audit

@samuelmbabhazi
samuelmbabhazi changed the base branch from feat/m2.3a-rbac-policy to main November 5, 2026 12:43
@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m2.3b1-tenant-schema branch from 8eecc56 to 5bac68c Compare November 5, 2026 12:44
@samuelmbabhazi
samuelmbabhazi merged commit 7997819 into main Nov 5, 2026
10 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/m2.3b1-tenant-schema branch November 5, 2026 12:44
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
…#78)

## Task

M2.3b2, stacked on #77 (`feat/m2.3b1-tenant-schema`).

## What changed and why

Force PostgreSQL row security on organizations, memberships, projects,
environments and role bindings. A transaction-local organization context
scopes reads and writes; missing context denies access. Deferred
constraint triggers require every surviving organization to retain an
owner and permit atomic ownership transfers. A serialized organization
write prevents concurrent owner removals, including stale snapshots at
REPEATABLE READ. Checks retain their stored tenant when a caller changes
context before commit. Migration refuses pre-existing ownerless
organizations.

## Validation

- All 98 real PostgreSQL integration tests pass, including eight new
isolation/concurrency tests and the six tenant-schema tests.
- Restricted-role tests cover all five tables, foreign
reads/updates/deletes, missing-context and foreign writes, table-owner
FORCE RLS, context restoration, ownership transfer, organization
deletion and concurrent last-owner protection at two isolation levels.
- API lint/typechecks pass. Workspace TypeScript tests/builds, Go tests
and buf lint passed in this session; their sources are unchanged by this
SQL slice.

## Security considerations

The trigger runs with invoker privileges, a fixed search path and
qualified table names. No security-definer bypass, new dependency,
secret or application shell invocation is added. This slice exposes no
tenant endpoint or application mutation. M2.3c must enforce a restricted
runtime database role and transaction context before tenant routes are
enabled; superusers and BYPASSRLS roles still bypass PostgreSQL RLS.
Fleet foreign keys remain tracked in M2.3b3.

## Checklist

- [x] M2.3b2 acceptance and malicious cross-tenant cases covered
- [x] Concurrent last-owner removals tested against real PostgreSQL
- [x] Lint, types and integration suite pass
- [x] SPEC, threat model and PLAN updated
- [x] No new application mutation requiring an audit event
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant