feat(supabase): files foundation — assets table, private buckets, avatar policies through assets, purge queue, browser and worker RPCs

Migration 20261013100000: asset_status enum, public.assets (server-built keys,
lease token, closed rejection reasons), seven private buckets (avatars open with
cap and MIME list, six closed), storage.objects SELECT/INSERT policies for
avatars evaluated through assets, private.object_purge_queue fed by status and
delete triggers (48 h / 1 h grace), storage ceiling, RPCs begin_avatar_upload /
confirm_asset_upload / remove_avatar (authenticated) and claim_avatar_jobs /
complete_avatar_job / reject_avatar_job / mark_quarantine_purged / claim_purges /
ack_purge / claim_quarantine_leftovers / expire_pending_uploads (service_role),
explicit revokes and column grants. pgTAP 010 (125 checks: allowed and denied
twins, policy census, stale lease, supersede, removal, leftovers, cascade,
ceiling), 001 census extended, database.types.ts regenerated from the real
stack, schema-types test counts input arguments only.
This commit is contained in:
bchanot committed 2026-10-13 19:32:15 +02:00
1 parent 3f1105bf39
commit 614887470c
5 files changed
+1346 -6

No files matched your search

+135
View File
@@ -239,6 +239,75 @@ export type Database = {
},
]
}
assets: {
Row: {
attempts: number
bucket_id: string
byte_size: number | null
created_at: string
declared_mime: string
declared_size: number
id: string
kind: string
lock_token: string | null
locked_at: string | null
object_key: string
owner_id: string
quarantine_key: string | null
rejection_reason: string | null
sha256: string | null
status: Database['public']['Enums']['asset_status']
updated_at: string
}
ComputedFields: never
Insert: {
attempts?: number
bucket_id: string
byte_size?: number | null
created_at?: string
declared_mime: string
declared_size: number
id?: string
kind: string
lock_token?: string | null
locked_at?: string | null
object_key: string
owner_id: string
quarantine_key?: string | null
rejection_reason?: string | null
sha256?: string | null
status?: Database['public']['Enums']['asset_status']
updated_at?: string
}
Update: {
attempts?: number
bucket_id?: string
byte_size?: number | null
created_at?: string
declared_mime?: string
declared_size?: number
id?: string
kind?: string
lock_token?: string | null
locked_at?: string | null
object_key?: string
owner_id?: string
quarantine_key?: string | null
rejection_reason?: string | null
sha256?: string | null
status?: Database['public']['Enums']['asset_status']
updated_at?: string
}
Relationships: [
{
foreignKeyName: 'assets_owner_id_fkey'
columns: ['owner_id']
isOneToOne: false
referencedRelation: 'profiles'
referencedColumns: ['id']
},
]
}
profile_roles: {
Row: {
granted_at: string
@@ -380,7 +449,50 @@ export type Database = {
[_ in never]: never
}
Functions: {
ack_purge: { Args: { purge_id: string }; Returns: undefined }
archive_anatomical_sheet: { Args: { sheet_id: string }; Returns: string }
begin_avatar_upload: {
Args: { declared_mime: string; declared_size: number }
Returns: Json
}
claim_avatar_jobs: {
Args: { max_jobs: number }
Returns: {
asset_id: string
attempts: number
bucket_id: string
lock_token: string
object_key: string
owner_id: string
quarantine_key: string
}[]
}
claim_purges: {
Args: { max_items: number }
Returns: {
bucket_id: string
object_name: string
purge_id: string
}[]
}
claim_quarantine_leftovers: {
Args: { max_items: number; older_than_seconds: number }
Returns: {
asset_id: string
bucket_id: string
object_name: string
}[]
}
complete_avatar_job: {
Args: {
asset_id: string
byte_size: number
lock_token: string
sha256: string
}
Returns: undefined
}
confirm_asset_upload: { Args: { asset_id: string }; Returns: undefined }
confirm_sheet_asset_upload: {
Args: { asset_id: string }
Returns: string
@@ -412,6 +524,10 @@ export type Database = {
Args: { reason?: string; version_id: string }
Returns: string
}
expire_pending_uploads: {
Args: { max_age_seconds: number }
Returns: number
}
fork_anatomical_sheet: {
Args: {
name: string
@@ -428,6 +544,7 @@ export type Database = {
}
Returns: string
}
mark_quarantine_purged: { Args: { asset_id: string }; Returns: undefined }
publish_sheet_version: { Args: { version_id: string }; Returns: string }
register_sheet_asset: {
Args: {
@@ -438,6 +555,11 @@ export type Database = {
}
Returns: string
}
reject_avatar_job: {
Args: { asset_id: string; lock_token: string; reason: string }
Returns: undefined
}
remove_avatar: { Args: Record<PropertyKey, never>; Returns: number }
restore_anatomical_sheet: { Args: { sheet_id: string }; Returns: string }
revoke_platform_role: {
Args: {
@@ -456,6 +578,12 @@ export type Database = {
}
}
Enums: {
asset_status:
| 'pending_upload'
| 'uploaded'
| 'clean'
| 'rejected'
| 'deleted'
platform_role: 'owner' | 'admin' | 'moderator' | 'support'
sheet_status: 'active' | 'archived' | 'deleted'
sheet_version_status: 'draft' | 'published' | 'deprecated'
@@ -587,6 +715,13 @@ export type CompositeTypes<
export const Constants = {
public: {
Enums: {
asset_status: [
'pending_upload',
'uploaded',
'clean',
'rejected',
'deleted',
],
platform_role: ['owner', 'admin', 'moderator', 'support'],
sheet_status: ['active', 'archived', 'deleted'],
sheet_version_status: ['draft', 'published', 'deprecated'],
@@ -66,6 +66,25 @@ const expectedColumns = {
'updated_at',
'visibility',
],
assets: [
'attempts',
'bucket_id',
'byte_size',
'created_at',
'declared_mime',
'declared_size',
'id',
'kind',
'lock_token',
'locked_at',
'object_key',
'owner_id',
'quarantine_key',
'rejection_reason',
'sha256',
'status',
'updated_at',
],
profile_roles: [
'granted_at',
'granted_by',
@@ -109,7 +128,14 @@ type FunctionArgs = {
string)[]
}
const expectedFunctionArgs = {
ack_purge: ['purge_id'],
archive_anatomical_sheet: ['sheet_id'],
begin_avatar_upload: ['declared_mime', 'declared_size'],
claim_avatar_jobs: ['max_jobs'],
claim_purges: ['max_items'],
claim_quarantine_leftovers: ['max_items', 'older_than_seconds'],
complete_avatar_job: ['asset_id', 'byte_size', 'lock_token', 'sha256'],
confirm_asset_upload: ['asset_id'],
confirm_sheet_asset_upload: ['asset_id'],
create_anatomical_sheet: [
'description',
@@ -125,20 +151,28 @@ const expectedFunctionArgs = {
delete_sheet_asset: ['asset_id'],
delete_sheet_version: ['version_id'],
deprecate_sheet_version: ['reason', 'version_id'],
expire_pending_uploads: ['max_age_seconds'],
fork_anatomical_sheet: ['name', 'slug', 'source_version_id', 'visibility'],
grant_platform_role: ['role', 'target_user_id'],
mark_quarantine_purged: ['asset_id'],
publish_sheet_version: ['version_id'],
register_sheet_asset: ['byte_size', 'file_name', 'sha256', 'sheet_id'],
reject_avatar_job: ['asset_id', 'lock_token', 'reason'],
remove_avatar: [],
restore_anatomical_sheet: ['sheet_id'],
revoke_platform_role: ['reason', 'role', 'target_user_id'],
set_sheet_visibility: ['sheet_id', 'visibility'],
} as const satisfies FunctionArgs
// A function without arguments is generated as `Record<PropertyKey, never>`
// (every key, no value): its key set is open, so only named keys are checked.
type MissingArgs = {
[F in keyof Functions]: Exclude<
keyof Functions[F]['Args'],
(typeof expectedFunctionArgs)[F][number]
>
[F in keyof Functions]: string extends keyof Functions[F]['Args']
? never
: Exclude<
keyof Functions[F]['Args'],
(typeof expectedFunctionArgs)[F][number]
>
}[keyof Functions]
const noMissingArg: MissingArgs extends never ? true : never = true
@@ -216,9 +250,15 @@ describe('database.types.ts matches the migrated schema', () => {
expect(actual).toEqual(expected)
})
// Input arguments only: a `returns table` function also names its output columns in proargnames.
it('lists exactly the public functions and their argument names', async () => {
const { rows } = await db.query<FunctionRow>(
`select p.proname as name, coalesce(p.proargnames, '{}') as args
`select p.proname as name,
case when p.proargmodes is null then coalesce(p.proargnames, '{}')
else coalesce((select array_agg(u.n order by u.i)
from unnest(p.proargnames, p.proargmodes) with ordinality as u(n, m, i)
where u.m in ('i', 'b', 'v')), '{}')
end as args
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public' and p.prokind = 'f'
and not exists (