ciallo-migrate

    Schema diff, SHA-256 drift detection, transactional migrations, and idempotent seeds for unmbt/ciallo-orm

    migrate
    sql
    schema
    sha256
    ciallo
    Download zip
    Author
    Version
    0.2.0
    License
    MIT
    Last updated
    5 hours ago
    Downloads
    19

    #unmbt/ciallo-migrate

    Schema diffing (up.sql / down.sql), pure-MoonBit SHA-256 checksum drift detection (_ciallo_migrations), transactional migration application, and idempotent seeding for unmbt/ciallo-orm.

    MigrateError

    pub(all) suberror MigrateError {
    ChecksumDrift(version~ : String, expected~ : String, actual~ : String)
    ApplyFailed(version~ : String, message~ : String)
    RollbackFailed(version~ : String, message~ : String)
    } derive(Eq,
    Debug
    )

    ColumnDef

    pub(all) struct ColumnDef {
    name : String
    sql_type : String
    nullable : Bool
    primary_key : Bool
    auto_increment : Bool
    unique : Bool
    default_expr : String?
    } derive(Eq,
    Debug
    ,
    FromJson
    ,
    ToJson
    )

    列定义快照(供 Schema Diff 与 DB-first 逆向建模共用)

    ColumnDef::ColumnDef

    fn ColumnDef::ColumnDef(name~ : String, sql_type~ : String, nullable? : Bool, primary_key? : Bool, auto_increment? : Bool, unique? : Bool, default_expr? : String) -> ColumnDef

    ForeignKeyAction

    pub(all) enum ForeignKeyAction {
    NoAction
    Restrict
    Cascade
    SetNull
    SetDefault
    } derive(Eq,
    Debug
    ,
    FromJson
    ,
    ToJson
    )

    外键级联行为

    ForeignKeyAction::to_sql

    fn ForeignKeyAction::to_sql(self : ForeignKeyAction) -> String

    ForeignKeyDef

    pub(all) struct ForeignKeyDef {
    name : String
    columns : Array[String]
    ref_table : String
    ref_columns : Array[String]
    on_delete : ForeignKeyAction
    on_update : ForeignKeyAction
    } derive(Eq,
    Debug
    ,
    FromJson
    ,
    ToJson
    )

    外键约束定义

    ForeignKeyDef::ForeignKeyDef

    fn ForeignKeyDef::ForeignKeyDef(name~ : String, columns~ : Array[String], ref_table~ : String, ref_columns~ : Array[String], on_delete? : ForeignKeyAction, on_update? : ForeignKeyAction) -> ForeignKeyDef

    IndexDef

    pub(all) struct IndexDef {
    name : String
    columns : Array[String]
    unique : Bool
    } derive(Eq,
    Debug
    ,
    FromJson
    ,
    ToJson
    )

    索引定义(普通索引 / 唯一索引)

    IndexDef::IndexDef

    fn IndexDef::IndexDef(name~ : String, columns~ : Array[String], unique? : Bool) -> IndexDef

    MigrationFile

    pub(all) struct MigrationFile {
    version : String
    name : String
    up_sql : String
    down_sql : String?
    } derive(Eq,
    Debug
    )

    MigrationFile::checksum

    fn MigrationFile::checksum(self : MigrationFile) -> String

    MigrationRecord

    pub(all) struct MigrationRecord {
    version : String
    name : String
    checksum_sha256 : String
    applied_at_ms : Int64
    } derive(Eq,
    Debug
    )

    MigrationStatusEntry

    pub(all) struct MigrationStatusEntry {
    version : String
    name : String
    applied : Bool
    checksum_matches : Bool
    } derive(Eq,
    Debug
    )

    SchemaDiffResult

    pub(all) struct SchemaDiffResult {
    up_sql : String
    down_sql : String
    has_changes : Bool
    } derive(Eq,
    Debug
    )

    SchemaSnapshot

    完整数据库 Schema 快照

    SchemaSnapshot::empty

    SchemaSnapshot::to_json

    automatically derived

    SeedFile

    pub(all) struct SeedFile {
    name : String
    sql : String
    } derive(Eq,
    Debug
    )

    TableDef

    pub(all) struct TableDef {
    name : String
    columns : Array[ColumnDef]
    indexes : Array[IndexDef]
    foreign_keys : Array[ForeignKeyDef]
    composite_primary_key : Array[String]
    } derive(Eq,
    Debug
    )

    表定义快照(支持普通/唯一索引、外键约束与复合主键)

    TableDef::TableDef

    fn TableDef::TableDef(name~ : String, columns~ : Array[ColumnDef], indexes? : Array[IndexDef], foreign_keys? : Array[ForeignKeyDef], composite_primary_key? : Array[String]) -> TableDef

    apply_migrations

    async fn apply_migrations(driver : &
    SqlDriver
    , migrations : Array[MigrationFile]) -> Array[MigrationRecord]

    事务化顺序执行未应用的迁移脚本(执行前先校验历史防漂移,失败自动回滚)

    diff_schemas

    比较新旧 SchemaSnapshot,按指定方言生成确定性 up.sql 与 down.sql 迁移脚本 (覆盖建/删表、列变更、普通/唯一索引、外键约束、复合主键与 SQLite 12 步安全重建表)

    migration_status

    async fn migration_status(driver : &
    SqlDriver
    , migrations : Array[MigrationFile]) -> Array[MigrationStatusEntry]

    查询所有迁移脚本的当前状态(是否已执行及校验和是否一致)

    render_sqlite_table_rebuild

    fn render_sqlite_table_rebuild(prev : TableDef, next : TableDef) -> String

    Standard SQLite 12-step safe table reconstruction DDL (handles column type/nullability/PK/FK alterations).

    rollback_migrations

    async fn rollback_migrations(driver : &
    SqlDriver
    , migrations : Array[MigrationFile], steps? : Int) -> Array[MigrationRecord]

    Rolls back the most recently applied steps migrations in reverse order, verifying historical checksums first and executing each migration's down_sql transactionally.

    run_seeds

    async fn run_seeds(driver : &
    SqlDriver
    , seeds : Array[SeedFile]) -> Array[String]

    幂等种子执行器:按种子名记录 _ciallo_seeds,重复执行安全跳过已运行种子

    sha256_str_hex

    fn sha256_str_hex(s : StringView) -> String

    Computes lowercase hex SHA-256 of a UTF-8 string using shared unmbt/webstd/crypto.

    split_sql_statements

    fn split_sql_statements(sql : StringView) -> Array[String]

    Splits a multi-statement SQL script into individual statements while respecting single-quoted literals ('...'), double-quoted identifiers ("..."), dollar-quoted blocks ($$...$$), -- line comments, and /* ... */ block comments.

    verify_migrations

    async fn verify_migrations(driver : &
    SqlDriver
    , migrations : Array[MigrationFile]) -> Unit

    校验目标库已执行迁移脚本的 SHA-256 校验和,精准拦截历史脚本漂移篡改