@@ -17,9 +17,13 @@ extern crate diesel;
1717extern crate dotenv;
1818extern crate infer_schema_internals;
1919extern crate migrations_internals;
20+ #[ macro_use]
21+ extern crate serde;
22+ extern crate toml;
2023#[ cfg( feature = "url" ) ]
2124extern crate url;
2225
26+ mod config;
2327mod database_error;
2428#[ macro_use]
2529mod database;
@@ -32,10 +36,12 @@ use chrono::*;
3236use clap:: { ArgMatches , Shell } ;
3337use migrations_internals:: { self as migrations, MigrationConnection } ;
3438use std:: any:: Any ;
39+ use std:: error:: Error ;
3540use std:: io:: stdout;
3641use std:: path:: { Path , PathBuf } ;
3742use std:: { env, fs} ;
3843
44+ use self :: config:: Config ;
3945use self :: database_error:: { DatabaseError , DatabaseResult } ;
4046use migrations_internals:: TIMESTAMP_FORMAT ;
4147
@@ -46,7 +52,7 @@ fn main() {
4652 let matches = cli:: build_cli ( ) . get_matches ( ) ;
4753
4854 match matches. subcommand ( ) {
49- ( "migration" , Some ( matches) ) => run_migration_command ( matches) ,
55+ ( "migration" , Some ( matches) ) => run_migration_command ( matches) . unwrap_or_else ( handle_error ) ,
5056 ( "setup" , Some ( matches) ) => run_setup_command ( matches) ,
5157 ( "database" , Some ( matches) ) => run_database_command ( matches) ,
5258 ( "bash-completion" , Some ( matches) ) => generate_bash_completion_command ( matches) ,
@@ -55,35 +61,37 @@ fn main() {
5561 }
5662}
5763
58- fn run_migration_command ( matches : & ArgMatches ) {
64+ fn run_migration_command ( matches : & ArgMatches ) -> Result < ( ) , Box < Error > > {
5965 match matches. subcommand ( ) {
6066 ( "run" , Some ( _) ) => {
6167 let database_url = database:: database_url ( matches) ;
6268 let dir = migrations_dir ( matches) ;
6369 call_with_conn ! (
6470 database_url,
6571 migrations:: run_pending_migrations_in_directory( & dir, & mut stdout( ) )
66- ) . unwrap_or_else ( handle_error) ;
72+ ) ?;
73+ regenerate_schema_if_file_specified ( matches) ?;
6774 }
6875 ( "revert" , Some ( _) ) => {
6976 let database_url = database:: database_url ( matches) ;
7077 let dir = migrations_dir ( matches) ;
7178 call_with_conn ! (
7279 database_url,
7380 migrations:: revert_latest_migration_in_directory( & dir)
74- ) . unwrap_or_else ( handle_error) ;
81+ ) ?;
82+ regenerate_schema_if_file_specified ( matches) ?;
7583 }
7684 ( "redo" , Some ( _) ) => {
7785 let database_url = database:: database_url ( matches) ;
7886 let dir = migrations_dir ( matches) ;
7987 call_with_conn ! ( database_url, redo_latest_migration( & dir) ) ;
88+ regenerate_schema_if_file_specified ( matches) ?;
8089 }
8190 ( "list" , Some ( _) ) => {
8291 let database_url = database:: database_url ( matches) ;
8392 let dir = migrations_dir ( matches) ;
8493 let mut migrations =
85- call_with_conn ! ( database_url, migrations:: mark_migrations_in_directory( & dir) )
86- . unwrap_or_else ( handle_error) ;
94+ call_with_conn ! ( database_url, migrations:: mark_migrations_in_directory( & dir) ) ?;
8795
8896 migrations. sort_by_key ( |& ( ref m, _) | m. version ( ) . to_string ( ) ) ;
8997
@@ -101,8 +109,8 @@ fn run_migration_command(matches: &ArgMatches) {
101109 }
102110 ( "pending" , Some ( _) ) => {
103111 let database_url = database:: database_url ( matches) ;
104- let result = call_with_conn ! ( database_url, migrations:: any_pending_migrations) ;
105- println ! ( "{:?}" , result. unwrap ( ) ) ;
112+ let result = call_with_conn ! ( database_url, migrations:: any_pending_migrations) ? ;
113+ println ! ( "{:?}" , result) ;
106114 }
107115 ( "generate" , Some ( args) ) => {
108116 use std:: io:: Write ;
@@ -111,10 +119,10 @@ fn run_migration_command(matches: &ArgMatches) {
111119 let version = migration_version ( args) ;
112120 let versioned_name = format ! ( "{}_{}" , version, migration_name) ;
113121 let migration_dir = migrations_dir ( matches) . join ( versioned_name) ;
114- fs:: create_dir ( & migration_dir) . unwrap ( ) ;
122+ fs:: create_dir ( & migration_dir) ? ;
115123
116124 let migration_dir_relative =
117- convert_absolute_path_to_relative ( & migration_dir, & env:: current_dir ( ) . unwrap ( ) ) ;
125+ convert_absolute_path_to_relative ( & migration_dir, & env:: current_dir ( ) ? ) ;
118126
119127 let up_path = migration_dir. join ( "up.sql" ) ;
120128 println ! (
@@ -129,12 +137,13 @@ fn run_migration_command(matches: &ArgMatches) {
129137 "Creating {}" ,
130138 migration_dir_relative. join( "down.sql" ) . display( )
131139 ) ;
132- let mut down = fs:: File :: create ( down_path) . unwrap ( ) ;
133- down. write_all ( b"-- This file should undo anything in `up.sql`" )
134- . unwrap ( ) ;
140+ let mut down = fs:: File :: create ( down_path) ?;
141+ down. write_all ( b"-- This file should undo anything in `up.sql`" ) ?;
135142 }
136143 _ => unreachable ! ( "The cli parser should prevent reaching here" ) ,
137- }
144+ } ;
145+
146+ Ok ( ( ) )
138147}
139148
140149use std:: fmt:: Display ;
@@ -165,6 +174,7 @@ fn migrations_dir(matches: &ArgMatches) -> PathBuf {
165174
166175fn run_setup_command ( matches : & ArgMatches ) {
167176 let migrations_dir = create_migrations_dir ( matches) . unwrap_or_else ( handle_error) ;
177+ create_config_file ( matches) . unwrap_or_else ( handle_error) ;
168178
169179 database:: setup_database ( matches, & migrations_dir) . unwrap_or_else ( handle_error) ;
170180}
@@ -187,6 +197,17 @@ fn create_migrations_dir(matches: &ArgMatches) -> DatabaseResult<PathBuf> {
187197 Ok ( dir. to_owned ( ) )
188198}
189199
200+ fn create_config_file ( matches : & ArgMatches ) -> DatabaseResult < ( ) > {
201+ use std:: io:: Write ;
202+ let path = Config :: file_path ( matches) ;
203+ if !path. exists ( ) {
204+ let mut file = fs:: File :: create ( path) ?;
205+ file. write_all ( include_bytes ! ( "default_files/diesel.toml" ) ) ?;
206+ }
207+
208+ Ok ( ( ) )
209+ }
210+
190211fn run_database_command ( matches : & ArgMatches ) {
191212 match matches. subcommand ( ) {
192213 ( "setup" , Some ( args) ) => {
@@ -327,6 +348,22 @@ fn run_infer_schema(matches: &ArgMatches) {
327348 ) . map_err ( handle_error :: < _ , ( ) > ) ;
328349}
329350
351+ fn regenerate_schema_if_file_specified ( matches : & ArgMatches ) -> Result < ( ) , Box < Error > > {
352+ use print_schema:: * ;
353+
354+ let config = Config :: read ( matches) ?;
355+ if let Some ( path) = config. print_schema . file {
356+ if let Some ( parent) = path. parent ( ) {
357+ fs:: create_dir_all ( parent) ?;
358+ }
359+
360+ let database_url = database:: database_url ( matches) ;
361+ let mut file = fs:: File :: create ( path) ?;
362+ print_schema:: output_schema ( & database_url, None , & Filtering :: None , false , & mut file) ?;
363+ }
364+ Ok ( ( ) )
365+ }
366+
330367#[ cfg( test) ]
331368mod tests {
332369 extern crate tempdir;
0 commit comments