github crates.io build status

Mongodb migrations management tool.

Setup

[dependencies] mongodb-migrator = "0.1.0"

How to use

```rust use anyhow::Result; use asynctrait::asynctrait; use mongodb::Database; use serde_derive::{Deserialize, Serialize}; use testcontainers::Docker;

use mongodb_migrator::migration::Migration;

[tokio::main]

async fn main() -> Result<()> { let docker = testcontainers::clients::Cli::default(); let node = docker.run(testcontainers::images::mongo::Mongo::default()); let hostport = node.gethostport(27017).unwrap(); let url = format!("mongodb://localhost:{}/", hostport); let client = mongodb::Client::withuristr(url).await.unwrap(); let db = client.database("test");

let migrations: Vec<Box<dyn Migration>> = vec![Box::new(M0 {}), Box::new(M1 {})];
mongodb_migrator::migrator::DefaultMigrator::new()
    .with_conn(db.clone())
    .with_migrations_vec(migrations)
    .up()
    .await?;

Ok(())

}

struct M0 {} struct M1 {}

[async_trait]

impl Migration for M0 { async fn up(&self, db: Database) -> Result<()> { db.collection("users") .insert_one(bson::doc! { "name": "Batman" }, None) .await?;

    Ok(())
}

fn get_id(&self) -> &str {
    "M0"
}

}

[async_trait]

impl Migration for M1 { async fn up(&self, db: Database) -> Result<()> { db.collection::("users") .update_one( bson::doc! { "name": "Batman" }, bson::doc! { "$set": { "name": "Superman" } }, None, ) .await?;

    Ok(())
}

fn get_id(&self) -> &str {
    "M1"
}

}

[derive(Serialize, Deserialize)]

struct Users { name: String, } ```