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+ ```
0 commit comments