Skip to main content

schema_sync/
lib.rs

1//! Developer: s4gor
2//! Github: https://github.com/s4gor
3//!
4//! # Schema Sync
5//!
6//! Production-grade schema synchronization for multi-tenant databases.
7//!
8//! ## Architecture Overview
9//!
10//! ```text
11//! ┌─────────────────────────────────────────────────────────────┐
12//! │                        CLI Layer                             │
13//! │  (dry-run, diff, validation, audit modes)                   │
14//! └───────────────────────┬─────────────────────────────────────┘
15//!                         │
16//! ┌───────────────────────▼─────────────────────────────────────┐
17//! │                    Engine Layer                              │
18//! │  ┌──────────┐  ┌──────────┐  ┌──────────┐  ┌──────────┐   │
19//! │  │ Planner  │→ │ Executor │→ │  Diff    │→ │ Snapshot │   │
20//! │  └──────────┘  └──────────┘  └──────────┘  └──────────┘   │
21//! └───────────────────────┬─────────────────────────────────────┘
22//!                         │
23//! ┌───────────────────────▼─────────────────────────────────────┐
24//! │                  Adapter Layer                               │
25//! │  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐      │
26//! │  │   Database   │  │  Migration   │  │   Schema     │      │
27//! │  │   Adapter    │  │   Runner     │  │  Inspector   │      │
28//! │  └──────────────┘  └──────────────┘  └──────────────┘      │
29//! └───────────────────────┬─────────────────────────────────────┘
30//!                         │
31//! ┌───────────────────────▼─────────────────────────────────────┐
32//! │              Database-Specific Implementations               │
33//! │  ┌──────────┐  ┌──────────┐  ┌──────────┐                 │
34//! │  │PostgreSQL│  │  MySQL   │  │  SQLite  │                 │
35//! │  └──────────┘  └──────────┘  └──────────┘                 │
36//! └─────────────────────────────────────────────────────────────┘
37//! ```
38//!
39//! ## Design Principles
40//!
41//! 1. **Trait-Based Extensibility**: All database operations go through traits,
42//!    allowing new database types to be added without changing core logic.
43//!
44//! 2. **Separation of Concerns**:
45//!    - **Adapters**: Database-specific connection and query execution
46//!    - **Inspectors**: Schema introspection (read-only)
47//!    - **Runners**: Migration execution (write operations)
48//!    - **Planner**: Determines what changes need to be made
49//!    - **Executor**: Orchestrates the execution of planned changes
50//!
51//! 3. **Tenant Isolation**: Every operation is scoped to a tenant context,
52//!    preventing cross-tenant data leakage.
53//!
54//! 4. **Mode-Agnostic Core**: The engine doesn't know about CLI modes;
55//!    modes are implemented at the CLI layer using the same engine primitives.
56//!
57//! ## Example Usage
58//!
59//! ```rust,no_run
60//! use schema_sync::prelude::*;
61//!
62//! #[tokio::main]
63//! async fn main() -> Result<()> {
64//!     // Create adapter (PostgreSQL implementation)
65//!     // let adapter = PostgresAdapter::new("postgresql://...").await?;
66//!
67//!     // Create inspector to read schema
68//!     // let inspector = adapter.inspector();
69//!
70//!     // Create migration runner
71//!     // let runner = adapter.migration_runner();
72//!
73//!     // Create planner, executor, and diff calculator
74//!     // let planner = DefaultPlanner::new();
75//!     // let executor = DefaultExecutor::new();
76//!     // let diff_calculator = DefaultDiffCalculator::new();
77//!
78//!     // Create engine
79//!     // let engine = Engine::from_adapter(
80//!     //     Box::new(adapter),
81//!     //     Box::new(planner),
82//!     //     Box::new(executor),
83//!     //     Box::new(diff_calculator),
84//!     //     None,
85//!     // );
86//!
87//!     // Sync schema for a tenant
88//!     let tenant = TenantContext::new("tenant_123");
89//!     // let target_snapshot = /* get target snapshot */;
90//!     // let result = engine.sync_tenant(&tenant, Some(&target_snapshot), true).await?;
91//!
92//!     // println!("Synced {} changes", result.changes_applied);
93//!     Ok(())
94//! }
95//! ```
96
97pub mod adapters;
98pub mod diff;
99pub mod engine;
100pub mod errors;
101pub mod planner;
102pub mod executor;
103pub mod snapshot;
104pub mod cli;
105
106/// Re-exports for convenience
107pub mod prelude {
108    pub use crate::adapters::{DatabaseAdapter, MigrationRunner, SchemaInspector};
109    pub use crate::diff::SchemaDiff;
110    pub use crate::engine::Engine;
111    pub use crate::errors::{Error, Result};
112    pub use crate::planner::Planner;
113    pub use crate::executor::Executor;
114    pub use crate::snapshot::{SchemaSnapshot, SnapshotStore};
115    pub use crate::cli::TenantContext;
116}
117