Skip to content

Commit abe218a

Browse files
authored
Merge pull request #21 from Philipp-M/fix_example_migrations_smallint
Fix problems with the example migrations
2 parents a87df35 + 03d7105 commit abe218a

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
@@ -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)