feat(supabase): workspaces M1, one personal workspace per account

V1-1.1 (contract V1-1_OWNERS_ANIMALS.md §4, §8, §9). Tables workspaces and workspace_members, created with the profile by trigger through one atomic, idempotent write path; deferred invariant (a personal workspace has exactly its account as holder); backfill of existing profiles; access helpers caller_workspace_ids (active profile, confirmed email) and writable_workspace_ids; RLS select for members only; no privilege for anon or service_role. log_event keeps x-correlation-id only as a short token. pgTAP 012 (85 checks), census and types updated, config test.
This commit is contained in:
bchanot committed 2026-10-25 19:53:16 +01:00
1 parent 5d734be512
commit 7bf4fb6079
6 files changed
+923 -1

No files matched your search

+71
View File
@@ -444,6 +444,73 @@ export type Database = {
}
Relationships: []
}
workspace_members: {
Row: {
created_at: string
member_role: Database['public']['Enums']['workspace_member_role']
user_id: string
workspace_id: string
}
ComputedFields: never
Insert: {
created_at?: string
member_role: Database['public']['Enums']['workspace_member_role']
user_id: string
workspace_id: string
}
Update: {
created_at?: string
member_role?: Database['public']['Enums']['workspace_member_role']
user_id?: string
workspace_id?: string
}
Relationships: [
{
foreignKeyName: 'workspace_members_user_id_fkey'
columns: ['user_id']
isOneToOne: false
referencedRelation: 'profiles'
referencedColumns: ['id']
},
{
foreignKeyName: 'workspace_members_workspace_id_fkey'
columns: ['workspace_id']
isOneToOne: false
referencedRelation: 'workspaces'
referencedColumns: ['id']
},
]
}
workspaces: {
Row: {
created_at: string
id: string
kind: Database['public']['Enums']['workspace_kind']
personal_user_id: string | null
}
ComputedFields: never
Insert: {
created_at?: string
id?: string
kind: Database['public']['Enums']['workspace_kind']
personal_user_id?: string | null
}
Update: {
created_at?: string
id?: string
kind?: Database['public']['Enums']['workspace_kind']
personal_user_id?: string | null
}
Relationships: [
{
foreignKeyName: 'workspaces_personal_user_id_fkey'
columns: ['personal_user_id']
isOneToOne: true
referencedRelation: 'profiles'
referencedColumns: ['id']
},
]
}
}
Views: {
[_ in never]: never
@@ -588,6 +655,8 @@ export type Database = {
sheet_status: 'active' | 'archived' | 'deleted'
sheet_version_status: 'draft' | 'published' | 'deprecated'
sheet_visibility: 'private' | 'public'
workspace_kind: 'personal'
workspace_member_role: 'holder'
}
CompositeTypes: {
[_ in never]: never
@@ -726,6 +795,8 @@ export const Constants = {
sheet_status: ['active', 'archived', 'deleted'],
sheet_version_status: ['draft', 'published', 'deprecated'],
sheet_visibility: ['private', 'public'],
workspace_kind: ['personal'],
workspace_member_role: ['holder'],
},
},
} as const
+38
View File
@@ -0,0 +1,38 @@
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { supabaseDir } from './harness'
/**
* Settings the workspace access rule depends on (V1-1 contract §6.2, §9.1).
* private.caller_workspace_ids() requires auth.users.email_confirmed_at, but
* with confirmations off GoTrue stamps that column at sign-up; the helpers
* live in `private`, which must stay out of the PostgREST schemas.
*/
async function readSetting(
section: string,
key: string,
): Promise<string | undefined> {
const config = await readFile(join(supabaseDir, 'config.toml'), 'utf8')
let current = ''
for (const line of config.split('\n')) {
const header = line.match(/^\[([^\]]+)\]\s*$/)
if (header) current = header[1]
const setting = line.match(/^(\w+)\s*=\s*(.+?)\s*$/)
if (current === section && setting?.[1] === key) return setting[2]
}
return undefined
}
describe('config.toml settings the access rules rely on', () => {
it('requires email confirmation before any session', async () => {
expect(await readSetting('auth.email', 'enable_confirmations')).toBe('true')
})
it('keeps anonymous sign-ins off (no account without an email)', async () => {
expect(await readSetting('auth', 'enable_anonymous_sign_ins')).toBe('false')
})
it('exposes only the public schema through PostgREST', async () => {
expect(await readSetting('api', 'schemas')).toBe('["public"]')
})
})
@@ -112,6 +112,8 @@ const expectedColumns = {
'updated_at',
],
species: ['active', 'created_at', 'id', 'name_en', 'name_fr', 'sort_order'],
workspace_members: ['created_at', 'member_role', 'user_id', 'workspace_id'],
workspaces: ['created_at', 'id', 'kind', 'personal_user_id'],
} as const satisfies ColumnMap
// Compile-time: a column present in the type but absent from the list is an error.