Skip to content

Commit de1ac77

Browse files
committed
Fixed a problem with running migrations in the example as they are in subfolders (pg, sqlite). They are now run depending on what database feature is selected (sqlite/postgres)
1 parent a87df35 commit de1ac77

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

wundergraph_example/src/bin/main.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use juniper::http::GraphQLRequest;
3838
use serde::{Deserialize, Serialize};
3939

4040
use failure::Error;
41+
use std::path::PathBuf;
4142
use std::sync::Arc;
4243
use structopt::StructOpt;
4344

@@ -82,6 +83,24 @@ fn graphql(
8283
.body(serde_json::to_string(&res)?))
8384
}
8485

86+
fn run_migrations(conn: &DBConnection) {
87+
let mut migration_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
88+
migration_path.push("migrations");
89+
if cfg!(feature = "postgres") {
90+
migration_path.push("pg");
91+
} else if cfg!(feature = "sqlite") {
92+
migration_path.push("sqlite");
93+
}
94+
let pending_migrations =
95+
::diesel_migrations::mark_migrations_in_directory(conn, &migration_path)
96+
.unwrap()
97+
.into_iter()
98+
.filter_map(|(migration, run)| if run { None } else { Some(migration) });
99+
100+
::diesel_migrations::run_migrations(conn, pending_migrations, &mut ::std::io::stdout())
101+
.expect("Failed to run migrations");
102+
}
103+
85104
#[allow(clippy::print_stdout)]
86105
fn main() {
87106
let opt = Opt::from_args();
@@ -92,8 +111,8 @@ fn main() {
92111
.max_size(1)
93112
.build(manager)
94113
.expect("Failed to init pool");
95-
::diesel_migrations::run_pending_migrations(&pool.get().expect("Failed to get db connection"))
96-
.expect("Failed to run migrations");
114+
115+
run_migrations(&pool.get().expect("Failed to get db connection"));
97116

98117
let query = Query::<MyContext<DBConnection>>::default();
99118
let mutation = Mutation::<MyContext<DBConnection>>::default();

0 commit comments

Comments
 (0)