#mongodb

mongodb-migrator

MongoDB migrations management tool

9 releases

Uses new Rust 2021

0.1.8 Jan 4, 2023
0.1.7 Dec 29, 2022
0.1.4 Jul 6, 2022
0.1.1 Jun 29, 2022

#267 in Database interfaces

Download history 5/week @ 2022-10-21 3/week @ 2022-11-04 2/week @ 2022-11-11 5/week @ 2022-11-18 10/week @ 2022-11-25 22/week @ 2022-12-02 27/week @ 2022-12-09 8/week @ 2022-12-16 21/week @ 2022-12-23 56/week @ 2022-12-30 10/week @ 2023-01-06 8/week @ 2023-01-13 11/week @ 2023-01-20

104 downloads per month

MIT/Apache

37KB
790 lines

github crates.io docs.rs build status codecov.io

Mongodb migrations management tool.

Setup

[dependencies]
mongodb-migrator = "0.1.8"

Functionality

How to use

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,
}

Roadmap

  • Rust based migrations
  • JavaScript based migrations
  • Logging
  • Rollbacks
  • Cli tool
  • UI dashboard
  • RESTful service
  • As npm package
  • Stragegies
    • Fail first
    • Try all
    • Retries

Dependencies

~25–34MB
~701K SLoC