Skip to main content

Module sync

Module sync 

Source
Expand description

db_ops_sync — Align db_ops/{backend}/migrations with the proto AST across all backends.

Inspired by the legacy_sql legacycore/db migration pattern. This module:

  1. Discovers the db_ops root at sibling level relative to the UDB crate root (nearest Cargo.toml). Override with UDB_DB_OPS_ROOT env var.

  2. Creates db_ops/<backend>/migrations and db_ops/<backend>/bootstrap for each backend selected. Also creates the configured seeders directory (UDB_SEEDERS_PATH / UDB_SEEDER_PATH, default: db_ops/seeds) with a .gitkeep.

  3. If <backend>/migrations is empty → writes baseline artifacts derived from the current proto AST. These become the canonical audit trail.

  4. If <backend>/migrations is non-empty → runs a double-checksum check:

    • Computes the current proto manifest checksum (SHA-256 over sorted AST).
    • Reads each artifact header for its -- UDB:proto_manifest_checksum=<sha> (or _udb_meta.proto_manifest_checksum for JSON, # UDB:proto_manifest_checksum: for YAML, // UDB:proto_manifest_checksum= for Cypher).
    • Files whose embedded checksum does not match the current proto checksum are marked Stale. Proto always wins.
    • By default, stale or headerless artifacts are refreshed in place so the checked-in baseline converges on the current proto state without manual steps.
    • When force_bootstrap is enabled, refreshed artifacts are written to <backend>/bootstrap for operator review instead of overwriting migrations/.
  5. Returns a MigrationSyncReport per backend, aggregated into MultiBackendSyncReport by sync_all_backends.

§Directory layout produced

<db_ops_root>/
├── postgres/
│   ├── migrations/          ← baseline SQL (proto-derived, written once)
│   └── bootstrap/           ← updated SQL when proto changes
├── qdrant/
│   ├── migrations/          ← JSON collection definitions
│   └── bootstrap/
├── minio/
│   ├── migrations/          ← JSON bucket policies
│   └── bootstrap/
├── redis/
│   ├── migrations/          ← YAML key-schema docs
│   └── bootstrap/
├── mongodb/
│   ├── migrations/          ← JSON collection validators
│   └── bootstrap/
├── neo4j/
│   ├── migrations/          ← Cypher constraint scripts
│   └── bootstrap/
├── clickhouse/
│   ├── migrations/          ← ClickHouse DDL SQL
│   └── bootstrap/
└── seeds/
    └── .gitkeep

When `UDB_SEEDERS_PATH` is set, the seeders directory is created at that
absolute path or, for relative values, relative to the repository root.

Structs§

DbOpsSyncConfig
Configuration for sync_db_ops and sync_all_backends.
MigrationFileRecord
Record for one SQL file processed during sync.
MigrationSyncReport
Full report returned by sync_db_ops.
MultiBackendSyncReport
Aggregate report from sync_all_backends.

Enums§

BackendSyncTarget
Which backend(s) to sync during a sync_db_ops / sync_all_backends run.
FileSyncStatus
Per-file sync status after a sync_db_ops run.
SyncError

Functions§

discover_db_ops_root
Discover the db_ops root directory by walking up from the current working directory to the nearest Cargo.toml file.
sync_all_backends
Synchronise all configured backends (or a specific one) with the current proto AST.
sync_db_ops
Synchronise db_ops/postgres/{migrations,bootstrap} with the current proto AST.