@@ -32,6 +32,7 @@ use diesel::r2d2::{ConnectionManager, Pool};
3232use juniper:: graphiql:: graphiql_source;
3333use juniper:: http:: GraphQLRequest ;
3434use serde:: { Deserialize , Serialize } ;
35+ use std:: path:: PathBuf ;
3536use std:: sync:: Arc ;
3637use structopt:: StructOpt ;
3738use 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) ]
7998fn 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