---
task_id: task_4
story_id: story_0
sprint_id: sprint_4
status: not_started
type: backend
assignee: backend
created_date: 2025-11-05
completion_date: null
---

# Task 4: Create and Apply Database Migration

## What to do

Create an EF Core migration to add the new fields (AcceptanceCriteria, Tags, StoryPoints on Story; Order on Task) to the database.

## Files to create

- `colaflow-api/src/Modules/ProjectManagement/ColaFlow.Modules.ProjectManagement.Infrastructure/Migrations/YYYYMMDDHHMMSS_AddAdvancedStoryTaskFields.cs`

## Implementation

### Migration Creation

Run EF Core migration command:
```bash
cd colaflow-api/src/Modules/ProjectManagement/ColaFlow.Modules.ProjectManagement.Infrastructure
dotnet ef migrations add AddAdvancedStoryTaskFields --context ProjectManagementDbContext
```

### Expected Migration Content

**Up Migration**:
```csharp
migrationBuilder.AddColumn<string>(
    name: "AcceptanceCriteria",
    table: "Stories",
    type: "nvarchar(max)",
    nullable: true);

migrationBuilder.AddColumn<string>(
    name: "Tags",
    table: "Stories",
    type: "nvarchar(max)",
    nullable: true);

migrationBuilder.AddColumn<int>(
    name: "StoryPoints",
    table: "Stories",
    type: "int",
    nullable: true);

migrationBuilder.AddColumn<int>(
    name: "Order",
    table: "Tasks",
    type: "int",
    nullable: false,
    defaultValue: 0);

migrationBuilder.CreateIndex(
    name: "IX_Tasks_StoryId_Order",
    table: "Tasks",
    columns: new[] { "StoryId", "Order" });
```

**Down Migration**:
```csharp
migrationBuilder.DropIndex(
    name: "IX_Tasks_StoryId_Order",
    table: "Tasks");

migrationBuilder.DropColumn(name: "AcceptanceCriteria", table: "Stories");
migrationBuilder.DropColumn(name: "Tags", table: "Stories");
migrationBuilder.DropColumn(name: "StoryPoints", table: "Stories");
migrationBuilder.DropColumn(name: "Order", table: "Tasks");
```

### Data Backfill (Optional)

Add SQL to set Task Order based on CreatedAt:
```csharp
migrationBuilder.Sql(@"
    WITH OrderedTasks AS (
        SELECT Id, ROW_NUMBER() OVER (PARTITION BY StoryId ORDER BY CreatedAt) - 1 AS NewOrder
        FROM Tasks
    )
    UPDATE t
    SET t.[Order] = ot.NewOrder
    FROM Tasks t
    INNER JOIN OrderedTasks ot ON t.Id = ot.Id;
");
```

### Apply Migration

Run migration locally:
```bash
cd colaflow-api/src/ColaFlow.API
dotnet ef database update --context ProjectManagementDbContext
```

## Acceptance

- [ ] Migration file created
- [ ] Migration adds AcceptanceCriteria column (nvarchar(max), nullable)
- [ ] Migration adds Tags column (nvarchar(max), nullable)
- [ ] Migration adds StoryPoints column (int, nullable)
- [ ] Migration adds Order column (int, not null, default 0)
- [ ] Migration creates composite index (StoryId, Order)
- [ ] Migration applied successfully to local database
- [ ] Schema verified in database
- [ ] Rollback migration tested
- [ ] Migration script committed
