fixed all and operations in sqlalchemy statements

This commit is contained in:
Linus Vogel 2026-03-08 11:54:08 +01:00
parent faf831a7e2
commit ecf3bfdda4
4 changed files with 11 additions and 11 deletions

View File

@ -3,7 +3,7 @@ from tkinter.font import names
from fastapi import HTTPException from fastapi import HTTPException
from sqlalchemy.orm import Session 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 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: if parent is not None:
parent_labels = parent.split('/') parent_labels = parent.split('/')
for label in parent_labels: 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() results = db.execute(parent_stmt).fetchall()
if len(results) != 1: if len(results) != 1:
raise HTTPException(status_code=400, detail="Parent not found") raise HTTPException(status_code=400, detail="Parent not found")

View File

@ -1,6 +1,6 @@
import uuid import uuid
from sqlalchemy import select, insert, bindparam from sqlalchemy import select, insert, bindparam, and_
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
from starlette.requests import Request 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 # Traverse the parent hierarchy to ensure all components exist
parent_id = None 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: for label in parent_labels:
result = db.execute(stmt_select_respecting_parent, { result = db.execute(stmt_select_respecting_parent, {
'label': label, 'label': label,

View File

@ -1,7 +1,7 @@
import uuid import uuid
from sqlalchemy import select, insert, bindparam, delete from sqlalchemy import select, insert, bindparam, delete, and_
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
from starlette.requests import Request 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 [] path = split_and_validate_path(params.path) if params.path else []
# get the path from the db # 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: for label in path:
result = db.execute(path_stmt, {'name': label, 'parent_id': last}).fetchall() 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 last = tmp.id
# get the host in question # 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() result = db.execute(stmt).fetchall()
if len(result) == 0: 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 = split_and_validate_path(path) if path is not None else []
labels += [ name ] 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 last = None
for label in labels: for label in labels:
result = db.execute(stmt, {'name': label, 'last': last}).fetchall() 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) labels.append(name)
last = None 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: for label in labels:
result = db.execute(stmt_step, {'name': label, 'last': last}).fetchall() result = db.execute(stmt_step, {'name': label, 'last': last}).fetchall()

View File

@ -4,7 +4,7 @@ from uuid import uuid4
from sqlalchemy.dialects import postgresql from sqlalchemy.dialects import postgresql
from sqlalchemy.dialects.postgresql import insert 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 sqlalchemy.orm import Session
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
from starlette.requests import Request from starlette.requests import Request
@ -38,7 +38,7 @@ def pillar_get(req: Request, fqdn: str):
db: Session = req.state.db db: Session = req.state.db
# get the host hierarchy # 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() result = db.execute(host_stmt).fetchall()
if len(result) == 0: if len(result) == 0:
return JSONResponse(status=404, content={}) return JSONResponse(status=404, content={})