Repository navigation
chore: enforce data rules in the database - #6
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
users,organization_invitationsusers,organizations,libraries,storages,library_api_keysorganizationsvideosvideosuploaded_atexactly when it's uploadedvideoslibrarieslibrary_api_keysactivity_logsvideo_viewscheckOneOfandcheckNonEmptySubsetOfindatabase.utils.tsbuild the "known values" checks from the same constants the code uses.videos (id, library_id, organization_id). The view is still deleted with its video.Organization status
organizations.statusis dropped. An organization is pending deletion exactly while it has a deletion request. Queries derive the status withorganizationStatusColumn.lockActiveOrganizationnow 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
credentials_*columns are replaced by onecredentialsjsonb column, shaped like a webhook's signing secret.Migrations
0007_enforce_schema_rulesvideosand thecredentialscolumn; dropsorganizations.status0008_backfill_storage_credentialscredentials0009_finish_video_view_and_storage_credential_changesvideo_viewsforeign key, makescredentialsrequired and drops the old columns