1 个不稳定版本

0.1.0 2021 年 8 月 25 日

#1214开发工具

Download history 32/week @ 2024-03-11 22/week @ 2024-03-18 28/week @ 2024-03-25 63/week @ 2024-04-01 13/week @ 2024-04-08 20/week @ 2024-04-15 22/week @ 2024-04-22 19/week @ 2024-04-29 22/week @ 2024-05-06 19/week @ 2024-05-13 24/week @ 2024-05-20 71/week @ 2024-05-27 19/week @ 2024-06-03 17/week @ 2024-06-10 17/week @ 2024-06-17 15/week @ 2024-06-24

69 每月下载次数
5 个包中使用 (4 直接)

MIT/Apache

7KB

⚠️ 注意

包处于早期 MVP 阶段。您可能已经使用它们,并且它们会为您提供足够的功能,但可能尚未涵盖一些高级用例。

docs.rs crates.io
migrate
migrate-core
migrate-state
migrate-state-dynamodb
migrate-state-file
migrate-state-test

master 分支的文档可在 此处 查找。

migrate

migrate 是一款通用迁移工具。它提供了一个灵活的接口,用于在 Rust 中编写迁移脚本,并为您处理迁移状态记录。

概述

migrate 能够迁移任何类型的外部状态:生产数据库、云资源等。它监控应应用或回滚的迁移。在更高级的设置中,migrate 使用外部锁来防止数据竞争,确保在任何时候只有一个迁移正在运行(目前尚未实现)。

在基本情况下,您应该能够仅实现 up 方法,并(可选地)实现 down 方法。

示例样板

use async_trait::async_trait;
use migrate::MigrateCli;
use migrate::core::{Migration, Plan, MigrationCtxProvider};
use std::error::Error;
use rusoto_dynamodb::DynamoDb;

// File where `migrate` CLI will record the list of allready applied migrations
// This is called the migration state and there are several different backends
// that implement it. You may also implement your own, e.g. store the migration
// state in your own database.
// See the list of ready-to-use state backends bellow.
const MIGRATION_STATE_FILE_PATH: &str = "./migration-state";

type Result<T, E = Box<dyn Error + Send + Sync>> = std::result::Result<T, E>;

struct MyMigration;

#[async_trait]
impl Migration for MyMigration {
    type Ctx = rusoto_dynamodb::DynamoDbClient;

    async fn up(&mut self, ctx: &mut Self::Ctx) -> Result<()> {
        // Apply forward migration logic with the given context
        ctx.put_item(todo!()).await?;
    }

    async fn down(&mut self, ctx: &mut Self::Ctx) -> Result<()> {
        // Rollback the applied migration.
        // Ideally this should be purely inverse to up()
        ctx.delete_item(todo!()).await?;
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let state_storage = migrate_state_file::FileStateLock::new(MIGRATION_STATE_FILE_PATH);

    let mut plan = Plan::builder(state_storage);

    plan.ctx_provider(DynamoDbClientProvider)
        // Add migrations in order one after each other to the plan
        .migration("migration-1", MyMigration);

    // Run the `migrate` cli to get the parameters of how to
    // build and execute the rest of the migration plan
    // let cli = migrate::MigrateCli::from_cli_args();

    // Run the CLI (this is run in a test, it's commented out not to run CLI)
    // cli.run(plan).await?;

    Ok(())
}

struct DynamoDbClientProvider;

#[async_trait]
impl MigrationCtxProvider for DynamoDbClientProvider {
    type Ctx = rusoto_dynamodb::DynamoDbClient;

    async fn create_in_commit_mode(self: Box<Self>) -> Result<Self::Ctx> {
        // Create real database client that will do real changes on real data
        todo!()
    }

    async fn create_in_no_commit_mode(self: Box<Self>) -> Option<Result<Self::Ctx>> {
        // We can provide some mocked implementation of database client here
        // via the usage of traits or enums so that the client doesn't commit changes to the database
        todo!()
    }
}

可用的迁移状态后端

锁定

migrate 应该支持状态锁定以防止数据竞争(并发迁移)。此功能尚未实现,但计划在 1.0 版本中实现...

新的迁移引导

migrate CLI 应该有一个子命令用于创建新的迁移占位符,该占位符应由迁移上下文特质的实现来配置。此功能也计划在 1.0 版本中实现,但尚未实现...

目标

  • 方便且灵活的 API
  • 一致性

非目标

  • 性能

贡献

欢迎任何贡献,只需确保通过所需的 CI 检查即可 :D

参考

migrate 的想法最初受到了 east 迁移工具(来自 TypeScript 世界)的启发。

许可

许可协议为 Apache License, Version 2.0 或 MIT 许可协议,您可选择其一。
除非您明确表示,否则您根据 Apache-2.0 许可协议定义的贡献,将双重许可,如上所述,没有任何附加条款或条件。

依赖项

约290–750KB
约18K SLoC