11//! Schema export command for WASM bindings.
22//!
33//! This command connects to a PostgreSQL database and exports the schema cache
4- //! as a JSON file that can be used with the WASM bindings.
4+ //! as JSON that can be used with the WASM bindings.
55
66use pgls_console:: { ConsoleExt , EnvConsole , markup} ;
77use pgls_schema_cache:: SchemaCache ;
88use sqlx:: postgres:: PgPoolOptions ;
9+ use std:: io:: Write ;
910use std:: path:: Path ;
1011
1112use crate :: CliDiagnostic ;
1213
13- /// Export the database schema to a JSON file .
14+ /// Export the database schema to JSON.
1415///
1516/// # Arguments
1617/// * `connection_string` - PostgreSQL connection string
17- /// * `output_path` - Path to write the JSON output
18+ /// * `output_path` - Optional path to write the JSON output (stdout if None)
1819pub async fn run_schema_export (
1920 connection_string : & str ,
20- output_path : & Path ,
21+ output_path : Option < & Path > ,
2122) -> Result < ( ) , CliDiagnostic > {
2223 let mut console = EnvConsole :: default ( ) ;
2324
24- console. log ( markup ! {
25- "Connecting to database..."
26- } ) ;
25+ // Only print progress to stderr if we're writing to stdout,
26+ // so the JSON output is clean and can be piped
27+ let write_to_stdout = output_path. is_none ( ) ;
28+
29+ if !write_to_stdout {
30+ console. log ( markup ! {
31+ "Connecting to database..."
32+ } ) ;
33+ }
2734
2835 // Connect to the database
2936 let pool = PgPoolOptions :: new ( )
@@ -36,9 +43,11 @@ pub async fn run_schema_export(
3643 ) ) )
3744 } ) ?;
3845
39- console. log ( markup ! {
40- "Loading schema cache..."
41- } ) ;
46+ if !write_to_stdout {
47+ console. log ( markup ! {
48+ "Loading schema cache..."
49+ } ) ;
50+ }
4251
4352 // Load the schema cache
4453 let schema_cache = SchemaCache :: load ( & pool) . await . map_err ( |e| {
@@ -47,9 +56,11 @@ pub async fn run_schema_export(
4756 ) ) )
4857 } ) ?;
4958
50- console. log ( markup ! {
51- "Serializing schema..."
52- } ) ;
59+ if !write_to_stdout {
60+ console. log ( markup ! {
61+ "Serializing schema..."
62+ } ) ;
63+ }
5364
5465 // Serialize to JSON
5566 let json = serde_json:: to_string_pretty ( & schema_cache) . map_err ( |e| {
@@ -58,33 +69,47 @@ pub async fn run_schema_export(
5869 ) ) )
5970 } ) ?;
6071
61- // Write to file
62- std:: fs:: write ( output_path, json) . map_err ( |e| {
63- CliDiagnostic :: io_error ( std:: io:: Error :: other ( format ! (
64- "Failed to write output file: {e}"
65- ) ) )
66- } ) ?;
72+ // Write output
73+ if let Some ( path) = output_path {
74+ std:: fs:: write ( path, & json) . map_err ( |e| {
75+ CliDiagnostic :: io_error ( std:: io:: Error :: other ( format ! (
76+ "Failed to write output file: {e}"
77+ ) ) )
78+ } ) ?;
6779
68- console. log ( markup ! {
69- "Schema exported to " <Emphasis >{ output_path . display( ) . to_string( ) } </Emphasis >
70- } ) ;
80+ console. log ( markup ! {
81+ "Schema exported to " <Emphasis >{ path . display( ) . to_string( ) } </Emphasis >
82+ } ) ;
7183
72- // Print summary
73- console. log ( markup ! {
74- "\n Schema summary:"
75- } ) ;
76- console. log ( markup ! {
77- " Schemas: " { schema_cache. schemas. len( ) . to_string( ) }
78- } ) ;
79- console. log ( markup ! {
80- " Tables: " { schema_cache. tables. len( ) . to_string( ) }
81- } ) ;
82- console. log ( markup ! {
83- " Functions: " { schema_cache. functions. len( ) . to_string( ) }
84- } ) ;
85- console. log ( markup ! {
86- " Types: " { schema_cache. types. len( ) . to_string( ) }
87- } ) ;
84+ // Print summary
85+ console. log ( markup ! {
86+ "\n Schema summary:"
87+ } ) ;
88+ console. log ( markup ! {
89+ " Schemas: " { schema_cache. schemas. len( ) . to_string( ) }
90+ } ) ;
91+ console. log ( markup ! {
92+ " Tables: " { schema_cache. tables. len( ) . to_string( ) }
93+ } ) ;
94+ console. log ( markup ! {
95+ " Functions: " { schema_cache. functions. len( ) . to_string( ) }
96+ } ) ;
97+ console. log ( markup ! {
98+ " Types: " { schema_cache. types. len( ) . to_string( ) }
99+ } ) ;
100+ } else {
101+ // Write to stdout
102+ std:: io:: stdout ( ) . write_all ( json. as_bytes ( ) ) . map_err ( |e| {
103+ CliDiagnostic :: io_error ( std:: io:: Error :: other ( format ! (
104+ "Failed to write to stdout: {e}"
105+ ) ) )
106+ } ) ?;
107+ std:: io:: stdout ( ) . write_all ( b"\n " ) . map_err ( |e| {
108+ CliDiagnostic :: io_error ( std:: io:: Error :: other ( format ! (
109+ "Failed to write to stdout: {e}"
110+ ) ) )
111+ } ) ?;
112+ }
88113
89114 Ok ( ( ) )
90115}
0 commit comments