Crate surrealdb_migrate

Crate surrealdb_migrate 

Source
Expand description

A lib to migrate a SurrealDB programmatically from within an application or to build a custom migration tool.

§Usage

For migrating or reverting a database the easiest way is to configure a MigrationRunner and use its functions MigrationRunner::migrate() and MigrationRunner::revert().

§Example

use std::path::Path;
use anyhow::Context;
use surrealdb_migrate::{
    config::{DbAuthLevel, DbClientConfig, RunnerConfig},
    runner::MigrationRunner,
    db_client::connect_to_database
};

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
    // setup database connection
    let db_config = DbClientConfig::default()
        .with_address("wss://localhost:9000")
        .with_namespace("playground")
        .with_database("examples")
        .with_auth_level(DbAuthLevel::Database)
        .with_username("example.user")
        .with_password("s3cr3t");
    let db = connect_to_database(&db_config)
        .await
        .context("failed to connect to examples database")?;

    // Instantiate the `MigrationRunner`
    let runner_config = RunnerConfig::default()
        .with_migrations_folder(Path::new("my_application/migrations"));
    let runner = MigrationRunner::new(runner_config);

    // Run all forward (up) migrations
    runner.migrate(&db).await?;

    Ok(())
}

For a fully working example see the run_migrations example.

The configuration of the database connection and the migration runner can be provided in an application specific way or by using the configuration mechanism provided by this crate (see next chapter below).

§Configuring the DB-connection and the Migration-Runner

Both the DB-connection and the MigrationRunner can be configured using the configuration mechanism provided by this crate. The configuration mechanism is gated behind the optional crate feature config.

This mechanism loads the configuration settings from the configuration file surrealdb-migrate.toml and from environment variables. See the settings module for more details.

§Crate features

FeatureDescriptionDefault
configProvides a configuration mechanism for the DB-connection and the migration runner (see settings module)no

Modules§

action
checksum
config
db_client
definition
error
files
migration
repository
result
runner
settings
Configuration mechanism for the surrealdb-migrate crate.