1 个不稳定版本
0.1.0 | 2021年8月25日 |
---|
#2077 在 开发工具
在 2 个库中使用了
13KB
86 行
⚠️ 警告
这些库处于早期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!()
}
}
可用的迁移状态后端
- DynamoDb:
migrate_state_dynamodb
- 本地文件:
migrate_state_file
锁定
migrate
应支持状态锁定以防止数据竞争(并发迁移)。此功能尚未实现,但计划在 1.0 版本中实现...
新的迁移引导
migrate
命令行界面应该有一个子命令用于创建新的迁移占位符,该占位符应由迁移上下文特质的实现进行配置。此功能也计划在 1.0 版本中实现,但尚未实现...
目标
- 方便且灵活的 API
- 一致性
非目标
- 性能
贡献
欢迎任何贡献,只需确保通过所需的 CI 检查即可 :D
参考
migrate
的想法最初受到 east
迁移工具(来自 TypeScript 世界)的启发。
许可证
根据您的选择,该软件受以下任一许可证的许可:Apache License, Version 2.0 或 MIT许可证。除非您明确声明,否则根据Apache-2.0许可证定义,您提交的任何有意包含在本项目中的贡献,将双重许可如上所述,无任何附加条款或条件。
依赖项
~3–10MB
~90K SLoC