feat: all unique constraints tested
This commit is contained in:
parent
52990aae13
commit
e9b272811f
7 changed files with 28 additions and 11 deletions
|
|
@ -18,7 +18,6 @@ Endpoints:
|
|||
|
||||
from fastapi import APIRouter, status
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from psycopg import errors
|
||||
|
||||
from src.service.exceptions import ServiceNotFoundException
|
||||
from src.exceptions import ConflictException
|
||||
|
|
@ -264,12 +263,15 @@ async def create_new_permission(
|
|||
if service_model is None:
|
||||
raise ServiceNotFoundException(service_id=request_model.service_id)
|
||||
perm_model = Perm(**request_model.__dict__)
|
||||
db.add(perm_model)
|
||||
try:
|
||||
db.add(perm_model)
|
||||
db.flush()
|
||||
except IntegrityError as e:
|
||||
if isinstance(e.orig, errors.UniqueViolation):
|
||||
if (
|
||||
getattr(e.orig, "pgcode", None) == "23505" # Postgres unique violation
|
||||
or "UNIQUE constraint failed" in str(e.orig) # SQLite unique violation
|
||||
):
|
||||
raise ConflictException(message="Permission already exists")
|
||||
db.flush()
|
||||
response = {
|
||||
"service_name": perm_model.service_name,
|
||||
"resource": perm_model.resource,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue