|
| 1 | +-- Migration 006: Orchestration Schema for Phase 4 Advanced Workflow Engine |
| 2 | +-- Creates tables for workflow definitions, executions, and state management |
| 3 | + |
| 4 | +-- Enable UUID extension if not already enabled |
| 5 | +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; |
| 6 | + |
| 7 | +-- Workflow definitions table |
| 8 | +CREATE TABLE workflows ( |
| 9 | + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 10 | + name VARCHAR(255) NOT NULL, |
| 11 | + version VARCHAR(50) NOT NULL, |
| 12 | + description TEXT, |
| 13 | + definition JSONB NOT NULL, |
| 14 | + status VARCHAR(50) NOT NULL DEFAULT 'draft', |
| 15 | + created_by VARCHAR(255) NOT NULL, |
| 16 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 17 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 18 | + |
| 19 | + -- Metadata fields |
| 20 | + tags TEXT[] DEFAULT '{}', |
| 21 | + category VARCHAR(100) DEFAULT 'general', |
| 22 | + priority VARCHAR(20) DEFAULT 'normal', |
| 23 | + environment VARCHAR(20) DEFAULT 'development', |
| 24 | + estimated_duration INTEGER, -- in milliseconds |
| 25 | + |
| 26 | + -- Constraints |
| 27 | + CONSTRAINT workflows_status_check CHECK (status IN ('draft', 'active', 'inactive', 'deprecated', 'archived')), |
| 28 | + CONSTRAINT workflows_priority_check CHECK (priority IN ('low', 'normal', 'high', 'critical')), |
| 29 | + CONSTRAINT workflows_environment_check CHECK (environment IN ('development', 'staging', 'production')), |
| 30 | + CONSTRAINT workflows_name_version_unique UNIQUE (name, version) |
| 31 | +); |
| 32 | + |
| 33 | +-- Workflow templates table |
| 34 | +CREATE TABLE workflow_templates ( |
| 35 | + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 36 | + name VARCHAR(255) NOT NULL UNIQUE, |
| 37 | + description TEXT, |
| 38 | + category VARCHAR(100) DEFAULT 'general', |
| 39 | + version VARCHAR(50) NOT NULL, |
| 40 | + definition JSONB NOT NULL, |
| 41 | + parameters JSONB DEFAULT '[]', |
| 42 | + examples JSONB DEFAULT '[]', |
| 43 | + documentation TEXT, |
| 44 | + created_by VARCHAR(255) NOT NULL, |
| 45 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 46 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 47 | + tags TEXT[] DEFAULT '{}', |
| 48 | + popularity INTEGER DEFAULT 0 |
| 49 | +); |
| 50 | + |
| 51 | +-- Workflow executions table |
| 52 | +CREATE TABLE workflow_executions ( |
| 53 | + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 54 | + workflow_id UUID NOT NULL REFERENCES workflows(id) ON DELETE CASCADE, |
| 55 | + workflow_version VARCHAR(50) NOT NULL, |
| 56 | + status VARCHAR(50) NOT NULL DEFAULT 'pending', |
| 57 | + context JSONB DEFAULT '{}', |
| 58 | + result JSONB, |
| 59 | + triggered_by VARCHAR(255) NOT NULL, |
| 60 | + trigger_type VARCHAR(50) NOT NULL DEFAULT 'manual', |
| 61 | + correlation_id VARCHAR(255), |
| 62 | + parent_execution_id UUID REFERENCES workflow_executions(id), |
| 63 | + |
| 64 | + -- Timing fields |
| 65 | + started_at TIMESTAMP WITH TIME ZONE, |
| 66 | + completed_at TIMESTAMP WITH TIME ZONE, |
| 67 | + duration INTEGER, -- in milliseconds |
| 68 | + |
| 69 | + -- Metadata |
| 70 | + metadata JSONB DEFAULT '{}', |
| 71 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 72 | + |
| 73 | + -- Constraints |
| 74 | + CONSTRAINT workflow_executions_status_check CHECK (status IN ('pending', 'running', 'completed', 'failed', 'cancelled', 'timeout', 'paused')), |
| 75 | + CONSTRAINT workflow_executions_trigger_type_check CHECK (trigger_type IN ('manual', 'schedule', 'event', 'webhook', 'api')) |
| 76 | +); |
| 77 | + |
| 78 | +-- Workflow step executions table |
| 79 | +CREATE TABLE workflow_step_executions ( |
| 80 | + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 81 | + execution_id UUID NOT NULL REFERENCES workflow_executions(id) ON DELETE CASCADE, |
| 82 | + step_id VARCHAR(255) NOT NULL, |
| 83 | + step_name VARCHAR(255), |
| 84 | + step_type VARCHAR(50), |
| 85 | + status VARCHAR(50) NOT NULL DEFAULT 'pending', |
| 86 | + input JSONB, |
| 87 | + output JSONB, |
| 88 | + error_message TEXT, |
| 89 | + error_code VARCHAR(100), |
| 90 | + error_details JSONB, |
| 91 | + retry_count INTEGER DEFAULT 0, |
| 92 | + |
| 93 | + -- Timing fields |
| 94 | + started_at TIMESTAMP WITH TIME ZONE, |
| 95 | + completed_at TIMESTAMP WITH TIME ZONE, |
| 96 | + duration INTEGER, -- in milliseconds |
| 97 | + |
| 98 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 99 | + |
| 100 | + -- Constraints |
| 101 | + CONSTRAINT workflow_step_executions_status_check CHECK (status IN ('pending', 'running', 'completed', 'failed', 'skipped', 'cancelled', 'timeout', 'retrying')) |
| 102 | +); |
| 103 | + |
| 104 | +-- Workflow execution logs table |
| 105 | +CREATE TABLE workflow_execution_logs ( |
| 106 | + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 107 | + execution_id UUID NOT NULL REFERENCES workflow_executions(id) ON DELETE CASCADE, |
| 108 | + step_execution_id UUID REFERENCES workflow_step_executions(id) ON DELETE CASCADE, |
| 109 | + level VARCHAR(10) NOT NULL, |
| 110 | + message TEXT NOT NULL, |
| 111 | + data JSONB, |
| 112 | + timestamp TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 113 | + |
| 114 | + -- Constraints |
| 115 | + CONSTRAINT workflow_execution_logs_level_check CHECK (level IN ('debug', 'info', 'warn', 'error')) |
| 116 | +); |
| 117 | + |
| 118 | +-- Workflow state management table |
| 119 | +CREATE TABLE workflow_states ( |
| 120 | + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 121 | + execution_id UUID NOT NULL REFERENCES workflow_executions(id) ON DELETE CASCADE, |
| 122 | + workflow_id UUID NOT NULL REFERENCES workflows(id) ON DELETE CASCADE, |
| 123 | + current_step VARCHAR(255), |
| 124 | + completed_steps TEXT[] DEFAULT '{}', |
| 125 | + failed_steps TEXT[] DEFAULT '{}', |
| 126 | + skipped_steps TEXT[] DEFAULT '{}', |
| 127 | + variables JSONB DEFAULT '{}', |
| 128 | + parallel_groups JSONB DEFAULT '{}', |
| 129 | + checkpoints JSONB DEFAULT '[]', |
| 130 | + last_updated TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 131 | + |
| 132 | + -- Ensure one state per execution |
| 133 | + CONSTRAINT workflow_states_execution_unique UNIQUE (execution_id) |
| 134 | +); |
| 135 | + |
| 136 | +-- Workflow state checkpoints table for detailed state tracking |
| 137 | +CREATE TABLE workflow_state_checkpoints ( |
| 138 | + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 139 | + execution_id UUID NOT NULL REFERENCES workflow_executions(id) ON DELETE CASCADE, |
| 140 | + step_id VARCHAR(255) NOT NULL, |
| 141 | + checkpoint_data JSONB NOT NULL, |
| 142 | + variables JSONB DEFAULT '{}', |
| 143 | + timestamp TIMESTAMP WITH TIME ZONE DEFAULT NOW() |
| 144 | +); |
| 145 | + |
| 146 | +-- Workflow metrics table for performance tracking |
| 147 | +CREATE TABLE workflow_metrics ( |
| 148 | + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 149 | + execution_id UUID NOT NULL REFERENCES workflow_executions(id) ON DELETE CASCADE, |
| 150 | + workflow_id UUID NOT NULL REFERENCES workflows(id) ON DELETE CASCADE, |
| 151 | + |
| 152 | + -- Performance metrics |
| 153 | + total_duration INTEGER, -- in milliseconds |
| 154 | + step_count INTEGER, |
| 155 | + successful_steps INTEGER, |
| 156 | + failed_steps INTEGER, |
| 157 | + skipped_steps INTEGER, |
| 158 | + parallel_executions INTEGER, |
| 159 | + retry_attempts INTEGER, |
| 160 | + |
| 161 | + -- Resource usage |
| 162 | + cpu_time INTEGER, -- in milliseconds |
| 163 | + memory_peak INTEGER, -- in bytes |
| 164 | + network_bytes BIGINT, |
| 165 | + storage_bytes BIGINT, |
| 166 | + |
| 167 | + -- Calculated metrics |
| 168 | + average_step_duration NUMERIC(10,2), |
| 169 | + parallel_efficiency NUMERIC(5,2), |
| 170 | + queue_wait_time INTEGER, |
| 171 | + resource_utilization NUMERIC(5,2), |
| 172 | + throughput NUMERIC(10,2), |
| 173 | + error_rate NUMERIC(5,2), |
| 174 | + |
| 175 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 176 | + |
| 177 | + -- Ensure one metrics record per execution |
| 178 | + CONSTRAINT workflow_metrics_execution_unique UNIQUE (execution_id) |
| 179 | +); |
| 180 | + |
| 181 | +-- Workflow schedules table for scheduled executions |
| 182 | +CREATE TABLE workflow_schedules ( |
| 183 | + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 184 | + workflow_id UUID NOT NULL REFERENCES workflows(id) ON DELETE CASCADE, |
| 185 | + name VARCHAR(255) NOT NULL, |
| 186 | + cron_expression VARCHAR(100) NOT NULL, |
| 187 | + timezone VARCHAR(50) DEFAULT 'UTC', |
| 188 | + enabled BOOLEAN DEFAULT true, |
| 189 | + start_date TIMESTAMP WITH TIME ZONE, |
| 190 | + end_date TIMESTAMP WITH TIME ZONE, |
| 191 | + max_executions INTEGER, |
| 192 | + execution_count INTEGER DEFAULT 0, |
| 193 | + last_execution TIMESTAMP WITH TIME ZONE, |
| 194 | + next_execution TIMESTAMP WITH TIME ZONE, |
| 195 | + created_by VARCHAR(255) NOT NULL, |
| 196 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 197 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() |
| 198 | +); |
| 199 | + |
| 200 | +-- Create indexes for performance optimization |
| 201 | + |
| 202 | +-- Workflows indexes |
| 203 | +CREATE INDEX idx_workflows_status ON workflows(status); |
| 204 | +CREATE INDEX idx_workflows_category ON workflows(category); |
| 205 | +CREATE INDEX idx_workflows_created_by ON workflows(created_by); |
| 206 | +CREATE INDEX idx_workflows_created_at ON workflows(created_at); |
| 207 | +CREATE INDEX idx_workflows_tags ON workflows USING GIN(tags); |
| 208 | + |
| 209 | +-- Workflow templates indexes |
| 210 | +CREATE INDEX idx_workflow_templates_category ON workflow_templates(category); |
| 211 | +CREATE INDEX idx_workflow_templates_popularity ON workflow_templates(popularity DESC); |
| 212 | +CREATE INDEX idx_workflow_templates_tags ON workflow_templates USING GIN(tags); |
| 213 | + |
| 214 | +-- Workflow executions indexes |
| 215 | +CREATE INDEX idx_workflow_executions_workflow_id ON workflow_executions(workflow_id); |
| 216 | +CREATE INDEX idx_workflow_executions_status ON workflow_executions(status); |
| 217 | +CREATE INDEX idx_workflow_executions_triggered_by ON workflow_executions(triggered_by); |
| 218 | +CREATE INDEX idx_workflow_executions_trigger_type ON workflow_executions(trigger_type); |
| 219 | +CREATE INDEX idx_workflow_executions_started_at ON workflow_executions(started_at); |
| 220 | +CREATE INDEX idx_workflow_executions_correlation_id ON workflow_executions(correlation_id); |
| 221 | +CREATE INDEX idx_workflow_executions_parent_id ON workflow_executions(parent_execution_id); |
| 222 | + |
| 223 | +-- Workflow step executions indexes |
| 224 | +CREATE INDEX idx_workflow_step_executions_execution_id ON workflow_step_executions(execution_id); |
| 225 | +CREATE INDEX idx_workflow_step_executions_step_id ON workflow_step_executions(step_id); |
| 226 | +CREATE INDEX idx_workflow_step_executions_status ON workflow_step_executions(status); |
| 227 | +CREATE INDEX idx_workflow_step_executions_started_at ON workflow_step_executions(started_at); |
| 228 | + |
| 229 | +-- Workflow execution logs indexes |
| 230 | +CREATE INDEX idx_workflow_execution_logs_execution_id ON workflow_execution_logs(execution_id); |
| 231 | +CREATE INDEX idx_workflow_execution_logs_step_execution_id ON workflow_execution_logs(step_execution_id); |
| 232 | +CREATE INDEX idx_workflow_execution_logs_level ON workflow_execution_logs(level); |
| 233 | +CREATE INDEX idx_workflow_execution_logs_timestamp ON workflow_execution_logs(timestamp); |
| 234 | + |
| 235 | +-- Workflow states indexes |
| 236 | +CREATE INDEX idx_workflow_states_execution_id ON workflow_states(execution_id); |
| 237 | +CREATE INDEX idx_workflow_states_workflow_id ON workflow_states(workflow_id); |
| 238 | +CREATE INDEX idx_workflow_states_current_step ON workflow_states(current_step); |
| 239 | +CREATE INDEX idx_workflow_states_last_updated ON workflow_states(last_updated); |
| 240 | + |
| 241 | +-- Workflow state checkpoints indexes |
| 242 | +CREATE INDEX idx_workflow_state_checkpoints_execution_id ON workflow_state_checkpoints(execution_id); |
| 243 | +CREATE INDEX idx_workflow_state_checkpoints_step_id ON workflow_state_checkpoints(step_id); |
| 244 | +CREATE INDEX idx_workflow_state_checkpoints_timestamp ON workflow_state_checkpoints(timestamp); |
| 245 | + |
| 246 | +-- Workflow metrics indexes |
| 247 | +CREATE INDEX idx_workflow_metrics_execution_id ON workflow_metrics(execution_id); |
| 248 | +CREATE INDEX idx_workflow_metrics_workflow_id ON workflow_metrics(workflow_id); |
| 249 | +CREATE INDEX idx_workflow_metrics_created_at ON workflow_metrics(created_at); |
| 250 | + |
| 251 | +-- Workflow schedules indexes |
| 252 | +CREATE INDEX idx_workflow_schedules_workflow_id ON workflow_schedules(workflow_id); |
| 253 | +CREATE INDEX idx_workflow_schedules_enabled ON workflow_schedules(enabled); |
| 254 | +CREATE INDEX idx_workflow_schedules_next_execution ON workflow_schedules(next_execution); |
| 255 | + |
| 256 | +-- Create composite indexes for common query patterns |
| 257 | +CREATE INDEX idx_workflow_executions_workflow_status ON workflow_executions(workflow_id, status); |
| 258 | +CREATE INDEX idx_workflow_executions_triggered_by_started ON workflow_executions(triggered_by, started_at); |
| 259 | +CREATE INDEX idx_workflow_step_executions_execution_status ON workflow_step_executions(execution_id, status); |
| 260 | + |
| 261 | +-- Add triggers for automatic timestamp updates |
| 262 | +CREATE OR REPLACE FUNCTION update_updated_at_column() |
| 263 | +RETURNS TRIGGER AS $$ |
| 264 | +BEGIN |
| 265 | + NEW.updated_at = NOW(); |
| 266 | + RETURN NEW; |
| 267 | +END; |
| 268 | +$$ language 'plpgsql'; |
| 269 | + |
| 270 | +CREATE TRIGGER update_workflows_updated_at BEFORE UPDATE ON workflows |
| 271 | + FOR EACH ROW EXECUTE FUNCTION update_updated_at_column(); |
| 272 | + |
| 273 | +CREATE TRIGGER update_workflow_templates_updated_at BEFORE UPDATE ON workflow_templates |
| 274 | + FOR EACH ROW EXECUTE FUNCTION update_updated_at_column(); |
| 275 | + |
| 276 | +CREATE TRIGGER update_workflow_schedules_updated_at BEFORE UPDATE ON workflow_schedules |
| 277 | + FOR EACH ROW EXECUTE FUNCTION update_updated_at_column(); |
| 278 | + |
| 279 | +-- Add trigger for automatic state updates |
| 280 | +CREATE OR REPLACE FUNCTION update_workflow_state_timestamp() |
| 281 | +RETURNS TRIGGER AS $$ |
| 282 | +BEGIN |
| 283 | + NEW.last_updated = NOW(); |
| 284 | + RETURN NEW; |
| 285 | +END; |
| 286 | +$$ language 'plpgsql'; |
| 287 | + |
| 288 | +CREATE TRIGGER update_workflow_states_timestamp BEFORE UPDATE ON workflow_states |
| 289 | + FOR EACH ROW EXECUTE FUNCTION update_workflow_state_timestamp(); |
| 290 | + |
| 291 | +-- Add comments for documentation |
| 292 | +COMMENT ON TABLE workflows IS 'Stores workflow definitions and metadata'; |
| 293 | +COMMENT ON TABLE workflow_templates IS 'Stores reusable workflow templates'; |
| 294 | +COMMENT ON TABLE workflow_executions IS 'Tracks individual workflow execution instances'; |
| 295 | +COMMENT ON TABLE workflow_step_executions IS 'Tracks execution of individual workflow steps'; |
| 296 | +COMMENT ON TABLE workflow_execution_logs IS 'Stores detailed execution logs'; |
| 297 | +COMMENT ON TABLE workflow_states IS 'Manages current state of workflow executions'; |
| 298 | +COMMENT ON TABLE workflow_state_checkpoints IS 'Stores state checkpoints for recovery'; |
| 299 | +COMMENT ON TABLE workflow_metrics IS 'Stores performance and usage metrics'; |
| 300 | +COMMENT ON TABLE workflow_schedules IS 'Manages scheduled workflow executions'; |
| 301 | + |
| 302 | +-- Grant permissions (adjust as needed for your security model) |
| 303 | +-- GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO workflow_service; |
| 304 | +-- GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO workflow_service; |
0 commit comments