Skip to content

Commit

Permalink
Merge pull request #417 from uhh-lt/remove-current-code
Browse files Browse the repository at this point in the history
Remove current code
  • Loading branch information
bigabig authored Sep 26, 2024
2 parents 7dfde14 + c54ceb2 commit c7e67cc
Show file tree
Hide file tree
Showing 47 changed files with 296 additions and 588 deletions.
121 changes: 121 additions & 0 deletions backend/src/alembic/versions/85d69d90d3e4_remove_current_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"""remove current code
Revision ID: 85d69d90d3e4
Revises: 714fa3c0323d
Create Date: 2024-09-26 14:59:52.379282
"""

from typing import Sequence, Union

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "85d69d90d3e4"
down_revision: Union[str, None] = "714fa3c0323d"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# add code id columns to bbox and span annotation
op.add_column("bboxannotation", sa.Column("code_id", sa.Integer(), nullable=True))
op.create_foreign_key(
None, "bboxannotation", "code", ["code_id"], ["id"], ondelete="CASCADE"
)
op.create_index(
op.f("ix_bboxannotation_code_id"), "bboxannotation", ["code_id"], unique=False
)
op.add_column("spanannotation", sa.Column("code_id", sa.Integer(), nullable=True))
op.create_index(
op.f("ix_spanannotation_code_id"), "spanannotation", ["code_id"], unique=False
)
op.create_foreign_key(
None, "spanannotation", "code", ["code_id"], ["id"], ondelete="CASCADE"
)

# now, populate the code_id columns
# basically, to get the code_id for span and bbox annotations, we need to get the code_id from the current code
op.execute(
"""
UPDATE spanannotation
SET code_id = currentcode.code_id
FROM currentcode
WHERE spanannotation.current_code_id = currentcode.id
"""
)

op.execute(
"""
UPDATE bboxannotation
SET code_id = currentcode.code_id
FROM currentcode
WHERE bboxannotation.current_code_id = currentcode.id
"""
)

# now, make the columns not nullable
op.alter_column("bboxannotation", "code_id", nullable=False)
op.alter_column("spanannotation", "code_id", nullable=False)

# remove everything related to current code
op.drop_index("ix_currentcode_code_id", table_name="currentcode")
op.drop_index("ix_currentcode_id", table_name="currentcode")
op.drop_index("ix_bboxannotation_current_code_id", table_name="bboxannotation")
op.drop_constraint(
"bboxannotation_current_code_id_fkey", "bboxannotation", type_="foreignkey"
)
op.drop_column("bboxannotation", "current_code_id")
op.drop_index("ix_objecthandle_current_code_id", table_name="objecthandle")
op.drop_constraint(
"UC_only_one_object_handle_per_instance", "objecthandle", type_="unique"
)
op.create_unique_constraint(
"UC_only_one_object_handle_per_instance",
"objecthandle",
[
"user_id",
"project_id",
"code_id",
"source_document_id",
"span_annotation_id",
"span_group_id",
"document_tag_id",
"action_id",
"memo_id",
],
)
op.drop_index("idx_for_uc_work_with_null", table_name="objecthandle")
op.create_index(
"idx_for_uc_work_with_null",
"objecthandle",
[
sa.text("coalesce(user_id, 0)"),
sa.text("coalesce(project_id, 0)"),
sa.text("coalesce(code_id, 0)"),
sa.text("coalesce(source_document_id, 0)"),
sa.text("coalesce(span_annotation_id, 0)"),
sa.text("coalesce(bbox_annotation_id, 0)"),
sa.text("coalesce(span_group_id, 0)"),
sa.text("coalesce(document_tag_id, 0)"),
sa.text("coalesce(action_id, 0)"),
sa.text("coalesce(memo_id, 0)"),
],
unique=True,
)
op.drop_constraint(
"objecthandle_current_code_id_fkey", "objecthandle", type_="foreignkey"
)
op.drop_index("ix_spanannotation_current_code_id", table_name="spanannotation")
op.drop_constraint(
"spanannotation_current_code_id_fkey", "spanannotation", type_="foreignkey"
)
op.drop_column("objecthandle", "current_code_id")
op.drop_column("spanannotation", "current_code_id")
op.drop_table("currentcode")


def downgrade() -> None:
raise RuntimeError("Downgrade not supported")
2 changes: 1 addition & 1 deletion backend/src/api/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def skip_limit_params(
async def resolve_code_param(
resolve: bool = Query(
title="Resolve Code",
description="If true, the current_code_id of the"
description="If true, the code_id of the"
" SpanAnnotation gets resolved and replaced"
" by the respective Code entity",
default=True,
Expand Down
15 changes: 7 additions & 8 deletions backend/src/api/endpoints/bbox_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
from app.core.data.crud.bbox_annotation import crud_bbox_anno
from app.core.data.crud.memo import crud_memo
from app.core.data.dto.bbox_annotation import (
BBoxAnnotationCreateWithCodeId,
BBoxAnnotationCreate,
BBoxAnnotationRead,
BBoxAnnotationReadResolved,
BBoxAnnotationUpdateWithCodeId,
BBoxAnnotationUpdate,
)
from app.core.data.dto.code import CodeRead
from app.core.data.dto.memo import AttachedObjectType, MemoCreate, MemoInDB, MemoRead
Expand All @@ -36,15 +36,15 @@
def add_bbox_annotation(
*,
db: Session = Depends(get_db_session),
bbox: BBoxAnnotationCreateWithCodeId,
bbox: BBoxAnnotationCreate,
resolve_code: bool = Depends(resolve_code_param),
authz_user: AuthzUser = Depends(),
) -> Union[BBoxAnnotationRead, BBoxAnnotationReadResolved]:
authz_user.assert_is_same_user(bbox.user_id)
authz_user.assert_in_same_project_as(Crud.SOURCE_DOCUMENT, bbox.sdoc_id)
authz_user.assert_in_same_project_as(Crud.CODE, bbox.code_id)

db_obj = crud_bbox_anno.create_with_code_id(db=db, create_dto=bbox)
db_obj = crud_bbox_anno.create(db=db, create_dto=bbox)
if resolve_code:
return BBoxAnnotationReadResolved.model_validate(db_obj)
else:
Expand Down Expand Up @@ -81,14 +81,14 @@ def update_by_id(
*,
db: Session = Depends(get_db_session),
bbox_id: int,
bbox_anno: BBoxAnnotationUpdateWithCodeId,
bbox_anno: BBoxAnnotationUpdate,
resolve_code: bool = Depends(resolve_code_param),
authz_user: AuthzUser = Depends(),
) -> Union[BBoxAnnotationRead, BBoxAnnotationReadResolved]:
authz_user.assert_in_same_project_as(Crud.BBOX_ANNOTATION, bbox_id)
authz_user.assert_in_same_project_as(Crud.CODE, bbox_anno.code_id)

db_obj = crud_bbox_anno.update_with_code_id(db=db, id=bbox_id, update_dto=bbox_anno)
db_obj = crud_bbox_anno.update(db=db, id=bbox_id, update_dto=bbox_anno)
if resolve_code:
return BBoxAnnotationReadResolved.model_validate(db_obj)
else:
Expand Down Expand Up @@ -126,8 +126,7 @@ def get_code(
authz_user.assert_in_same_project_as(Crud.BBOX_ANNOTATION, bbox_id)

bbox_db_obj = crud_bbox_anno.read(db=db, id=bbox_id)

return CodeRead.model_validate(bbox_db_obj.current_code.code)
return CodeRead.model_validate(bbox_db_obj.code)


@router.put(
Expand Down
18 changes: 0 additions & 18 deletions backend/src/api/endpoints/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from app.core.authorization.authz_user import AuthzUser
from app.core.data.crud import Crud
from app.core.data.crud.code import crud_code
from app.core.data.crud.current_code import crud_current_code
from app.core.data.crud.memo import crud_memo
from app.core.data.dto.code import CodeCreate, CodeRead, CodeUpdate
from app.core.data.dto.memo import AttachedObjectType, MemoCreate, MemoInDB, MemoRead
Expand Down Expand Up @@ -39,23 +38,6 @@ def create_new_code(
return CodeRead.model_validate(db_code)


@router.get(
"/current/{current_code_id}",
response_model=CodeRead,
summary="Returns the Code linked by the CurrentCode with the given ID.",
)
def get_code_by_current_code_id(
*,
db: Session = Depends(get_db_session),
current_code_id: int,
authz_user: AuthzUser = Depends(),
) -> CodeRead:
authz_user.assert_in_same_project_as(Crud.CURRENT_CODE, current_code_id)

cc_db_obj = crud_current_code.read(db=db, id=current_code_id)
return CodeRead.model_validate(cc_db_obj.code)


@router.get(
"/{code_id}",
response_model=CodeRead,
Expand Down
17 changes: 8 additions & 9 deletions backend/src/api/endpoints/span_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
from app.core.data.dto.code import CodeRead
from app.core.data.dto.memo import AttachedObjectType, MemoCreate, MemoInDB, MemoRead
from app.core.data.dto.span_annotation import (
SpanAnnotationCreateBulkWithCodeId,
SpanAnnotationCreateWithCodeId,
SpanAnnotationCreate,
SpanAnnotationRead,
SpanAnnotationReadResolved,
SpanAnnotationUpdateWithCodeId,
SpanAnnotationUpdate,
)
from app.core.data.dto.span_group import SpanGroupRead

Expand All @@ -34,7 +33,7 @@
def add_span_annotation(
*,
db: Session = Depends(get_db_session),
span: SpanAnnotationCreateWithCodeId,
span: SpanAnnotationCreate,
resolve_code: bool = Depends(resolve_code_param),
authz_user: AuthzUser = Depends(),
validate: Validate = Depends(),
Expand All @@ -49,7 +48,7 @@ def add_span_annotation(
]
)

db_obj = crud_span_anno.create_with_code_id(db=db, create_dto=span)
db_obj = crud_span_anno.create(db=db, create_dto=span)
if resolve_code:
return SpanAnnotationReadResolved.model_validate(db_obj)
else:
Expand All @@ -64,7 +63,7 @@ def add_span_annotation(
def add_span_annotations_bulk(
*,
db: Session = Depends(get_db_session),
spans: List[SpanAnnotationCreateBulkWithCodeId],
spans: List[SpanAnnotationCreate],
resolve_code: bool = Depends(resolve_code_param),
authz_user: AuthzUser = Depends(),
validate: Validate = Depends(),
Expand Down Expand Up @@ -116,7 +115,7 @@ def update_by_id(
*,
db: Session = Depends(get_db_session),
span_id: int,
span_anno: SpanAnnotationUpdateWithCodeId,
span_anno: SpanAnnotationUpdate,
resolve_code: bool = Depends(resolve_code_param),
authz_user: AuthzUser = Depends(),
validate: Validate = Depends(),
Expand All @@ -127,7 +126,7 @@ def update_by_id(
[(Crud.SPAN_ANNOTATION, span_id), (Crud.CODE, span_anno.code_id)]
)

db_obj = crud_span_anno.update_with_code_id(db=db, id=span_id, update_dto=span_anno)
db_obj = crud_span_anno.update(db=db, id=span_id, update_dto=span_anno)
if resolve_code:
return SpanAnnotationReadResolved.model_validate(db_obj)
else:
Expand Down Expand Up @@ -165,7 +164,7 @@ def get_code(
authz_user.assert_in_same_project_as(Crud.SPAN_ANNOTATION, span_id)

span_db_obj = crud_span_anno.read(db=db, id=span_id)
return CodeRead.model_validate(span_db_obj.current_code.code)
return CodeRead.model_validate(span_db_obj.code)


@router.get(
Expand Down
Loading

0 comments on commit c7e67cc

Please sign in to comment.