Extract organization CRUD out of the crud/__init__.py monolith - #2592
Merged
Conversation
This was referenced Aug 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Continues the incremental split of the
crud/__init__.pymonolith (#2410 through #2458, most recently #2588 for Test Result) by moving the Organization CRUD functions into their own per-entity module, per the "split, don't grow" rule inapps/backend/AGENTS.md.What Changed
New
app/crud/organization.pyholdsget_organization,get_organizations,create_organization,update_organizationanddelete_organization, moved verbatim out of the# Organization CRUDsection ofcrud/__init__.py, plus its ownlogger.The
get_session_variablesdebug helper moved with them. It sat at the top of the monolith butcreate_organizationwas its only caller — it reads backapp.current_organization/app.current_userbefore and after the session-context reset so the log shows the GUCs really were cleared — so it belongs in this module.create_organization's behaviour is untouched: it still callsreset_session_context(db)and skips RLS (an organization is the tenant, so there is nothing to scope it to yet), still expires the session, still returns the flushed object without a refresh, and still honoursowner_user_id=Nonefor internal callers that already supply the correct IDs in the schema. The module docstring now explains why.Callers moved to the house pattern (
from rhesis.backend.app.crud import organization as organization_crud):routers/organization.py,tests/backend/fixtures/test_setup.pyandtests/backend/crud/test_transaction_management.py. In all three the barecrudname was used only for organization functions, socrudwas dropped from theirfrom rhesis.backend.app import crud, ...lines. A stalecrud.get_organizationdoc reference inservices/platform_key.pywas updated tocrud.organization.get_organization.Note for the sibling extraction PRs open against the same file: this is the PR that also drops the now-unused imports from the top of
crud/__init__.py—textfrom thesqlalchemyimport on line 10 (onlyget_session_variablesused it), the wholefrom rhesis.backend.app.database import reset_session_contextline (onlycreate_organizationused it), andfrom uuid import UUID(onlycreate_organization's signature used it).and_andselectstay; they are still used by the TestSet and Test sections. No other line in the import block was touched, reordered or reformatted.crud/__init__.pydrops 111 lines, from 1336 to 1225.Additional Context
Pure refactor. Function bodies were moved byte-for-byte (verified by diffing the extracted blocks against the originals) — no signature, query or behaviour change, so the generated SQL is identical. The only pre-existing lint findings left in place are
typing.Unionunused incrud/__init__.pyand the import-order plus unusedfastapi.APIRouterfindings inrouters/organization.py, all of which are already present onmain.Testing
uv run pytest ../../tests/backend/security/ ../../tests/backend/crud/ ../../tests/backend/routes/test_organization.py ../../tests/backend/services/test_organization.py ../../tests/backend/auth/ -qfromapps/backend— 1931 passed, 6 skipped (all pre-existing skips). Ruff check and format clean on every touched file apart from the pre-existing findings noted above.