Schema Designed
The SQL for the Upload service's tables, in the upload schema. No other service can read them.
-- Upload service, version 1. Schema: upload.
-- IDs are UUIDv7 values generated by the service.
-- Checked with `npm run schema:check`: runs on PGlite, and every sample row passes every constraint.
create schema if not exists upload;
create table upload.files (
id uuid primary key,
owner_id uuid not null,
s3_key text not null,
file_name text not null,
mime text not null,
size_bytes bigint not null,
pages integer,
accept jsonb not null default '{}',
status text not null default 'pending_upload',
rejection_reason text,
sha256 text,
created_at timestamptz not null default now(),
completed_at timestamptz,
deleted_at timestamptz,
expires_at timestamptz not null,
constraint files_s3_key_key unique (s3_key),
constraint files_s3_key_check
check (s3_key like 'users/' || owner_id::text || '/' || id::text || '/original.%'),
constraint files_file_name_check
check (length(file_name) between 1 and 255 and file_name !~ '[/\\[:cntrl:]]'),
constraint files_mime_check
check (mime in ('application/pdf', 'image/png', 'image/jpeg', 'application/json', 'text/csv')),
constraint files_size_bytes_check check (size_bytes between 1 and 10485760),
constraint files_pages_check check (
pages is null
or (mime = 'application/pdf' and pages between 1 and 10)
or (mime in ('image/png', 'image/jpeg') and pages = 1)
),
constraint files_accept_check
check (accept - array['mimes', 'max_bytes', 'max_pages'] = '{}'::jsonb),
constraint files_status_check
check (status in ('pending_upload', 'ready', 'rejected', 'deleted', 'expired')),
constraint files_rejection_check check (
(status = 'rejected') = (rejection_reason is not null)
and (rejection_reason is null
or rejection_reason in ('type_mismatch', 'too_many_pages', 'unreadable', 'size_mismatch'))
),
constraint files_ready_check check (
status <> 'ready'
or (sha256 is not null and completed_at is not null
and (pages is not null or mime in ('application/json', 'text/csv')))
),
constraint files_sha256_check check (sha256 ~ '^[0-9a-f]{64}$'),
constraint files_deleted_check check ((status = 'deleted') = (deleted_at is not null)),
constraint files_dates_check check (
(completed_at is null or completed_at >= created_at)
and expires_at = created_at + interval '30 days'
)
);
-- A user's files, newest first, page by page.
create index files_owner_id_idx on upload.files (owner_id, created_at desc, id desc);
-- The daily clean-up of uploads that never arrived.
create index files_pending_idx on upload.files (created_at) where status = 'pending_upload';
-- Marking files expired when the bucket removes them.
create index files_expiry_idx on upload.files (expires_at) where status = 'ready';
create table upload.inbound_events (
event_id uuid primary key,
type text not null,
created_at timestamptz not null default now(),
constraint inbound_events_type_check check (length(type) between 1 and 100)
);
-- The service's database login reaches only this schema. Rows are never
-- deleted (a deleted file keeps its row, without content), and audit events
-- can be added and read but never changed.
grant usage on schema upload to upload_service;
grant select, insert, update on upload.files to upload_service;
grant select, insert on upload.inbound_events to upload_service;
grant select, insert on upload.audit_events to upload_service;
Schema level: enforced by the database, whatever the code does
- Primary keys on both tables;
s3_key is unique, so two files can never share an object. - The storage key must be built from the owner and file IDs.
- Type, size, page count, status and rejection reason accept only their listed values or ranges.
- A rejection reason exists exactly when a file is rejected; a deleted date exists exactly when it is deleted.
- A ready file always has its fingerprint and completion time.
expires_at is always exactly 30 days after created_at, matching the bucket's lifecycle rule.- Rows are never deleted: the service's login has no
DELETE grant. Audit events can be added and read, never changed. - Upload's database login can reach only the
upload schema. No other service can read these tables.
Constraints and indexes by table
upload.files
files_pkey: primary key (id)files_s3_key_key: unique (s3_key)files_s3_key_check: s3_key starts with users/{owner_id}/{id}/original.files_file_name_check: 1 to 255 characters, none of them /, \ or a control characterfiles_mime_check: mime is one of the five allowed typesfiles_size_bytes_check: size_bytes between 1 and 10,485,760files_pages_check: pages empty, or 1 to 10 for a PDF and exactly 1 for an image; always empty for JSON and CSVfiles_accept_check: accept has no keys other than mimes, max_bytes, max_pagesfiles_status_check: status is one of the five statusesfiles_rejection_check: rejection_reason is set exactly when status is rejected, and is one of the four reasonsfiles_ready_check: a ready file has sha256, completed_at and, unless it is JSON or CSV, pagesfiles_sha256_check: sha256 is 64 lower-case hex charactersfiles_deleted_check: deleted_at is set exactly when status is deletedfiles_dates_check: completed_at is not before created_at; expires_at is exactly 30 days after created_atfiles_owner_id_idx: index on (owner_id, created_at desc, id desc), for listing a user's files page by pagefiles_pending_idx: partial index on (created_at) where status = 'pending_upload', for the daily clean-upfiles_expiry_idx: partial index on (expires_at) where status = 'ready', for marking files expired
upload.inbound_events
inbound_events_pkey: primary key (event_id); inserting a duplicate fails, which is how a repeat is detectedinbound_events_type_check: type is 1 to 100 characters
Sample data
Common queries
List a user's files, one page (`GET /files`)
select id, file_name, mime, size_bytes, pages, status, created_at
from upload.files
where owner_id = $1
and status in ('pending_upload', 'ready', 'rejected')
and (created_at, id) < ($2, $3) -- from the cursor; omitted on the first page
order by created_at desc, id desc
limit $4;
Mark a checked file ready (`POST /files/{fileId}/complete`)
update upload.files
set status = 'ready', pages = $3, sha256 = $4, completed_at = now()
where id = $1 and owner_id = $2 and status = 'pending_upload'
returning *; -- no row: already checked, or not this user's file
Find abandoned uploads (daily clean-up)
select id, s3_key
from upload.files
where status = 'pending_upload'
and created_at < now() - interval '24 hours';
Schema on GitHub →