Crates.io | mongodb-migrator |
lib.rs | mongodb-migrator |
version | 0.1.8 |
source | src |
created_at | 2022-06-29 20:17:30.966066 |
updated_at | 2023-01-04 20:56:43.822285 |
description | MongoDB migrations management tool |
homepage | |
repository | https://github.com/kakoc/mongodb_migrator |
max_upload_size | |
id | 615672 |
size | 142,061 |
Mongodb migrations management tool.
[dependencies]
mongodb-migrator = "0.1.8"
use anyhow::Result;
use async_trait::async_trait;
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 host_port = node.get_host_port(27017).unwrap();
let url = format!("mongodb://localhost:{}/", host_port);
let client = mongodb::Client::with_uri_str(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(())
}
}
#[async_trait]
impl Migration for M1 {
async fn up(&self, db: Database) -> Result<()> {
db.collection::<Users>("users")
.update_one(
bson::doc! { "name": "Batman" },
bson::doc! { "$set": { "name": "Superman" } },
None,
)
.await?;
Ok(())
}
}
#[derive(Serialize, Deserialize)]
struct Users {
name: String,
}
Fail first
Try all
Retries