@@ -38,6 +38,7 @@ use juniper::http::GraphQLRequest;
38
38
use serde:: { Deserialize , Serialize } ;
39
39
40
40
use failure:: Error ;
41
+ use std:: path:: PathBuf ;
41
42
use std:: sync:: Arc ;
42
43
use structopt:: StructOpt ;
43
44
@@ -82,6 +83,24 @@ fn graphql(
82
83
. body ( serde_json:: to_string ( & res) ?) )
83
84
}
84
85
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
+
85
104
#[ allow( clippy:: print_stdout) ]
86
105
fn main ( ) {
87
106
let opt = Opt :: from_args ( ) ;
@@ -92,8 +111,8 @@ fn main() {
92
111
. max_size ( 1 )
93
112
. build ( manager)
94
113
. 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" ) ) ;
97
116
98
117
let query = Query :: < MyContext < DBConnection > > :: default ( ) ;
99
118
let mutation = Mutation :: < MyContext < DBConnection > > :: default ( ) ;
0 commit comments