Draft: [PoC] Rewrite queries: Filter on Current.organization
What does this MR do and why?
This MR modifies SELECT queries: if Current.organization is set, it will dynamically add WHERE organization_id = <org_id> based on sharding keys.
This is unreviewed and not tested.
Demo
First, Current.organization is not set, so no WHERE-clauses are applied. All 8 Groups are returned.
➜ gitlab git:(poc-query-parsing) gdk rails c
Loading development environment (Rails 7.1.5.1)
[2] pry(main)> Group.count
=> 8
[3] pry(main)> Group.all.map(&:path)
=> ["toolbox", "gitlab-org", "gnuwget", "Commit451", "jashkenas", "flightjs", "twitter", "group2"]
The newest group, group2, is part of My Org Organization. We set Current.organization to this My Org and then, SQL WHERE clause on organization_id is applied. Only 1 Group is now returned:
[4] pry(main)> Current.organization = Organizations::Organization.second
=> #<Organizations::Organization:0x000000031f71acd8>
[5] pry(main)> Group.count
=> 1
[6] pry(main)> Group.all.map(&:path)
=> ["group2"]
Implementation
- The fast
pg_queryQuery Parser is used to decompose the SQL, add the where-clauses if needed, and return the modified SQL statement - It works for both ActiveRecord as more low-level SQL
- More complex SQL is not yet supported (CTE's)
- The logic is patching
Pg::Connectionclass - The query rewriting happens AFTER the logs are written: Rails log will show the unmodified query
References
Screenshots or screen recordings
| Before | After |
|---|---|
How to set up and validate locally
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.
Edited by Rutger Wessels