From ecf3bfdda4eb2181163bd1ea2a40a05c842c6aae Mon Sep 17 00:00:00 2001 From: Linus Vogel Date: Sun, 8 Mar 2026 11:54:08 +0100 Subject: [PATCH] fixed all and operations in sqlalchemy statements --- pillar_tool/db/queries/host_queries.py | 4 ++-- pillar_tool/routers/host.py | 4 ++-- pillar_tool/routers/hostgroup.py | 10 +++++----- pillar_tool/routers/pillar.py | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pillar_tool/db/queries/host_queries.py b/pillar_tool/db/queries/host_queries.py index fa62024..e79c43a 100644 --- a/pillar_tool/db/queries/host_queries.py +++ b/pillar_tool/db/queries/host_queries.py @@ -3,7 +3,7 @@ from tkinter.font import names from fastapi import HTTPException from sqlalchemy.orm import Session -from sqlalchemy import select, insert, update +from sqlalchemy import select, insert, update, and_ from pillar_tool.db.models import Host @@ -18,7 +18,7 @@ def create_host(db: Session, fqdn: str, parent: str | None) -> Host: if parent is not None: parent_labels = parent.split('/') for label in parent_labels: - parent_stmt = select(Host).where(Host.name == label and Host.parent_id == parent_id) + parent_stmt = select(Host).where(and_(Host.name == label, Host.parent_id == parent_id)) results = db.execute(parent_stmt).fetchall() if len(results) != 1: raise HTTPException(status_code=400, detail="Parent not found") diff --git a/pillar_tool/routers/host.py b/pillar_tool/routers/host.py index 6217c18..5d0bcaa 100644 --- a/pillar_tool/routers/host.py +++ b/pillar_tool/routers/host.py @@ -1,6 +1,6 @@ import uuid -from sqlalchemy import select, insert, bindparam +from sqlalchemy import select, insert, bindparam, and_ from sqlalchemy.orm import Session from starlette.exceptions import HTTPException from starlette.requests import Request @@ -130,7 +130,7 @@ async def host_add(request: Request, fqdn: str, params: HostCreateParams): # Traverse the parent hierarchy to ensure all components exist parent_id = None - stmt_select_respecting_parent = select(Host).where(Host.name == bindparam("label") and Host.parent_id == bindparam("parent_id")) + stmt_select_respecting_parent = select(Host).where(and_(Host.name == bindparam("label"), Host.parent_id == bindparam("parent_id"))) for label in parent_labels: result = db.execute(stmt_select_respecting_parent, { 'label': label, diff --git a/pillar_tool/routers/hostgroup.py b/pillar_tool/routers/hostgroup.py index 988e77b..ec46eea 100644 --- a/pillar_tool/routers/hostgroup.py +++ b/pillar_tool/routers/hostgroup.py @@ -1,7 +1,7 @@ import uuid -from sqlalchemy import select, insert, bindparam, delete +from sqlalchemy import select, insert, bindparam, delete, and_ from sqlalchemy.orm import Session from starlette.exceptions import HTTPException from starlette.requests import Request @@ -69,7 +69,7 @@ def hostgroup_get(req: Request, name: str, params: HostgroupParams = Depends(get path = split_and_validate_path(params.path) if params.path else [] # get the path from the db - path_stmt = select(Host).where(Host.name == bindparam('name') and Host.parent_id == bindparam('parent_id')) + path_stmt = select(Host).where(and_(Host.name == bindparam('name') and Host.parent_id == bindparam('parent_id'))) for label in path: result = db.execute(path_stmt, {'name': label, 'parent_id': last}).fetchall() @@ -82,7 +82,7 @@ def hostgroup_get(req: Request, name: str, params: HostgroupParams = Depends(get last = tmp.id # get the host in question - stmt = select(Host).where(Host.name == name and Host.is_hostgroup == True and Host.parent_id == last) + stmt = select(Host).where(and_(Host.name == name, Host.is_hostgroup == True, Host.parent_id == last)) result = db.execute(stmt).fetchall() if len(result) == 0: @@ -120,7 +120,7 @@ def hostgroup_create(req: Request, name: str, params: HostgroupParams): labels = split_and_validate_path(path) if path is not None else [] labels += [ name ] - stmt = select(Host).where(Host.name == bindparam('name') and Host.is_hostgroup == True and Host.parent_id == bindparam('last')) + stmt = select(Host).where(and_(Host.name == bindparam('name'), Host.is_hostgroup == True, Host.parent_id == bindparam('last'))) last = None for label in labels: result = db.execute(stmt, {'name': label, 'last': last}).fetchall() @@ -164,7 +164,7 @@ def hostgroup_delete(req: Request, name: str, params: HostgroupParams = Depends( labels.append(name) last = None - stmt_step = select(Host).where(Host.name == bindparam('name') and Host.parent_id == bindparam('last') and Host.is_hostgroup == True) + stmt_step = select(Host).where(and_(Host.name == bindparam('name'), Host.parent_id == bindparam('last'), Host.is_hostgroup == True)) for label in labels: result = db.execute(stmt_step, {'name': label, 'last': last}).fetchall() diff --git a/pillar_tool/routers/pillar.py b/pillar_tool/routers/pillar.py index 64dd7b8..e582a70 100644 --- a/pillar_tool/routers/pillar.py +++ b/pillar_tool/routers/pillar.py @@ -4,7 +4,7 @@ from uuid import uuid4 from sqlalchemy.dialects import postgresql from sqlalchemy.dialects.postgresql import insert -from sqlalchemy import select, delete, bindparam, and_, or_ +from sqlalchemy import select, delete, bindparam, and_ from sqlalchemy.orm import Session from starlette.exceptions import HTTPException from starlette.requests import Request @@ -38,7 +38,7 @@ def pillar_get(req: Request, fqdn: str): db: Session = req.state.db # get the host hierarchy - host_stmt = select(Host).where(Host.name == fqdn and Host.is_hostgroup == False) + host_stmt = select(Host).where(and_(Host.name == fqdn, Host.is_hostgroup == False)) result = db.execute(host_stmt).fetchall() if len(result) == 0: return JSONResponse(status=404, content={})