@@ -25,39 +25,60 @@ pub struct Guard {
2525}
2626
2727impl Guard {
28- async fn open ( filename : & Path , overwrite_existing : bool ) -> anyhow:: Result < ( Output , Guard ) > {
28+ async fn open (
29+ filename : & Path ,
30+ overwrite_existing : Option < bool > ,
31+ ) -> anyhow:: Result < ( Output , Guard ) > {
2932 if filename. to_str ( ) == Some ( "-" ) {
3033 Ok ( ( Box :: new ( io:: stdout ( ) ) , Guard { filenames : None } ) )
31- } else if cfg ! ( windows) || filename. starts_with ( "/dev/" ) || filename. file_name ( ) . is_none ( ) {
32- let file = OpenOptions :: new ( )
33- . write ( true )
34- . create ( overwrite_existing)
35- . create_new ( !overwrite_existing)
36- . truncate ( overwrite_existing)
37- . open ( & filename)
34+ } else if filename. starts_with ( "/dev/" ) {
35+ let file = fs:: File :: create ( & filename)
3836 . await
3937 . context ( filename. display ( ) . to_string ( ) ) ?;
4038 Ok ( ( Box :: new ( file) , Guard { filenames : None } ) )
4139 } else {
42- if !overwrite_existing && fs:: metadata ( & filename) . await . is_ok ( ) {
43- anyhow:: bail!(
44- "failed: target file already exists. Specify --overwrite-existing to replace."
45- )
46- }
47- // Create .~.tmp file path, first remove if already existing
48- let tmp_path = filename. with_file_name ( tmp_file_name ( filename) ) ;
49- if fs:: metadata ( & tmp_path) . await . is_ok ( ) {
50- fs:: remove_file ( & tmp_path) . await . ok ( ) ;
40+ let overwrite_existing = overwrite_existing. unwrap_or_else ( || {
41+ log:: warn!(
42+ "In the next EdgeDB CLI release, the dump behavior will \
43+ change to not overwrite the target file by default. For \
44+ compatibility, please specify --overwrite-existing to \
45+ preserve the current behavior, or --overwrite-existing=false \
46+ to adopt the new behavior."
47+ ) ;
48+ true
49+ } ) ;
50+ if cfg ! ( windows) || filename. file_name ( ) . is_none ( ) {
51+ let file = OpenOptions :: new ( )
52+ . write ( true )
53+ . create ( overwrite_existing)
54+ . create_new ( !overwrite_existing)
55+ . truncate ( overwrite_existing)
56+ . open ( & filename)
57+ . await
58+ . context ( filename. display ( ) . to_string ( ) ) ?;
59+ Ok ( ( Box :: new ( file) , Guard { filenames : None } ) )
60+ } else {
61+ if !overwrite_existing && fs:: metadata ( & filename) . await . is_ok ( ) {
62+ anyhow:: bail!(
63+ "failed: target file already exists. \
64+ Specify --overwrite-existing to replace."
65+ )
66+ }
67+ // Create .~.tmp file path, first remove if already existing
68+ let tmp_path = filename. with_file_name ( tmp_file_name ( filename) ) ;
69+ if fs:: metadata ( & tmp_path) . await . is_ok ( ) {
70+ fs:: remove_file ( & tmp_path) . await . ok ( ) ;
71+ }
72+ let tmp_file = fs:: File :: create ( & tmp_path)
73+ . await
74+ . context ( tmp_path. display ( ) . to_string ( ) ) ?;
75+ Ok ( (
76+ Box :: new ( tmp_file) ,
77+ Guard {
78+ filenames : Some ( ( tmp_path, filename. to_owned ( ) , overwrite_existing) ) ,
79+ } ,
80+ ) )
5181 }
52- let tmp_file = fs:: File :: create ( & tmp_path)
53- . await
54- . context ( tmp_path. display ( ) . to_string ( ) ) ?;
55- Ok ( (
56- Box :: new ( tmp_file) ,
57- Guard {
58- filenames : Some ( ( tmp_path, filename. to_owned ( ) , overwrite_existing) ) ,
59- } ,
60- ) )
6182 }
6283 }
6384
@@ -114,7 +135,7 @@ async fn dump_db(
114135 _options : & Options ,
115136 filename : & Path ,
116137 mut include_secrets : bool ,
117- overwrite_existing : bool ,
138+ overwrite_existing : Option < bool > ,
118139) -> Result < ( ) , anyhow:: Error > {
119140 if cli. get_version ( ) . await ?. specific ( ) < "4.0-alpha.2" . parse ( ) . unwrap ( ) {
120141 include_secrets = false ;
@@ -189,7 +210,7 @@ pub async fn dump_all(
189210
190211 fs:: create_dir_all ( dir) . await ?;
191212
192- let ( mut init, guard) = Guard :: open ( & dir. join ( "init.edgeql" ) , true ) . await ?;
213+ let ( mut init, guard) = Guard :: open ( & dir. join ( "init.edgeql" ) , Some ( true ) ) . await ?;
193214 if !config. trim ( ) . is_empty ( ) {
194215 init. write_all ( b"# DESCRIBE SYSTEM CONFIG\n " ) . await ?;
195216 init. write_all ( config. as_bytes ( ) ) . await ?;
@@ -207,7 +228,14 @@ pub async fn dump_all(
207228 match conn_params. branch ( database) ?. connect ( ) . await {
208229 Ok ( mut db_conn) => {
209230 let filename = dir. join ( & ( urlencoding:: encode ( database) + ".dump" ) [ ..] ) ;
210- dump_db ( & mut db_conn, options, & filename, include_secrets, true ) . await ?;
231+ dump_db (
232+ & mut db_conn,
233+ options,
234+ & filename,
235+ include_secrets,
236+ Some ( true ) ,
237+ )
238+ . await ?;
211239 }
212240 Err ( err) => {
213241 if let Some ( e) = err. downcast_ref :: < edgedb_errors:: Error > ( ) {
0 commit comments