feat: prevent duplicate rest day types per date (Migration 012)
Problem: User can create multiple rest days of same type per date (e.g., 2x Mental Rest on 2026-03-23) - makes no sense. Solution: UNIQUE constraint on (profile_id, date, focus) ## Migration 012: - Add focus column (extracted from rest_config JSONB) - Populate from existing data - Add NOT NULL constraint - Add CHECK constraint (valid focus values) - Add UNIQUE constraint (profile_id, date, focus) - Add index for performance ## Backend: - Insert focus column alongside rest_config - Handle UniqueViolation gracefully - User-friendly error: "Du hast bereits einen Ruhetag 'Muskelregeneration' für 23.03." ## Benefits: - DB-level enforcement (clean) - Fast queries (no JSONB scan) - Clear error messages - Prevents: 2x muscle_recovery same day - Allows: muscle_recovery + mental_rest same day ✓ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
f2e2aff17f
commit
f87b93ce2f
2 files changed
+67
-13
No files matched your search
@@ -10,6 +10,7 @@ from datetime import datetime, timedelta
|
||||
from fastapi import APIRouter, HTTPException, Depends, Header
|
||||
from pydantic import BaseModel, Field
|
||||
from psycopg2.extras import Json
|
||||
from psycopg2.errors import UniqueViolation
|
||||
|
||||
from db import get_db, get_cursor, r2d
|
||||
from auth import require_auth
|
||||
@@ -89,23 +90,39 @@ def create_rest_day(
|
||||
|
||||
# Convert RestConfig to dict for JSONB storage
|
||||
config_dict = data.rest_config.model_dump()
|
||||
focus = data.rest_config.focus
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
try:
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
# Insert (multiple entries per date allowed)
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO rest_days (profile_id, date, rest_config, note, created_at)
|
||||
VALUES (%s, %s, %s, %s, CURRENT_TIMESTAMP)
|
||||
RETURNING id, profile_id, date, rest_config, note, created_at
|
||||
""",
|
||||
(pid, data.date, Json(config_dict), data.note)
|
||||
# Insert (multiple entries per date allowed, but not same focus)
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO rest_days (profile_id, date, focus, rest_config, note, created_at)
|
||||
VALUES (%s, %s, %s, %s, %s, CURRENT_TIMESTAMP)
|
||||
RETURNING id, profile_id, date, focus, rest_config, note, created_at
|
||||
""",
|
||||
(pid, data.date, focus, Json(config_dict), data.note)
|
||||
)
|
||||
|
||||
result = cur.fetchone()
|
||||
return r2d(result)
|
||||
except UniqueViolation:
|
||||
# User-friendly error for duplicate focus
|
||||
focus_labels = {
|
||||
'muscle_recovery': 'Muskelregeneration',
|
||||
'cardio_recovery': 'Cardio-Erholung',
|
||||
'mental_rest': 'Mentale Erholung',
|
||||
'deload': 'Deload',
|
||||
'injury': 'Verletzungspause',
|
||||
}
|
||||
focus_label = focus_labels.get(focus, focus)
|
||||
raise HTTPException(
|
||||
400,
|
||||
f"Du hast bereits einen Ruhetag '{focus_label}' für {data.date}. Bitte wähle einen anderen Typ oder lösche den bestehenden Eintrag."
|
||||
)
|
||||
|
||||
result = cur.fetchone()
|
||||
return r2d(result)
|
||||
|
||||
|
||||
@router.get("/{rest_day_id}")
|
||||
def get_rest_day(
|
||||
@@ -159,6 +176,9 @@ def update_rest_day(
|
||||
if data.rest_config:
|
||||
updates.append("rest_config = %s")
|
||||
values.append(Json(data.rest_config.model_dump()))
|
||||
# Also update focus column if config changed
|
||||
updates.append("focus = %s")
|
||||
values.append(data.rest_config.focus)
|
||||
|
||||
if data.note is not None:
|
||||
updates.append("note = %s")
|
||||
|
||||
Reference in new issue
Block a user