Spaces:
Runtime error
Runtime error
File size: 17,590 Bytes
9eafd9f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
# Data Model: Task CRUD Operations
**Feature**: Task CRUD Operations
**Date**: 2026-01-08
**Status**: Complete
## Overview
This document defines the database schema, entity relationships, and data validation rules for the Task CRUD feature. The data model supports multi-user task management with user data isolation.
## Entity Relationship Diagram
```
┌─────────────────┐ ┌─────────────────┐
│ User │ │ Task │
├─────────────────┤ ├─────────────────┤
│ id (PK) │◄────────│ id (PK) │
│ email │ 1:N │ user_id (FK) │
│ name │ │ title │
│ created_at │ │ description │
│ updated_at │ │ completed │
└─────────────────┘ │ created_at │
│ updated_at │
└─────────────────┘
Relationship: One User has many Tasks
One Task belongs to one User
```
## Entities
### Task Entity
**Purpose**: Represents a to-do item belonging to a specific user.
**Table Name**: `tasks`
**Columns**:
| Column Name | Type | Constraints | Description |
|--------------|--------------|--------------------------------|------------------------------------------------|
| id | Integer | PRIMARY KEY, AUTO_INCREMENT | Unique task identifier |
| user_id | Integer | FOREIGN KEY (users.id), NOT NULL, INDEX | Owner of the task |
| title | String(200) | NOT NULL, LENGTH(1-200) | Task title (required) |
| description | String(1000) | NULLABLE, LENGTH(0-1000) | Optional task description |
| completed | Boolean | NOT NULL, DEFAULT FALSE, INDEX | Completion status |
| created_at | DateTime | NOT NULL, DEFAULT NOW() | Timestamp when task was created |
| updated_at | DateTime | NOT NULL, DEFAULT NOW(), ON UPDATE NOW() | Timestamp of last update |
**Indexes**:
- PRIMARY KEY on `id`
- INDEX on `user_id` (for filtering tasks by user)
- INDEX on `completed` (for filtering active/completed tasks)
- COMPOSITE INDEX on `(user_id, completed)` (for combined filtering)
- INDEX on `created_at` (for sorting by date)
**Constraints**:
- `user_id` FOREIGN KEY references `users(id)` ON DELETE CASCADE
- `title` must be between 1 and 200 characters
- `description` must be between 0 and 1000 characters (NULL allowed)
- `completed` must be boolean (true/false)
**SQLModel Definition**:
```python
from sqlmodel import SQLModel, Field, Relationship
from datetime import datetime
from typing import Optional
class Task(SQLModel, table=True):
"""Task entity representing a to-do item."""
__tablename__ = "tasks"
id: Optional[int] = Field(default=None, primary_key=True)
user_id: int = Field(foreign_key="users.id", nullable=False, index=True)
title: str = Field(max_length=200, nullable=False)
description: Optional[str] = Field(default=None, max_length=1000)
completed: bool = Field(default=False, nullable=False, index=True)
created_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)
updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)
# Relationship (will be fully implemented in Spec 2)
# user: Optional["User"] = Relationship(back_populates="tasks")
class Config:
json_schema_extra = {
"example": {
"id": 1,
"user_id": 42,
"title": "Buy groceries",
"description": "Milk, eggs, bread",
"completed": False,
"created_at": "2026-01-08T10:00:00Z",
"updated_at": "2026-01-08T10:00:00Z"
}
}
```
### User Entity (Stub)
**Purpose**: Represents an authenticated user (full implementation in Spec 2).
**Table Name**: `users`
**Columns** (minimal for Spec 1):
| Column Name | Type | Constraints | Description |
|--------------|--------------|--------------------------------|------------------------------------------------|
| id | Integer | PRIMARY KEY, AUTO_INCREMENT | Unique user identifier |
| email | String(255) | UNIQUE, NOT NULL | User email address |
| name | String(100) | NOT NULL | User display name |
| created_at | DateTime | NOT NULL, DEFAULT NOW() | Timestamp when user was created |
| updated_at | DateTime | NOT NULL, DEFAULT NOW() | Timestamp of last update |
**SQLModel Definition** (stub for Spec 1):
```python
from sqlmodel import SQLModel, Field
from datetime import datetime
from typing import Optional
class User(SQLModel, table=True):
"""User entity (stub for authentication spec)."""
__tablename__ = "users"
id: Optional[int] = Field(default=None, primary_key=True)
email: str = Field(max_length=255, unique=True, nullable=False)
name: str = Field(max_length=100, nullable=False)
created_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)
updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)
# Relationship (will be fully implemented in Spec 2)
# tasks: List["Task"] = Relationship(back_populates="user")
```
## Pydantic Schemas (Request/Response)
### TaskCreate (Request)
**Purpose**: Validate task creation requests.
```python
from pydantic import BaseModel, Field
from typing import Optional
class TaskCreate(BaseModel):
"""Schema for creating a new task."""
title: str = Field(
min_length=1,
max_length=200,
description="Task title (1-200 characters)"
)
description: Optional[str] = Field(
default=None,
max_length=1000,
description="Optional task description (0-1000 characters)"
)
class Config:
json_schema_extra = {
"example": {
"title": "Buy groceries",
"description": "Milk, eggs, bread"
}
}
```
### TaskUpdate (Request)
**Purpose**: Validate task update requests (full replacement).
```python
class TaskUpdate(BaseModel):
"""Schema for updating an existing task."""
title: str = Field(
min_length=1,
max_length=200,
description="Task title (1-200 characters)"
)
description: Optional[str] = Field(
default=None,
max_length=1000,
description="Optional task description (0-1000 characters)"
)
completed: bool = Field(
description="Task completion status"
)
class Config:
json_schema_extra = {
"example": {
"title": "Buy groceries and milk",
"description": "Updated description",
"completed": False
}
}
```
### TaskPatch (Request)
**Purpose**: Validate partial task updates (e.g., toggle completion).
```python
class TaskPatch(BaseModel):
"""Schema for partially updating a task."""
title: Optional[str] = Field(
default=None,
min_length=1,
max_length=200,
description="Task title (1-200 characters)"
)
description: Optional[str] = Field(
default=None,
max_length=1000,
description="Optional task description (0-1000 characters)"
)
completed: Optional[bool] = Field(
default=None,
description="Task completion status"
)
class Config:
json_schema_extra = {
"example": {
"completed": True
}
}
```
### TaskResponse (Response)
**Purpose**: Standardized task response format.
```python
from datetime import datetime
class TaskResponse(BaseModel):
"""Schema for task responses."""
id: int
user_id: int
title: str
description: Optional[str]
completed: bool
created_at: datetime
updated_at: datetime
class Config:
from_attributes = True # Enable ORM mode
json_schema_extra = {
"example": {
"id": 1,
"user_id": 42,
"title": "Buy groceries",
"description": "Milk, eggs, bread",
"completed": False,
"created_at": "2026-01-08T10:00:00Z",
"updated_at": "2026-01-08T10:00:00Z"
}
}
```
### TaskListResponse (Response)
**Purpose**: Response format for listing multiple tasks.
```python
from typing import List
class TaskListResponse(BaseModel):
"""Schema for task list responses."""
tasks: List[TaskResponse]
total: int
class Config:
json_schema_extra = {
"example": {
"tasks": [
{
"id": 1,
"user_id": 42,
"title": "Buy groceries",
"description": "Milk, eggs, bread",
"completed": False,
"created_at": "2026-01-08T10:00:00Z",
"updated_at": "2026-01-08T10:00:00Z"
}
],
"total": 1
}
}
```
## Data Validation Rules
### Title Validation
- **Required**: Yes
- **Min Length**: 1 character
- **Max Length**: 200 characters
- **Allowed Characters**: Any Unicode characters
- **Trimming**: Leading/trailing whitespace should be trimmed
- **Error Message**: "Title must be between 1 and 200 characters"
### Description Validation
- **Required**: No (optional)
- **Min Length**: 0 characters (empty string or NULL)
- **Max Length**: 1000 characters
- **Allowed Characters**: Any Unicode characters
- **Trimming**: Leading/trailing whitespace should be trimmed
- **Error Message**: "Description must be 1000 characters or less"
### Completed Validation
- **Required**: Yes (defaults to False on creation)
- **Type**: Boolean (true/false)
- **Error Message**: "Completed must be a boolean value"
### User ID Validation
- **Required**: Yes
- **Type**: Integer
- **Validation**: Must reference existing user in users table
- **Error Message**: "Invalid user ID"
## State Transitions
### Task Lifecycle
```
┌────────────┐
│ Created │ (completed = false)
│ (Initial) │
└──────┬──────┘
│
│ User marks complete
▼
┌─────────────┐
│ Completed │ (completed = true)
└──────┬──────┘
│
│ User marks incomplete
▼
┌─────────────┐
│ Active │ (completed = false)
└──────┬──────┘
│
│ User deletes
▼
┌─────────────┐
│ Deleted │ (removed from database)
└─────────────┘
```
**Valid Transitions**:
- Created → Completed (mark as done)
- Completed → Active (mark as not done)
- Any state → Deleted (remove task)
- Active → Updated (edit title/description)
- Completed → Updated (edit title/description)
## Database Migration
### Initial Migration (Alembic)
```python
"""Create tasks table
Revision ID: 001_create_tasks
Revises:
Create Date: 2026-01-08
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers
revision = '001_create_tasks'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# Create users table (stub for Spec 2)
op.create_table(
'users',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('email', sa.String(length=255), nullable=False),
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('email')
)
# Create tasks table
op.create_table(
'tasks',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('title', sa.String(length=200), nullable=False),
sa.Column('description', sa.String(length=1000), nullable=True),
sa.Column('completed', sa.Boolean(), nullable=False, server_default='false'),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
# Create indexes
op.create_index('ix_tasks_user_id', 'tasks', ['user_id'])
op.create_index('ix_tasks_completed', 'tasks', ['completed'])
op.create_index('ix_tasks_user_id_completed', 'tasks', ['user_id', 'completed'])
op.create_index('ix_tasks_created_at', 'tasks', ['created_at'])
def downgrade():
op.drop_index('ix_tasks_created_at', table_name='tasks')
op.drop_index('ix_tasks_user_id_completed', table_name='tasks')
op.drop_index('ix_tasks_completed', table_name='tasks')
op.drop_index('ix_tasks_user_id', table_name='tasks')
op.drop_table('tasks')
op.drop_table('users')
```
## Data Integrity Rules
### Foreign Key Constraints
- `tasks.user_id` MUST reference valid `users.id`
- ON DELETE CASCADE: Deleting a user deletes all their tasks
- Prevents orphaned tasks in database
### Uniqueness Constraints
- No uniqueness constraint on task titles (users can have duplicate titles)
- `users.email` must be unique (enforced in users table)
### NOT NULL Constraints
- `tasks.id`: Always required (auto-generated)
- `tasks.user_id`: Always required (task must belong to user)
- `tasks.title`: Always required (empty tasks not allowed)
- `tasks.completed`: Always required (defaults to false)
- `tasks.created_at`: Always required (auto-generated)
- `tasks.updated_at`: Always required (auto-updated)
### Check Constraints (Optional)
```sql
-- Ensure title is not empty after trimming
ALTER TABLE tasks ADD CONSTRAINT check_title_not_empty
CHECK (LENGTH(TRIM(title)) > 0);
-- Ensure description length if provided
ALTER TABLE tasks ADD CONSTRAINT check_description_length
CHECK (description IS NULL OR LENGTH(description) <= 1000);
```
## Query Patterns
### Common Queries
**Get all tasks for a user**:
```sql
SELECT * FROM tasks
WHERE user_id = ?
ORDER BY created_at DESC;
```
**Get active tasks for a user**:
```sql
SELECT * FROM tasks
WHERE user_id = ? AND completed = false
ORDER BY created_at DESC;
```
**Get completed tasks for a user**:
```sql
SELECT * FROM tasks
WHERE user_id = ? AND completed = true
ORDER BY created_at DESC;
```
**Get specific task with ownership check**:
```sql
SELECT * FROM tasks
WHERE id = ? AND user_id = ?;
```
**Update task with timestamp**:
```sql
UPDATE tasks
SET title = ?, description = ?, completed = ?, updated_at = NOW()
WHERE id = ? AND user_id = ?;
```
**Delete task with ownership check**:
```sql
DELETE FROM tasks
WHERE id = ? AND user_id = ?;
```
## Performance Considerations
### Index Usage
- `user_id` index: Used in all queries (data isolation)
- `completed` index: Used for filtering active/completed
- Composite `(user_id, completed)` index: Optimizes filtered queries
- `created_at` index: Used for sorting by date
### Query Optimization
- Always include `user_id` in WHERE clause (uses index)
- Limit result sets for large task lists (pagination)
- Use `SELECT *` sparingly in production (specify columns)
- Avoid N+1 queries (use joins if fetching related data)
### Connection Pooling
- Use Neon's built-in connection pooling
- Configure pool size based on expected concurrent users
- Reuse database sessions across requests
## Data Seeding (Development)
### Sample Data for Testing
```python
# Sample users
users = [
{"id": 1, "email": "alice@example.com", "name": "Alice"},
{"id": 2, "email": "bob@example.com", "name": "Bob"}
]
# Sample tasks
tasks = [
{
"user_id": 1,
"title": "Buy groceries",
"description": "Milk, eggs, bread",
"completed": False
},
{
"user_id": 1,
"title": "Finish project report",
"description": None,
"completed": True
},
{
"user_id": 2,
"title": "Call dentist",
"description": "Schedule appointment",
"completed": False
}
]
```
## Summary
The data model defines two entities: Task (full implementation) and User (stub for Spec 2). Tasks have a many-to-one relationship with Users, enforced via foreign key constraint. Validation rules ensure data integrity at both API and database layers. Indexes optimize query performance for filtering and sorting. The schema supports all functional requirements from the specification while maintaining user data isolation.
**Ready for**: API contract generation (contracts/).
|