lib.rs

Overview

This file provides a set of utilities and abstractions focused on database schema migration management. It enables version-controlled migration of SQLite databases using embedded migration scripts. The core functionality revolves around applying, validating, and managing migrations to keep the database schema in sync with application requirements.

The file defines structures to represent database metadata and maintenance tasks, as well as the migration target version enumerations and helper functions for version resolution. It integrates with the [rusqlite] crate for database connections and [rusqlite_migration] for migration execution, while embedding migration files at compile time using the [include_dir] crate. Error handling is augmented by [anyhow] for context-rich results.

Key Components and Their Details

Struct: DbInfo

Represents metadata about a specific database, including its name and embedded migration scripts.

Struct: DbMaintenance

Encapsulates maintenance operations on a database, such as migrations.

Struct: DbMaintenanceOptions

Holds options to configure the behavior of database maintenance operations.

Enum: MigrateTo

Defines migration target versions.

Functions

resolve_version(migrate_to: &MigrateTo, latest_migrations_version: u32) -> anyhow::Result<u32>

Resolves the effective target version number to migrate to based on the MigrateTo enum and the highest available migration version.

get_latest_migration_version(migrations_dir: &'static Dir<'static>) -> anyhow::Result<u32>

Extracts the highest migration version number from the embedded migrations directory structure.

Testing

Implementation Details and Algorithms

Interaction with Other System Components

Mermaid Diagram: Structure of lib.rs

classDiagram
class DbInfo {
-name: &'static str
-migrations: Dir
+BM_ARCHIVE: DbInfo
}
class DbMaintenance {
-path: PathBuf
-info: &'static DbInfo
+new()
+migrate()
+migrate_all_to_latest()
}
class DbMaintenanceOptions {
+silent: bool
}
class MigrateTo {
<<enum>>
+None
+Latest
+Version(u32)
}
DbMaintenance o-- DbInfo
DbMaintenance ..> DbMaintenanceOptions
DbMaintenance ..> MigrateTo