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 NULL constraint

Why this is safe

References

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: 69044

bin/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: 68559

MR 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

Merge request reports

Loading