Handle foreign key on stage items (#701)

fix https://github.com/evroon/bracket/issues/629
This commit is contained in:
Erik Vroon
2024-05-05 10:50:09 +02:00
committed by GitHub
parent 295809ef61
commit 0e4df8dcb5
3 changed files with 14 additions and 5 deletions

View File

@@ -32,6 +32,7 @@ from bracket.sql.stage_items import (
)
from bracket.sql.stages import get_full_tournament_details
from bracket.sql.validation import check_foreign_keys_belong_to_tournament
from bracket.utils.errors import ForeignKey, check_foreign_key_violation
from bracket.utils.id_types import StageItemId, TournamentId
router = APIRouter()
@@ -44,11 +45,16 @@ async def delete_stage_item(
tournament_id: TournamentId,
stage_item_id: StageItemId,
_: UserPublic = Depends(user_authenticated_for_tournament),
stage_item: StageItemWithRounds = Depends(stage_item_dependency),
__: StageItemWithRounds = Depends(stage_item_dependency),
) -> SuccessResponse:
await sql_delete_stage_item_with_foreign_keys(stage_item_id)
await recalculate_ranking_for_tournament_id(tournament_id)
return SuccessResponse()
with check_foreign_key_violation(
{
ForeignKey.matches_team1_winner_from_stage_item_id_fkey,
}
):
await sql_delete_stage_item_with_foreign_keys(stage_item_id)
await recalculate_ranking_for_tournament_id(tournament_id)
return SuccessResponse()
@router.post("/tournaments/{tournament_id}/stage_items", response_model=SuccessResponse)

View File

@@ -21,6 +21,7 @@ class ForeignKey(EnumAutoStr):
stage_item_inputs_team_id_fkey = auto()
matches_team1_id_fkey = auto()
matches_team2_id_fkey = auto()
matches_team1_winner_from_stage_item_id_fkey = auto()
unique_index_violation_error_lookup = {
@@ -35,6 +36,8 @@ foreign_key_violation_error_lookup = {
ForeignKey.stage_item_inputs_team_id_fkey: "This team is still used in stage items",
ForeignKey.matches_team1_id_fkey: "This team is still part of matches",
ForeignKey.matches_team2_id_fkey: "This team is still part of matches",
ForeignKey.matches_team1_winner_from_stage_item_id_fkey: "This stage item is referenced by "
"other stage items",
}

View File

@@ -110,7 +110,7 @@ export default function UserForm({ user, t, i18n }: { user: UserInterface; t: an
</Tabs.Panel>
<Tabs.Panel value="language" pt="xs">
<Select
clearable={false}
allowDeselect={false}
value={i18n.language}
label={t('language')}
data={locales}