Make organization_id NOT NULL on oauth_applications table
Summary
Add a NOT NULL constraint to the organization_id column on the oauth_applications table after backfill and application code changes are complete.
Context
This is the final step in adding the sharding key to oauth_applications. After the column is added, backfilled, and application code is updated, we need to enforce the constraint.
Prerequisites
All of the following must be completed before this issue:
-
organization_idcolumn added to table -
Background migration to backfill existing records is complete -
Application code updated to set organization_id on creation -
Verification that all records have organization_id set
Implementation Steps
-
Validate Data Completeness
- Query to confirm no NULL values exist:
SELECT COUNT(*) FROM oauth_applications WHERE organization_id IS NULL; - Should return 0
- Query to confirm no NULL values exist:
-
Add NOT NULL Constraint
- Create migration to add constraint
- Use
add_not_null_constrainthelper for zero-downtime deployment - Validate constraint before finalizing
-
Update Schema Documentation
- Update
db/docs/oauth_applications.yml - Mark
organization_idas required - Update sharding key documentation
- Update
-
Update Model Validations
- Add
validates :organization_id, presence: trueto model - Ensure validation aligns with database constraint
- Add
Migration Example
class AddNotNullConstraintToOauthApplicationsOrganizationId < Gitlab::Database::Migration[2.2]
disable_ddl_transaction!
def up
add_not_null_constraint :oauth_applications, :organization_id
end
def down
remove_not_null_constraint :oauth_applications, :organization_id
end
end
Validation
-
Constraint successfully added in development -
Constraint successfully added in staging -
No errors in production deployment -
Model validations working correctly -
Cannot create application without organization_id
Rollback Plan
If issues are discovered:
- Remove NOT NULL constraint
- Investigate missing organization_id cases
- Fix application code or backfill issues
- Re-attempt constraint addition
Dependencies
- Blocked by: All previous oauth_applications organization_id issues
- Related: #553465 (closed)
References
Edited by 🤖 GitLab Bot 🤖