Skip to content

Commit e23187e

Browse files
committed
version bump
1 parent cec6a3e commit e23187e

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

springtime-migrate-refinery/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "springtime-migrate-refinery"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition.workspace = true
55
authors.workspace = true
66
description = "SQL migration framework based on dependency injection."

springtime-migrate-refinery/README.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Springtime Migrate Reginery
1+
# Springtime Migrate Refinery
22

33
[![crates.io version](https://img.shields.io/crates/v/springtime-migrate-refinery.svg)](https://crates.io/crates/springtime-migrate-refinery)
44
![build status](https://github.com/krojew/springtime/actions/workflows/rust.yml/badge.svg)
@@ -19,3 +19,61 @@ Note: in addition to this crate, you need to also import
1919
* File-based and code-based migrations
2020
* Automatic migration application on startup for configured db clients
2121
* All `refinery` db clients supported
22+
23+
## Basic usage
24+
25+
As with `refinery`, the basic usage consists of creating or embedding migrations
26+
and providing a runner for desired database.
27+
28+
The following example assumes familiarity with
29+
[springtime](https://crates.io/crates/springtime) and
30+
[springtime-di](https://crates.io/crates/springtime-di).
31+
32+
```rust
33+
use refinery_core::Runner;
34+
use springtime::application;
35+
use springtime::future::{BoxFuture, FutureExt};
36+
use springtime_di::instance_provider::ErrorPtr;
37+
use springtime_migrate_refinery::migration::embed_migrations;
38+
use springtime_migrate_refinery::runner::MigrationRunnerExecutor;
39+
40+
// this is all that's needed to embed sql migrations from the given folder (the default path is
41+
// "migrations")
42+
embed_migrations!("examples/migrations");
43+
44+
// this is a migration source, which can provide migrations from code, instead of sql files
45+
#[derive(Component)]
46+
struct ExampleMigrationSource;
47+
48+
// register the source with dependency injection
49+
#[component_alias]
50+
impl MigrationSource for ExampleMigrationSource {
51+
fn migrations(&self) -> Result<Vec<Migration>, ErrorPtr> {
52+
Migration::unapplied("V00__test", "CREATE TABLE test (id INTEGER PRIMARY KEY);")
53+
.map(|migration| vec![migration])
54+
.map_err(|error| Arc::new(error) as ErrorPtr)
55+
}
56+
}
57+
58+
// refinery migration runner needs a concrete DB client to run - this necessitates an abstraction
59+
// layer; please see MigrationRunnerExecutor for details
60+
struct ExampleMigrationRunnerExecutor;
61+
62+
impl MigrationRunnerExecutor for ExampleMigrationRunnerExecutor {
63+
fn run_migrations(&self, _runner: &Runner) -> BoxFuture<'_, Result<(), ErrorPtr>> {
64+
// run migrations here with the given runner
65+
async { Ok(()) }.boxed()
66+
}
67+
}
68+
69+
// note: for the sake of simplicity, errors are unwrapped, rather than gracefully handled
70+
#[tokio::main]
71+
async fn main() {
72+
// create our application, which will run refinery migration runner before other runners
73+
let mut application =
74+
application::create_default().expect("unable to create default application");
75+
76+
// will run migrations from the "migrations" folder if MigrationRunnerExecutor(s) are available
77+
application.run().await.expect("error running application");
78+
}
79+
```

springtime-migrate-refinery/src/migration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use springtime_di::injectable;
99
/// Embed migrations from a given path (`migrations` by default). Path is inspected for `*.sql`
1010
/// files, which are converted into [MigrationSources](MigrationSource).
1111
///
12-
/// ```ignore
12+
/// ```
1313
/// use springtime_migrate_refinery::migration::embed_migrations;
14-
/// embed_migrations!("path/to/migrations");
14+
/// embed_migrations!("examples/migrations");
1515
/// ```
1616
pub use springtime_migrate_refinery_macros::embed_migrations;
1717

0 commit comments

Comments
 (0)