Files
ColaFlow/docs/plans/sprint_2_story_1_task_1.md
Yaojia Wang de6af53a77 feat(backend): Add AuditLog database schema and migration
Implement AuditLog entity and EF Core configuration for Sprint 2 Story 1 Task 1.

Changes:
- Created AuditLog entity with multi-tenant support
- Added EF Core configuration with JSONB columns for PostgreSQL
- Created composite indexes for query optimization
- Generated database migration (20251104220842_AddAuditLogTable)
- Updated PMDbContext with AuditLog DbSet and query filter
- Updated task status to in_progress in sprint plan

Technical Details:
- PostgreSQL JSONB type for OldValues/NewValues (flexible schema)
- Composite index on (TenantId, EntityType, EntityId) for entity history queries
- Timestamp index (DESC) for recent logs queries
- UserId index for user activity tracking
- Multi-tenant query filter applied to AuditLog

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-04 23:10:12 +01:00

2.1 KiB

task_id, story, status, estimated_hours, created_date, start_date, assignee
task_id story status estimated_hours created_date start_date assignee
sprint_2_story_1_task_1 sprint_2_story_1 in_progress 4 2025-11-05 2025-11-05 Backend Team

Task 1: Design AuditLog Database Schema and Create Migration

Story: Story 1 - Audit Log Foundation (Phase 1) Estimated: 4 hours

Description

Design and implement the AuditLog database table schema with proper columns, data types, and indexes to support efficient audit querying and multi-tenant isolation.

Acceptance Criteria

  • Database migration created with AuditLog table
  • All required columns defined with correct data types
  • Composite indexes created for query optimization
  • Multi-tenant isolation enforced (TenantId column)
  • Migration applied successfully

Implementation Details

Table Schema:

CREATE TABLE AuditLogs (
    Id UUID PRIMARY KEY,
    TenantId UUID NOT NULL,
    EntityType VARCHAR(100) NOT NULL,
    EntityId UUID NOT NULL,
    Action VARCHAR(20) NOT NULL, -- 'Create', 'Update', 'Delete'
    UserId UUID NULL,
    Timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    OldValues JSONB NULL,
    NewValues JSONB NULL,

    CONSTRAINT FK_AuditLogs_Tenants FOREIGN KEY (TenantId) REFERENCES Tenants(Id) ON DELETE CASCADE
);

-- Indexes for query performance
CREATE INDEX IX_AuditLogs_TenantId_EntityType_EntityId ON AuditLogs(TenantId, EntityType, EntityId);
CREATE INDEX IX_AuditLogs_Timestamp ON AuditLogs(Timestamp DESC);
CREATE INDEX IX_AuditLogs_UserId ON AuditLogs(UserId);

Files to Modify:

  • Create: colaflow-api/src/ColaFlow.Infrastructure/Data/Migrations/{timestamp}_AddAuditLogTable.cs

Technical Notes

  • Use PostgreSQL JSONB for OldValues/NewValues (flexible schema, indexed queries)
  • Composite index on (TenantId, EntityType, EntityId) for efficient entity history queries
  • Timestamp index with DESC order for recent logs queries
  • Consider table partitioning for future scalability (Phase 4)

Testing

  • Verify migration runs without errors
  • Check indexes are created
  • Verify foreign key constraints work

Created: 2025-11-05 by Backend Agent