Skip to main content

schema_sync/
executor.rs

1//! Developer: s4gor
2//! Github: https://github.com/s4gor
3//!
4//! Migration execution
5//!
6//! The executor orchestrates the execution of migration plans.
7//! It handles:
8//! - Lock acquisition
9//! - Transaction management
10//! - Step-by-step execution
11//! - Rollback on failure
12//! - Progress tracking
13//!
14//! ## Design Rationale
15//!
16//! Separating execution from planning allows:
17//! - Different execution strategies (transactional, non-transactional)
18//! - Progress reporting
19//! - Retry logic
20//! - Testing with mock executors
21
22use async_trait::async_trait;
23
24use crate::adapters::MigrationRunner;
25use crate::errors::Result;
26use crate::planner::MigrationPlan;
27
28/// Result of executing a migration plan
29#[derive(Debug, Clone)]
30pub struct ExecutionResult {
31    /// Number of steps executed successfully
32    pub steps_executed: usize,
33
34    /// Total number of steps in the plan
35    pub total_steps: usize,
36
37    /// Whether execution completed successfully
38    pub success: bool,
39
40    /// Error message if execution failed
41    pub error: Option<String>,
42
43    /// Duration of execution
44    pub duration_secs: f64,
45}
46
47/// Trait for executing migration plans
48///
49/// The executor coordinates between the planner, migration runner,
50/// and other components to safely execute migrations.
51#[async_trait]
52pub trait Executor: Send + Sync {
53    /// Execute a migration plan for a tenant
54    ///
55    /// # Arguments
56    ///
57    /// * `tenant` - The tenant context
58    /// * `plan` - The migration plan to execute
59    /// * `runner` - The migration runner to use
60    ///
61    /// # Returns
62    ///
63    /// Execution result with success status and details.
64    async fn execute(
65        &self,
66        tenant: &crate::cli::TenantContext,
67        plan: &MigrationPlan,
68        runner: &dyn MigrationRunner,
69    ) -> Result<ExecutionResult>;
70
71    /// Execute a migration plan in dry-run mode
72    ///
73    /// This validates the plan and returns what would happen,
74    /// but doesn't actually execute any changes.
75    async fn dry_run(
76        &self,
77        tenant: &crate::cli::TenantContext,
78        plan: &MigrationPlan,
79        runner: &dyn MigrationRunner,
80    ) -> Result<ExecutionResult>;
81}
82