@@ -38,6 +38,7 @@ use juniper::http::GraphQLRequest;
3838use serde:: { Deserialize , Serialize } ;
3939
4040use failure:: Error ;
41+ use std:: path:: PathBuf ;
4142use std:: sync:: Arc ;
4243use 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) ]
86105fn 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