Backport of 'Backfill NULL organization_id on oauth_applications before constraint validation' to 19.0
What does this MR do and why?
Backport of !242492 (merged) to 19-0-stable-ee.
The 20260417155126 migration (shipped in 19.0) adds a NOT NULL constraint to oauth_applications.organization_id, but assumes all rows were already backfilled by the 18.9 batched background migration. That holds on GitLab.com, but not on GitLab Dedicated and self-managed instances, where oauth_applications rows (e.g. GitLab Pages, Mattermost) can be created via gitlab-ctl reconfigure or CNG's custom-instance-setup without an organization_id. The constraint validation then fails with PG::CheckViolation, blocking the upgrade.
The fix converts 20260417155126 to a no-op and reintroduces the work as two single-mode post-deployment migrations: 20260625131308 backfills NULL organization_id to the default (root) organization in batches (DML), and 20260625131309 adds the NOT NULL constraint (DDL), running after the backfill so validation succeeds on all instance types. Clean cherry-pick of the default-branch commit.
NOTE:
Database queries
Backfill (20260625131308)
each_batch iterates by primary key and filters organization_id IS NULL within each batch. Per batch it issues a boundary lookup and an update:
SELECT id FROM oauth_applications
WHERE organization_id IS NULL AND id >= :start
ORDER BY id ASC LIMIT 1 OFFSET 1000;
UPDATE oauth_applications SET organization_id = 1
WHERE organization_id IS NULL AND id >= :start AND id < :next_start;The final batch has no upper bound: UPDATE ... WHERE organization_id IS NULL AND id >= :start.
** Limit (cost=1.96..1.97 rows=1 width=4) (actual time=0.050..0.051 rows=0 loops=1)
Buffers: shared hit=6
-> Sort (cost=1.96..1.96 rows=1 width=4) (actual time=0.049..0.050 rows=0 loops=1)
Sort Key: oauth_applications.id
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=6
-> Index Scan using idx_oauth_applications_organization_id on public.oauth_applications (cost=0.43..1.95 rows=1 width=4) (actual time=0.011..0.012 rows=0 loops=1)
Index Cond: (oauth_applications.organization_id IS NULL)
Filter: (oauth_applications.id >= 1000)
Rows Removed by Filter: 0
Buffers: shared hit=3
Settings: work_mem = '230MB', seq_page_cost = '4', effective_cache_size = '472585MB', jit = 'off', random_page_cost = '1.5'**https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/53189/commands/155139
ModifyTable on public.oauth_applications (cost=0.43..1.95 rows=0 width=0) (actual time=0.007..0.007 rows=0 loops=1)
Buffers: shared hit=3
-> Index Scan using idx_oauth_applications_organization_id on public.oauth_applications (cost=0.43..1.95 rows=1 width=14) (actual time=0.006..0.006 rows=0 loops=1)
Index Cond: (oauth_applications.organization_id IS NULL)
Filter: ((oauth_applications.id >= 1000) AND (oauth_applications.id < 2000))
Rows Removed by Filter: 0
Buffers: shared hit=3
Settings: effective_cache_size = '472585MB', jit = 'off', random_page_cost = '1.5', work_mem = '230MB', seq_page_cost = '4'https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/53189/commands/155140
Constraint (20260625131309)
ALTER TABLE oauth_applications ADD CONSTRAINT check_77eda6baaa CHECK (organization_id IS NOT NULL) NOT VALID;
ALTER TABLE oauth_applications VALIDATE CONSTRAINT check_77eda6baaa;MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
- This MR is backporting a bug fix, documentation update, or spec fix, previously merged in the default branch.
- The MR that fixed the bug on the default branch has been deployed to GitLab.com (not applicable for documentation or spec changes).
- The MR title is descriptive (e.g. "Backport of 'title of default branch MR'"). This is important, since the title will be copied to the patch blog post.
- Required labels have been applied to this merge request
- severity label and bug subtype labels (if applicable)
- If this MR fixes a bug that affects customers, the customer label has been applied.
- This MR has been approved by a maintainer (only one approval is required).
- Ensure the
e2e:test-on-omnibus-eejob has succeeded, or if it has failed, investigate the failures. If you determine the failures are unrelated, you may proceed. If you need assistance investigating, request help in the #s_developer_experience Slack channel to confirm the failures are unrelated to the merge request.
Note to the merge request author and maintainer
If you have questions about the patch release process, please:
- Refer to the patch release runbook for engineers and maintainers for guidance.
- Ask questions on the
#releasesSlack channel (internal only). - Once the backport has been merged, the commit changes will be automatically deployed to a release environment that can be used for manual validation. See after merging runbook for details.