7.3 KiB
Phase 1.2: Models & Repositories Implementation - Complete
Date: 2024 Status: ✅ Complete Phase: Workflow Orchestration - Models & Repositories
Overview
Phase 1.2 successfully implemented all data models and repository layers for the workflow orchestration system. This provides the foundational database access layer needed for workflow execution.
Completed Tasks
1. Workflow Models Added to common/src/models.rs
New Enum Types
WorkflowTaskStatus- Enum for workflow task status tracking (Pending, Running, Completed, Failed, Skipped, Cancelled)- Note: This enum was added but may not be needed if we use
ExecutionStatusfor tasks
- Note: This enum was added but may not be needed if we use
New Model Modules
workflow module containing three core models:
-
WorkflowDefinitionid,ref,pack,pack_ref,label,description,versionparam_schema,out_schema,definition(JSONB)tags(array),enabled(boolean)created,updatedtimestamps
-
WorkflowExecutionid,execution(parent execution ID),workflow_def- Task state arrays:
current_tasks,completed_tasks,failed_tasks,skipped_tasks variables(JSONB for workflow-scoped variables)task_graph(JSONB for execution graph)status(ExecutionStatus),error_messagepaused(boolean),pause_reasoncreated,updatedtimestamps
-
WorkflowTaskExecutionid,workflow_execution,execution(child execution ID)task_name,task_index,task_batch(for with-items iterations)status(ExecutionStatus),started_at,completed_at,duration_msresult,error(JSONB)retry_count,max_retries,next_retry_attimeout_seconds,timed_outcreated,updatedtimestamps
Updated Existing Models
Action model - Added workflow support:
is_workflow: bool- Flag indicating if action is a workflowworkflow_def: Option<Id>- Reference to workflow definition
2. Workflow Repository Created (common/src/repositories/workflow.rs)
Comprehensive repository implementation with 875 lines of code implementing CRUD operations and specialized queries for all three workflow entities.
WorkflowDefinitionRepository
Standard CRUD Operations:
FindById,FindByRef,List,Create,Update,Delete
Specialized Queries:
find_by_pack(pack_id)- Get all workflows for a packfind_enabled()- Get all enabled workflowsfind_by_tag(tag)- Search workflows by tag
Input Structs:
CreateWorkflowDefinitionInput- All fields for creating a workflowUpdateWorkflowDefinitionInput- Optional fields for updates
WorkflowExecutionRepository
Standard CRUD Operations:
FindById,List,Create,Update,Delete
Specialized Queries:
find_by_execution(execution_id)- Get workflow by parent executionfind_by_status(status)- Get workflows by statusfind_paused()- Get all paused workflowsfind_by_workflow_def(workflow_def_id)- Get executions of a specific workflow
Input Structs:
CreateWorkflowExecutionInput- Initial workflow execution stateUpdateWorkflowExecutionInput- Runtime state updates (task lists, variables, status, etc.)
WorkflowTaskExecutionRepository
Standard CRUD Operations:
FindById,List,Create,Update,Delete
Specialized Queries:
find_by_workflow_execution(workflow_execution_id)- Get all tasks for a workflowfind_by_task_name(workflow_execution_id, task_name)- Get specific task instancesfind_pending_retries()- Get tasks ready for retryfind_timed_out()- Get tasks that timed outfind_by_execution(execution_id)- Get task by child execution ID
Input Structs:
CreateWorkflowTaskExecutionInput- Task execution initializationUpdateWorkflowTaskExecutionInput- Task status and result updates
3. Action Repository Updates (common/src/repositories/action.rs)
Updated All SELECT Queries
- Added
is_workflowandworkflow_defcolumns to all queries - Ensures consistency across all action-related operations
New Workflow-Specific Methods
find_workflows()- Get all actions that are workflows (is_workflow = true)find_by_workflow_def(workflow_def_id)- Get action linked to a workflow definitionlink_workflow_def(action_id, workflow_def_id)- Link an action to a workflow definition
4. Repository Module Updates (common/src/repositories/mod.rs)
- Added
pub mod workflow;declaration - Exported all three workflow repositories:
WorkflowDefinitionRepositoryWorkflowExecutionRepositoryWorkflowTaskExecutionRepository
Technical Details
Database Schema Alignment
All models precisely match the database schema created in Phase 1.1:
- Column names, types, and nullability match exactly
- Array types (TEXT[]) mapped to
Vec<String> - JSONB types mapped to
JsonDict(serde_json::Value) - BIGSERIAL primary keys mapped to
Id(i64) - Timestamps use
DateTime<Utc>from chrono
SQLx Integration
- All models use
#[derive(FromRow)]for automatic mapping - Queries use
sqlx::query_asfor type-safe result mapping - Enums use
#[sqlx(type_name = "...")]for PostgreSQL enum mapping
Error Handling
- Consistent use of
Result<T>return types - Repository trait bounds ensure proper error propagation
- Not found errors use
Error::not_found()helper - Validation errors use
Error::validation()helper
Query Builder Pattern
- Update operations use
QueryBuilderfor dynamic SQL construction - Only modified fields are included in UPDATE statements
- Prevents unnecessary database writes when no changes are made
Verification
Compilation Status
✅ All checks passed:
cargo check -p attune-common # Success (6.06s)
cargo check # Success (15.10s)
No Errors or Warnings
- Zero compilation errors
- Zero warnings in common crate
- Existing warnings in other crates are unrelated to this work
Files Modified
crates/common/src/models.rs- Added workflow models and updated Action modelcrates/common/src/repositories/workflow.rs- New file with 875 linescrates/common/src/repositories/action.rs- Updated queries and added workflow methodscrates/common/src/repositories/mod.rs- Added workflow repository exports
Next Steps (Phase 1.3)
With the data layer complete, the next phase will implement:
- YAML Parser - Parse workflow YAML files into workflow definitions
- Validation - Validate workflow structure and task references
- Template Engine Integration - Set up Jinja2/Tera for variable templating
- Schema Utilities - JSON Schema validation helpers
Ready to proceed to: Phase 1.3 - YAML Parsing & Validation
Notes
- The
WorkflowTaskStatusenum was added but may be redundant since we're usingExecutionStatusfor task tracking - Consider removing or consolidating in a future refactor if not needed
- All specialized query methods follow existing repository patterns for consistency
- The repository layer provides a clean abstraction for workflow orchestration logic
Development Time
Estimated: 2-3 hours
Actual: ~45 minutes (efficient reuse of existing patterns)
Phase 1.2 Status: ✅ COMPLETE AND VERIFIED