Skip to main content

Crate schema_sync

Crate schema_sync 

Source
Expand description

Developer: s4gor Github: https://github.com/s4gor

§Schema Sync

Production-grade schema synchronization for multi-tenant databases.

§Architecture Overview

┌─────────────────────────────────────────────────────────────┐
│                        CLI Layer                             │
│  (dry-run, diff, validation, audit modes)                   │
└───────────────────────┬─────────────────────────────────────┘
                        │
┌───────────────────────▼─────────────────────────────────────┐
│                    Engine Layer                              │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐  ┌──────────┐   │
│  │ Planner  │→ │ Executor │→ │  Diff    │→ │ Snapshot │   │
│  └──────────┘  └──────────┘  └──────────┘  └──────────┘   │
└───────────────────────┬─────────────────────────────────────┘
                        │
┌───────────────────────▼─────────────────────────────────────┐
│                  Adapter Layer                               │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐      │
│  │   Database   │  │  Migration   │  │   Schema     │      │
│  │   Adapter    │  │   Runner     │  │  Inspector   │      │
│  └──────────────┘  └──────────────┘  └──────────────┘      │
└───────────────────────┬─────────────────────────────────────┘
                        │
┌───────────────────────▼─────────────────────────────────────┐
│              Database-Specific Implementations               │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐                 │
│  │PostgreSQL│  │  MySQL   │  │  SQLite  │                 │
│  └──────────┘  └──────────┘  └──────────┘                 │
└─────────────────────────────────────────────────────────────┘

§Design Principles

  1. Trait-Based Extensibility: All database operations go through traits, allowing new database types to be added without changing core logic.

  2. Separation of Concerns:

    • Adapters: Database-specific connection and query execution
    • Inspectors: Schema introspection (read-only)
    • Runners: Migration execution (write operations)
    • Planner: Determines what changes need to be made
    • Executor: Orchestrates the execution of planned changes
  3. Tenant Isolation: Every operation is scoped to a tenant context, preventing cross-tenant data leakage.

  4. Mode-Agnostic Core: The engine doesn’t know about CLI modes; modes are implemented at the CLI layer using the same engine primitives.

§Example Usage

use schema_sync::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    // Create adapter (PostgreSQL implementation)
    // let adapter = PostgresAdapter::new("postgresql://...").await?;

    // Create inspector to read schema
    // let inspector = adapter.inspector();

    // Create migration runner
    // let runner = adapter.migration_runner();

    // Create planner, executor, and diff calculator
    // let planner = DefaultPlanner::new();
    // let executor = DefaultExecutor::new();
    // let diff_calculator = DefaultDiffCalculator::new();

    // Create engine
    // let engine = Engine::from_adapter(
    //     Box::new(adapter),
    //     Box::new(planner),
    //     Box::new(executor),
    //     Box::new(diff_calculator),
    //     None,
    // );

    // Sync schema for a tenant
    let tenant = TenantContext::new("tenant_123");
    // let target_snapshot = /* get target snapshot */;
    // let result = engine.sync_tenant(&tenant, Some(&target_snapshot), true).await?;

    // println!("Synced {} changes", result.changes_applied);
    Ok(())
}

Modules§

adapters
Developer: s4gor Github: https://github.com/s4gor
cli
Developer: s4gor Github: https://github.com/s4gor
diff
Developer: s4gor Github: https://github.com/s4gor
engine
Developer: s4gor Github: https://github.com/s4gor
errors
Developer: s4gor Github: https://github.com/s4gor
executor
Developer: s4gor Github: https://github.com/s4gor
planner
Developer: s4gor Github: https://github.com/s4gor
prelude
Re-exports for convenience
snapshot
Developer: s4gor Github: https://github.com/s4gor