Skip to content

πŸ”§ TASK-007.1.1 - Implement Database Schema (3 Subtasks)Β #21

@tim-gameplan

Description

@tim-gameplan

TASK-007.1.1 - Implement Database Schema

Parent Task: TASK-007.1 - Database Infrastructure
Total Duration: 3 days
Priority: Critical
Status: Ready to start
Dependencies: βœ… TASK-007.0.3 (Production Environment completed)

πŸ“‹ Overview

Implement comprehensive database schema with all required tables, indexes, and constraints for handheld integration. This task has been broken down into 3 focused subtasks for better implementation and testing.

πŸ”§ Subtask Breakdown

TASK-007.1.1.1: Core User & Authentication Schema πŸ“… Day 1

Duration: 1 day
Priority: Critical
Dependencies: TASK-007.0.3 (Production Environment completed)

Description:
Implement the foundational user management and authentication schema that supports cross-device authentication and user preferences.

Scope:

  • Users table with authentication fields
  • Device registration and management
  • User preferences and security settings
  • Session management tables
  • Authentication tokens and refresh tokens

Key Tables:

-- Users and Authentication
CREATE TABLE users (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    email VARCHAR(255) UNIQUE NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    display_name VARCHAR(100),
    preferences JSONB DEFAULT '{}',
    security_settings JSONB DEFAULT '{}',
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW(),
    last_login TIMESTAMP,
    status VARCHAR(20) DEFAULT 'active'
);

-- Device Management
CREATE TABLE devices (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    device_name VARCHAR(100) NOT NULL,
    device_type VARCHAR(50) NOT NULL,
    platform VARCHAR(50),
    capabilities JSONB DEFAULT '{}',
    last_seen TIMESTAMP DEFAULT NOW(),
    created_at TIMESTAMP DEFAULT NOW(),
    status VARCHAR(20) DEFAULT 'active'
);

-- Session Management
CREATE TABLE sessions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    device_id UUID REFERENCES devices(id) ON DELETE CASCADE,
    session_token VARCHAR(255) UNIQUE NOT NULL,
    refresh_token VARCHAR(255) UNIQUE,
    expires_at TIMESTAMP NOT NULL,
    created_at TIMESTAMP DEFAULT NOW(),
    last_activity TIMESTAMP DEFAULT NOW()
);

Acceptance Criteria:

  • User authentication schema implemented
  • Device registration tables created
  • Session management with proper expiration
  • Proper indexes for authentication queries
  • Foreign key constraints properly set

TASK-007.1.1.2: Conversation & Message Schema πŸ“… Day 2

Duration: 1 day
Priority: Critical
Dependencies: TASK-007.1.1.1

Description:
Implement the conversation and message storage schema that supports real-time synchronization and message history across devices.

Scope:

  • Conversations table with workspace integration
  • Messages table with JSONB content storage
  • Message threading and parent-child relationships
  • Message metadata and device tracking
  • Optimized indexes for message retrieval

Key Tables:

-- Conversations
CREATE TABLE conversations (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    title VARCHAR(200),
    workspace_path VARCHAR(500),
    metadata JSONB DEFAULT '{}',
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW(),
    last_activity TIMESTAMP DEFAULT NOW()
);

-- Messages
CREATE TABLE messages (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    conversation_id UUID REFERENCES conversations(id) ON DELETE CASCADE,
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    device_id UUID REFERENCES devices(id) ON DELETE SET NULL,
    message_type VARCHAR(50) NOT NULL,
    content JSONB NOT NULL,
    metadata JSONB DEFAULT '{}',
    parent_message_id UUID REFERENCES messages(id),
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

-- Message Change Tracking
CREATE TABLE message_changes (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    message_id UUID REFERENCES messages(id) ON DELETE CASCADE,
    change_type VARCHAR(20) NOT NULL,
    change_data JSONB NOT NULL,
    device_id UUID REFERENCES devices(id),
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    timestamp TIMESTAMP DEFAULT NOW(),
    sync_status VARCHAR(20) DEFAULT 'pending',
    conflict_id UUID
);

Acceptance Criteria:

  • Conversation management schema implemented
  • Message storage with JSONB content
  • Message threading support
  • Change tracking for synchronization
  • Performance indexes for message queries

TASK-007.1.1.3: File Sync & Workspace Schema πŸ“… Day 3

Duration: 1 day
Priority: High
Dependencies: TASK-007.1.1.2

Description:
Implement file synchronization and workspace management schema that supports cross-device file sync with conflict resolution.

Scope:

  • File synchronization state tracking
  • Workspace management and settings
  • File change detection and versioning
  • Sync operations and conflict resolution
  • Offline operations queue

Key Tables:

-- Workspaces
CREATE TABLE workspaces (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    name VARCHAR(200) NOT NULL,
    path VARCHAR(1000) NOT NULL,
    settings JSONB DEFAULT '{}',
    last_accessed TIMESTAMP DEFAULT NOW(),
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW(),
    UNIQUE(user_id, path)
);

-- File Sync State
CREATE TABLE file_sync_state (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    workspace_id UUID REFERENCES workspaces(id) ON DELETE CASCADE,
    file_path VARCHAR(1000) NOT NULL,
    file_hash VARCHAR(64) NOT NULL,
    file_size BIGINT,
    last_modified TIMESTAMP NOT NULL,
    sync_status VARCHAR(20) DEFAULT 'synced',
    conflict_resolution VARCHAR(20),
    metadata JSONB DEFAULT '{}',
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW(),
    UNIQUE(user_id, workspace_id, file_path)
);

-- Sync Operations
CREATE TABLE sync_operations (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    device_id UUID REFERENCES devices(id) ON DELETE SET NULL,
    operation_type VARCHAR(50) NOT NULL,
    resource_type VARCHAR(50) NOT NULL,
    resource_id VARCHAR(100) NOT NULL,
    operation_data JSONB NOT NULL,
    status VARCHAR(20) DEFAULT 'pending',
    conflict_data JSONB,
    created_at TIMESTAMP DEFAULT NOW(),
    completed_at TIMESTAMP
);

-- Offline Operations Queue
CREATE TABLE offline_operations (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    device_id UUID REFERENCES devices(id) ON DELETE CASCADE,
    operation_type VARCHAR(50) NOT NULL,
    operation_data JSONB NOT NULL,
    created_at TIMESTAMP DEFAULT NOW(),
    retry_count INTEGER DEFAULT 0,
    max_retries INTEGER DEFAULT 3,
    status VARCHAR(20) DEFAULT 'pending',
    error_message TEXT
);

Acceptance Criteria:

  • File synchronization schema implemented
  • Workspace management tables created
  • Sync operations tracking
  • Offline operations queue
  • Conflict resolution support

πŸ“Š Performance Considerations

Indexing Strategy

-- User and Authentication Indexes
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_status ON users(status);
CREATE INDEX idx_devices_user_id ON devices(user_id);
CREATE INDEX idx_sessions_user_device ON sessions(user_id, device_id);
CREATE INDEX idx_sessions_token ON sessions(session_token);

-- Message and Conversation Indexes
CREATE INDEX idx_conversations_user_id ON conversations(user_id);
CREATE INDEX idx_conversations_last_activity ON conversations(last_activity DESC);
CREATE INDEX idx_messages_conversation_created ON messages(conversation_id, created_at DESC);
CREATE INDEX idx_messages_user_created ON messages(user_id, created_at DESC);
CREATE INDEX idx_message_changes_user_status ON message_changes(user_id, sync_status, timestamp);

-- File Sync Indexes
CREATE INDEX idx_workspaces_user_id ON workspaces(user_id);
CREATE INDEX idx_file_sync_user_workspace ON file_sync_state(user_id, workspace_id);
CREATE INDEX idx_file_sync_status ON file_sync_state(sync_status, updated_at);
CREATE INDEX idx_sync_ops_user_status ON sync_operations(user_id, status, created_at);
CREATE INDEX idx_offline_ops_device_status ON offline_operations(device_id, status, created_at);

JSONB Optimization

-- JSONB indexes for frequently queried fields
CREATE INDEX idx_users_preferences_gin ON users USING GIN (preferences);
CREATE INDEX idx_messages_content_gin ON messages USING GIN (content);
CREATE INDEX idx_messages_metadata_gin ON messages USING GIN (metadata);
CREATE INDEX idx_file_sync_metadata_gin ON file_sync_state USING GIN (metadata);

πŸ§ͺ Testing Strategy

Data Integrity Testing

  • Foreign key constraint validation
  • Unique constraint testing
  • JSONB field validation
  • Cascade delete behavior

Performance Testing

  • Index effectiveness validation
  • Query performance benchmarking
  • Large dataset handling
  • Concurrent access testing

Migration Testing

  • Schema migration validation
  • Data migration testing
  • Rollback procedure testing
  • Version compatibility

πŸ“‹ Implementation Order

  1. Day 1: TASK-007.1.1.1 - Core User & Authentication Schema

    • Implement user management tables
    • Add device registration schema
    • Create session management
    • Add authentication indexes
  2. Day 2: TASK-007.1.1.2 - Conversation & Message Schema

    • Implement conversation tables
    • Add message storage schema
    • Create change tracking
    • Add message indexes
  3. Day 3: TASK-007.1.1.3 - File Sync & Workspace Schema

    • Implement workspace management
    • Add file sync schema
    • Create sync operations tracking
    • Add performance indexes

πŸ”„ Integration Points

With Previous Tasks

  • TASK-007.0.3: Uses production Docker infrastructure
  • TASK-006: Integrates with authentication requirements
  • TASK-005: Supports real-time communication needs

With Future Tasks

  • TASK-007.1.2: Provides foundation for migration system
  • TASK-007.1.3: Enables connection pooling optimization
  • TASK-007.2: Supports synchronization engine implementation

πŸ“ˆ Success Metrics

  • Schema Completeness: All required tables implemented
  • Performance: <100ms for typical queries
  • Data Integrity: 100% constraint validation
  • Index Effectiveness: >90% index usage for queries
  • Migration Success: Zero data loss during deployment

🎯 Next Steps After Completion

  1. TASK-007.1.2: Create Migration System
  2. TASK-007.1.3: Set Up Connection Pooling
  3. TASK-007.2.1: Implement Message History Sync
  4. Integration Testing: Validate with existing CCS components

πŸ“ Related Files


Breakdown Created: 2025-06-23
Total Estimated Duration: 3 days (unchanged)
Subtasks: 3 focused implementation tasks
Ready for: Immediate implementation after TASK-007.0.3 completion

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions