Add method for finding sharding organization
What does this MR do and why?
grouporganizations is working on organization data isolation. As part of that, we need to find out to what organization a record in the database belongs.
For that purpose, the sharding_organization method is introduced. It is available on all ActiveRecord models. This logic is using data from the sharding key definitions in db/docs yaml files.
Query plans
::Organizations::Organization.joins(:projects).where(projects: {id: parent_record_id}).first
Click to expand
SELECT "organizations".*
FROM "organizations"
INNER JOIN "projects"
ON "projects"."organization_id" = "organizations"."id"
WHERE "projects"."id" = 278964
ORDER BY "organizations"."id" ASC
LIMIT 1
https://postgres.ai/console/gitlab/gitlab-production-main/sessions/46244/commands/141186
Time: 3.213 ms
- planning: 3.114 ms
- execution: 0.099 ms
- I/O read: 0.000 ms
- I/O write: 0.000 ms
Shared buffers:
- hits: 7 (~56.00 KiB) from the buffer pool
- reads: 0 from the OS file cache, including disk I/O
- dirtied: 0
- writes: 0
::Organizations::Organization.joins(:namespaces).where(namespaces: {id: parent_record_id}).first
Click to expand
SELECT "organizations".*
FROM "organizations"
INNER JOIN "namespaces"
ON "namespaces"."organization_id" = "organizations"."id"
WHERE "namespaces"."id" = 9970
ORDER BY "organizations"."id" ASC
LIMIT 1
https://postgres.ai/console/gitlab/gitlab-production-main/sessions/46244/commands/141184
Time: 2.728 ms
- planning: 2.592 ms
- execution: 0.136 ms
- I/O read: 0.000 ms
- I/O write: 0.000 ms
Shared buffers:
- hits: 7 (~56.00 KiB) from the buffer pool
- reads: 0 from the OS file cache, including disk I/O
- dirtied: 0
- writes: 0
::Organizations::Organization.joins('INNER JOIN users ON users.organization_id = organizations.id').where(users: {id: parent_record_id}).first
Click to expand
SELECT "organizations".*
FROM "organizations"
INNER JOIN users
ON users.organization_id = organizations.id
WHERE "users"."id" = 1614863
ORDER BY "organizations"."id" ASC
LIMIT 1
https://postgres.ai/console/gitlab/gitlab-production-main/sessions/45386/commands/139121
Time: 2.282 ms
- planning: 2.154 ms
- execution: 0.128 ms
- I/O read: 0.000 ms
- I/O write: 0.000 ms
Shared buffers:
- hits: 6 (~48.00 KiB) from the buffer pool
- reads: 0 from the OS file cache, including disk I/O
- dirtied: 0
- writes: 0
::Organizations::Organization.find_by_id(parent_record_id)
Click to expand
SELECT "organizations".*
FROM "organizations"
WHERE "organizations"."id" = 1
LIMIT 1
https://postgres.ai/console/gitlab/gitlab-production-main/sessions/46244/commands/141179
Time: 1.834 ms
- planning: 0.616 ms
- execution: 1.218 ms
- I/O read: 1.075 ms
- I/O write: 0.000 ms
Shared buffers:
- hits: 3 (~24.00 KiB) from the buffer pool
- reads: 2 (~16.00 KiB) from the OS file cache, including disk I/O
- dirtied: 0
- writes: 0
How to set up and validate locally
# With sharding key returns organization and group:
MergeRequest.first.sharding_organization
Ci::Pipeline.first.sharding_organization
# No sharding key: code returns nil
ApplicationSetting.first.sharding_organization
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 #577780