Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:TobikoData Sqlmesh Plan Confirmation And Application

From Leeroopedia


Knowledge Sources
Domains Data_Engineering, Deployment
Last Updated 2026-02-07 00:00 GMT

Overview

Plan confirmation and application is the process of executing a reviewed and approved deployment plan by orchestrating database operations to push new model versions, backfill historical data, and promote virtual views to make changes visible in the target environment.

Description

After a deployment plan is created and reviewed—showing exactly what will change, which models need backfilling, and what operations will execute—the next phase is applying those changes to make them reality. This is the "apply" in the "plan and apply" workflow, analogous to Terraform's apply phase where infrastructure changes are actualized.

Application involves multiple coordinated operations across the data warehouse. First, new snapshot metadata must be persisted to the state backend, recording the new versions of changed models and their fingerprints. Then physical tables must be created or updated through backfill operations—executing transformation queries for the specified time ranges and materializing results. For incremental models, this might mean processing hundreds or thousands of discrete intervals (days, hours) in the optimal order respecting dependencies. Throughout execution, the system must track progress, handle failures gracefully, and support circuit breakers that allow canceling long-running operations.

The final critical step is promotion: updating the virtual layer views that applications query. These views are redirected to point to the new snapshot's physical tables, making the changes visible atomically. This indirection is what enables zero-downtime deployments—new versions are built completely in parallel, then cut over instantly by updating view definitions. If something goes wrong, views can be rolled back to previous snapshots just as quickly.

The apply phase must be idempotent and resumable. If execution fails halfway through backfilling 1000 intervals, restarting should pick up where it left off, not redo completed work. The system tracks processed intervals in state, skipping already-complete work. It must also coordinate across multiple database gateways for architectures where different models target different warehouses, handle transient failures with retries, emit progress notifications for long-running operations, and respect external circuit breakers that allow administrators to halt operations if issues are detected.

Usage

Plan application should only occur after thorough review of the plan output and explicit approval (human or automated). Use automatic application for trusted CI/CD deployments with comprehensive test coverage and non-critical environments. Require manual approval for production deployments with breaking changes, large backfills, or changes affecting critical business metrics. The apply phase is when actual costs are incurred and production systems are affected, so safeguards are essential. Common patterns include: applying development environment plans immediately for fast iteration, applying staging plans automatically after CI tests pass, requiring peer review and approval for production plans, scheduling expensive backfills for off-peak hours, and using circuit breakers during initial rollout to abort if anomalies detected. Failed applications should be investigated before retrying—repeated failures may indicate configuration issues or insufficient warehouse capacity.

Theoretical Basis

The core logic for plan confirmation and application follows this algorithm:

Pre-Application Validation:

  1. Verify plan has no uncategorized changes (would block application)
  2. Check if plan requires any action: has changes, needs backfill, or has unpromoted models
  3. Confirm target environment exists or can be created
  4. Validate warehouse connectivity and credentials
  5. Check circuit breaker status (not already aborted)

State Sync Preparation:

  1. Begin transaction/checkpoint in state backend
  2. Push new snapshot metadata to state database
    1. Record snapshot versions, fingerprints, and model definitions
    2. Associate snapshots with target environment
    3. Store interval boundaries and backfill requirements
  3. Create or update environment metadata
    1. Set environment name, suffix target (schema/catalog)
    2. Record snapshot mappings: which model versions are active
    3. Update environment timestamps and plan identifiers

Backfill Execution:

  1. Generate execution plan respecting model dependencies (topological order)
  2. For each model requiring backfilling:
    1. Retrieve list of intervals to process
    2. Determine if intervals can be processed in parallel based on model kind
    3. For each interval:
      1. Check if interval already processed (skip if complete)
      2. Execute transformation query for that interval
      3. Materialize results to physical table (snapshot-specific name)
      4. Record interval completion in state
      5. Emit progress update
    4. Handle failures:
      1. Mark interval as failed in state
      2. Apply retry logic if transient failure
      3. Abort if persistent failure or circuit breaker triggered

Dependency Coordination:

  1. Ensure upstream models complete before downstream models start
  2. For models spanning multiple gateways:
    1. Coordinate execution across different warehouse connections
    2. Handle cross-database dependencies appropriately
  3. Monitor circuit breaker: callable that checks if apply should abort
    1. Check circuit breaker between intervals and models
    2. If triggered, halt execution gracefully, preserve progress

Promotion Phase:

  1. Once all backfills complete successfully:
    1. For each snapshot in plan:
      1. Create or update view in target environment
      2. Point view to snapshot's physical table
      3. Use environment-specific naming (catalog/schema suffixes)
    2. Handle deployment ordering: promote models in dependency order
    3. For multi-gateway deployments: promote across all relevant warehouses

Finalization and Notification:

  1. Commit state backend transaction
  2. Update environment expiration timestamps
  3. Record plan application completion in audit log
  4. Emit apply success notifications
    1. Include environment name, plan identifier
    2. Attach execution metrics: duration, intervals processed, models updated
  5. Clean up temporary resources if applicable

Error Handling and Recovery:

  1. On failure at any stage:
    1. Record failure in state with error details
    2. Preserve progress: intervals already processed remain recorded
    3. Emit failure notification with exception traceback
    4. Allow retry: next application resumes from last checkpoint
  2. On circuit breaker trigger:
    1. Gracefully stop execution
    2. Mark plan as aborted (not failed)
    3. Preserve all state for potential resume
    4. Notify operators of abort

The algorithm must ensure consistency—either all changes are applied or none (within practical bounds). Partial applications should leave the environment in a queryable state, not corrupted. It must be observable—provide continuous feedback on progress for long-running operations. And it must be safe—failures should never destroy data, always preserving the ability to retry or rollback.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment