Skip to content

Use one SQL query to get all domains#4878

Merged
quis merged 1 commit into
mainfrom
use-sql-to-query-domains
Jun 12, 2026
Merged

Use one SQL query to get all domains#4878
quis merged 1 commit into
mainfrom
use-sql-to-query-domains

Conversation

@quis

@quis quis commented Jun 11, 2026

Copy link
Copy Markdown
Member

Domains must have an organisation ID, so are always associated with an organisation:

organisation_id = db.Column("organisation_id", UUID(as_uuid=True), db.ForeignKey("organisation.id"), nullable=False)

The previous query gets all the domains for all organisations, no matter whether they are archived, have no services, etc.

This does the same thing but in one SQL query, rather than firing off one query per organisation.

Tested here:

def test_dao_get_organisation_domains(notify_db_session):
create_organisation("org_1", domains=["dept_of_health.com", "eg.com"])
create_organisation("org_2", domains=["other.com", "number10.com"])
create_organisation("org_3", domains=["my_org.gov", "whitechapel.com"])
assert set(dao_get_organisation_domains()) == {
"dept_of_health.com",
"eg.com",
"other.com",
"number10.com",
"my_org.gov",
"whitechapel.com",
}

Domains must have an organisation ID, so are always associated with an
organisation:
https://github.com/alphagov/notifications-api/blob/aee7023565123843ea614acd27fe473ed1bcb26a/app/models.py#L380

The previous query gets all the domains for all organisations, no matter
whether they are archived, have no services, etc.

This does the same thing but in one SQL query, rather than firing off
one query per organisation.
@quis quis merged commit d45656f into main Jun 12, 2026
10 checks passed
@quis quis deleted the use-sql-to-query-domains branch June 12, 2026 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants