Skip to content

chore: enforce data rules in the database - #6

Merged
its-gabo merged 1 commit into
mainfrom
gs_chore_enforce_database_checks
Nov 20, 2026
Merged

its-gabo merged 1 commit into
mainfrom
gs_chore_enforce_database_checks

Conversation

@its-gabo

Copy link
Copy Markdown
Owner

Summary

Until now, most rules about what a row may contain were checked only by the API. Postgres now enforces them too, so a bug, a script or a manual edit can't store an invalid row. Two values that used to be stored twice now have one source: an organization's status comes from its deletion request, and a storage's encrypted credentials sit in one column instead of six.

This PR also fixes a race where a membership, invitation or organization change could slip through while the organization's deletion was being scheduled.

Nothing changes for members or API clients.

Changes

Database rules

New CHECK constraints:

Table Rules
users, organization_invitations Emails are stored lowercased
users, organizations, libraries, storages, library_api_keys Names aren't blank
organizations The slug matches the slug format and is 2–63 characters long
videos Status, file format, processing status, poster source and failure reasons are known values
videos The failure reason is set exactly when the video failed, the processing status exactly when it's uploaded, and uploaded_at exactly when it's uploaded
videos Only a video being processed holds a processing run, sizes and durations aren't negative, and a frame poster has a frame time
libraries Transcode qualities aren't empty and are known values
library_api_keys Scopes aren't empty and are known values
activity_logs Actor kind, action and resource type are known values, and an API key actor has its key
video_views Device type is a known value, progress isn't negative, and the last report isn't before the start
Others Auth token purposes, invitation statuses, membership roles, outbox message types, webhook delivery statuses and event types are known values; attempt counters and upload offsets stay in range
  • Helpers. checkOneOf and checkNonEmptySubsetOf in database.utils.ts build the "known values" checks from the same constants the code uses.
  • Video views. A view's library and organization now have to match its video's, through a composite foreign key on videos (id, library_id, organization_id). The view is still deleted with its video.

Organization status

  • One source. organizations.status is dropped. An organization is pending deletion exactly while it has a deletion request. Queries derive the status with organizationStatusColumn.
  • Lock fix. lockActiveOrganization now locks the organization row first, then reads the deletion request in a separate query. Before, both happened in one query. A transaction that waited for the lock while a deletion was being scheduled could still see the organization as active and change it.

Storage credentials

  • One column. The six credentials_* columns are replaced by one credentials jsonb column, shaped like a webhook's signing secret.

Migrations

Migration What it does
0007_enforce_schema_rules Adds the constraints, the unique constraint on videos and the credentials column; drops organizations.status
0008_backfill_storage_credentials Copies the six credential columns into credentials
0009_finish_video_view_and_storage_credential_changes Adds the video_views foreign key, makes credentials required and drops the old columns

@its-gabo its-gabo self-assigned this Nov 20, 2026
@its-gabo
its-gabo merged commit 3d44811 into main Nov 20, 2026
1 check passed
@its-gabo
its-gabo deleted the gs_chore_enforce_database_checks branch November 20, 2026 00:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant