sqltrail-cli 0.1.0

Reusable PostgreSQL schema migration CLI and library: diff declared schema against a live database, generate versioned SQL, apply with a checksum-protected history
Documentation
//! # sqltrail-cli
//!
//! 通用的 PostgreSQL 结构迁移库与命令行工具。
//!
//! 提供完整迁移流水线:
//! **连接数据库 → 读取实际结构 → 与期望结构比较 → 生成迁移计划 → 渲染 SQL → 执行并记录历史**。
//!
//! ## 作为库使用(可被任意 Rust 项目引用)
//!
//! ```rust,ignore
//! use sqltrail_cli::{plan_from_database, SchemaModel, MigrationPlan};
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//!     let client = sqltrail_cli::database::connect("postgres://user:pass@localhost:5432/db").await?;
//!
//!     // 期望结构来源一:你的 Rust struct(配合 sqltrail-macros 派生宏)
//!     let desired: SchemaModel = /* User::schema() 或从 JSON 加载 */ SchemaModel::default();
//!
//!     // 期望结构来源二:加载 SchemaModel JSON 文件(可由 inspect 导出)
//!     // let desired: SchemaModel = serde_json::from_str(&std::fs::read_to_string("desired.json")?)?;
//!
//!     let plan: MigrationPlan = plan_from_database(&client, &desired).await?;
//!     println!("{}", sqltrail_cli::generator::render_plan(&plan));
//!     Ok(())
//! }
//! ```
//!
//! ## 作为 CLI 使用
//!
//! ```text
//! sqltrail-cli status
//! sqltrail-cli inspect
//! sqltrail-cli plan --desired desired.json
//! sqltrail-cli generate add_user_avatar --desired desired.json
//! sqltrail-cli apply [--allow-destructive]
//! ```

pub mod database;
pub mod generator;
pub mod history;
pub mod introspection;

// 结构模型、差异计算、迁移计划与统一校验全部通过本 crate 再导出,
// 接入项目只需依赖 sqltrail-cli(或直接依赖 sqltrail-core)。
pub use sqltrail_core::*;

/// 便捷入口:读取数据库实际结构,与期望结构比较,返回迁移计划。
///
/// 等价于 `build_plan(&compute_diff(desired, &introspection::introspect(client).await?))`。
pub async fn plan_from_database(
    client: &tokio_postgres::Client,
    desired: &SchemaModel,
) -> anyhow::Result<MigrationPlan> {
    if let Err(errors) = validate(desired) {
        anyhow::bail!("期望结构校验失败:{}", errors.join(";"));
    }
    let actual = introspection::introspect(client).await?;
    Ok(build_plan(&compute_diff(desired, &actual)))
}