main.rs

Overview

This file implements a command-line tool for managing and migrating the schema of an SQLite database named bm-archive.db associated with the Acki-Nacki DB project. It facilitates database schema version control by applying migrations stored as embedded resources, ensuring the database is created, initialized, and upgraded or downgraded to a specified version. The migration logic relies on the rusqlite and rusqlite_migration crates, employing migrations embedded at compile time via the include_dir crate.


Key Components

Enum MTarget<'a>

enum MTarget<'a> {
    BlockManager(Migrations<'a>),
}

Struct Args

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
    #[arg(short = 'p')]
    db_path: String,

    #[arg(long = "block-manager", value_name = "SCHEMA_VERSION", num_args = 0..=1, default_missing_value = "latest")]
    bm: Option<String>,
}

Fields

Field

Description

Usage Example

db_path

Path to the directory containing the database file bm-archive.db. If missing, directory is created.

-p ./data/db

bm

Optional target schema version for the block-manager database migration. Defaults to latest.

--block-manager 5 or --block-manager (for latest)


Function parse_version

fn parse_version(v: Option<String>, target: MTarget) -> anyhow::Result<u32>

Parameters

Parameter

Description

v

Optional string representing desired version or "latest".

target

Migration target specifying which migrations to query.

Returns

Usage Example

let version = parse_version(Some("3".to_string()), MTarget::BlockManager(migrations))?;

Function get_latest_migration_version

fn get_latest_migration_version(target: MTarget) -> anyhow::Result<u32>

Parameters

Parameter

Description

target

Migration target for which to get the latest version.

Returns


Function main

fn main() -> anyhow::Result<()>

Workflow

  1. Parse CLI arguments using Args::parse().

  2. Create database directory if missing.

  3. Open SQLite connection to bm-archive.db.

  4. Query current schema version from SQLite PRAGMA user_version.

  5. Load migrations embedded in M_BLOCK_MANAGER.

  6. Get latest migration version.

  7. Display current and latest schema versions.

  8. If migration target specified:

    • Parse target version.

    • Compare with current version.

    • Perform migration up or down accordingly.

  9. Exit gracefully.

Error Handling


Module tests


Implementation Details


Interaction with Other System Components


Visual Diagram

flowchart TD
A["main()"] --> B["Parse CLI Args (Args)"]
B --> C{Create DB Directory?}
C -- Yes --> D[Open SQLite Connection]
D --> E["Get current schema version (PRAGMA user_version)"]
E --> F["Load Embedded Migrations (M_BLOCK_MANAGER)"]
F --> G[Get Latest Migration Version]
G --> H[Print DB Info]
H --> I{Migration Version Specified?}
I -- No --> J[Exit]
I -- Yes --> K[Parse Target Version]
K --> L{Current Version == Target?}
L -- Yes --> M[Print "Up to date"]
L -- No --> N{Current < Target?}
N -- Yes --> O[Upgrade DB Schema]
N -- No --> P[Downgrade DB Schema]
O & P --> Q[Apply migration via rusqlite_migration]
Q --> J[Exit]

This flowchart summarizes the main workflow inside the main function, highlighting argument parsing, directory and database setup, migration version determination, and migration execution.


Usage Example

To create or migrate a database at path ./data/db to the latest schema version:

cargo run -- -p ./data/db

To migrate the block-manager database to version 3:

cargo run -- -p ./data/db --block-manager 3

Notes on Migration Versioning


Dependencies and Imports Overview


Error Handling


Testing

The test suite: