Add NOT NULL constraint to keys.organization_id
What does this MR do and why?
Enforces non-nullability of keys.organization_id. This column is the sharding key for this table.
- Make the model association non-optional
- Add the DB-level
NOT NULLconstraint
Why this is safe
- All places in the application that write to
keysnow set a value fororganization_id - All rows in
keyshave been backfilled with a value fororganization_id - A query (on .com) confirms every row has a value set for
organization_id: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/50906/commands/150786 - The first backfill was finalized in !217323 (merged), and the second backfill is being finalized in !231996 (merged).
References
- Tracking issue: #577246
Screenshots or screen recordings
N/A
How to set up and validate locally
bin/rails db:migrate:up:main VERSION=20260420200000
Command output
% bin/rails db:migrate:up:main VERSION=20260420200000
main: == [advisory_lock_connection] object_id: 140360, pg_backend_pid: 69044
main: == 20260417185928 AddNotNullConstraintToKeysOrganizationId: migrating =========
main: -- current_schema(nil)
main: -> 0.0008s
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- execute("ALTER TABLE keys\nADD CONSTRAINT check_8933ae4f38\nCHECK ( organization_id IS NOT NULL )\nNOT VALID;\n")
main: -> 0.0009s
main: -- execute("SET statement_timeout TO 0")
main: -> 0.0003s
main: -- execute("ALTER TABLE keys VALIDATE CONSTRAINT check_8933ae4f38;")
main: -> 0.0007s
main: -- execute("RESET statement_timeout")
main: -> 0.0003s
main: == 20260417185928 AddNotNullConstraintToKeysOrganizationId: migrated (0.0482s)
main: == [advisory_lock_connection] object_id: 140360, pg_backend_pid: 69044bin/rails db:migrate:down:main VERSION=20260420200000
Command output
% bin/rails db:migrate:down:main VERSION=20260420200000
main: == [advisory_lock_connection] object_id: 140340, pg_backend_pid: 68559
main: == 20260417185928 AddNotNullConstraintToKeysOrganizationId: reverting =========
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- execute(" ALTER TABLE keys\n DROP CONSTRAINT IF EXISTS check_8933ae4f38\n")
main: -> 0.0036s
main: == 20260417185928 AddNotNullConstraintToKeysOrganizationId: reverted (0.0238s)
main: == [advisory_lock_connection] object_id: 140340, pg_backend_pid: 68559MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Related to #577246
Edited by Jason Knabl