Skip to content

Commit e214acc

Browse files
committed
Merge branch 'master' of github.com:weiznich/wundergraph
2 parents 488e1b8 + abe218a commit e214acc

File tree

3 files changed

+23
-4
lines changed
  • wundergraph_example

3 files changed

+23
-4
lines changed

wundergraph_example/migrations/pg/2018-01-24-131925_setup/up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ CREATE TABLE heros(
2020

2121
CREATE TABLE appears_in(
2222
hero_id INTEGER NOT NULL REFERENCES heros(id) ON DELETE CASCADE ON UPDATE RESTRICT,
23-
episode INTEGER NOT NULL CHECK(episode IN (1,2,3)),
23+
episode SMALLINT NOT NULL CHECK(episode IN (1,2,3)),
2424
PRIMARY KEY(hero_id, episode)
2525
);
2626

wundergraph_example/migrations/sqlite/2018-01-24-131925_setup/up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ CREATE TABLE heros(
2020

2121
CREATE TABLE appears_in(
2222
hero_id INTEGER NOT NULL REFERENCES heros(id) ON DELETE CASCADE ON UPDATE RESTRICT,
23-
episode INTEGER NOT NULL CHECK(episode IN (1,2,3)),
23+
episode SMALLINT NOT NULL CHECK(episode IN (1,2,3)),
2424
PRIMARY KEY(hero_id, episode)
2525
);
2626

wundergraph_example/src/bin/main.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use diesel::r2d2::{ConnectionManager, Pool};
3232
use juniper::graphiql::graphiql_source;
3333
use juniper::http::GraphQLRequest;
3434
use serde::{Deserialize, Serialize};
35+
use std::path::PathBuf;
3536
use std::sync::Arc;
3637
use structopt::StructOpt;
3738
use wundergraph::scalar::WundergraphScalarValue;
@@ -75,6 +76,24 @@ fn graphql(
7576
.body(serde_json::to_string(&res)?))
7677
}
7778

79+
fn run_migrations(conn: &DBConnection) {
80+
let mut migration_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
81+
migration_path.push("migrations");
82+
if cfg!(feature = "postgres") {
83+
migration_path.push("pg");
84+
} else if cfg!(feature = "sqlite") {
85+
migration_path.push("sqlite");
86+
}
87+
let pending_migrations =
88+
::diesel_migrations::mark_migrations_in_directory(conn, &migration_path)
89+
.unwrap()
90+
.into_iter()
91+
.filter_map(|(migration, run)| if run { None } else { Some(migration) });
92+
93+
::diesel_migrations::run_migrations(conn, pending_migrations, &mut ::std::io::stdout())
94+
.expect("Failed to run migrations");
95+
}
96+
7897
#[allow(clippy::print_stdout)]
7998
fn main() {
8099
let opt = Opt::from_args();
@@ -85,8 +104,8 @@ fn main() {
85104
.max_size(1)
86105
.build(manager)
87106
.expect("Failed to init pool");
88-
// ::diesel_migrations::run_pending_migrations(&pool.get().expect("Failed to get db connection"))
89-
// .expect("Failed to run migrations");
107+
108+
run_migrations(&pool.get().expect("Failed to get db connection"));
90109

91110
let query = Query::<MyContext<DBConnection>>::default();
92111
let mutation = Mutation::<MyContext<DBConnection>>::default();

0 commit comments

Comments
 (0)