@@ -40,16 +40,18 @@ pub struct GenerateTypesConfig {
4040pub struct DbConnectionConfig {
4141 #[ serde( rename = "DB_TYPE" ) ]
4242 pub db_type : DatabaseType ,
43- #[ serde( rename = "DB_HOST" ) ]
43+ #[ serde( rename = "DB_HOST" , default ) ]
4444 pub db_host : String ,
45- #[ serde( rename = "DB_PORT" ) ]
45+ #[ serde( rename = "DB_PORT" , default ) ]
4646 pub db_port : u16 ,
47- #[ serde( rename = "DB_USER" ) ]
47+ #[ serde( rename = "DB_USER" , default ) ]
4848 pub db_user : String ,
4949 #[ serde( rename = "DB_PASS" ) ]
5050 pub db_pass : Option < String > ,
5151 #[ serde( rename = "DB_NAME" ) ]
5252 pub db_name : Option < String > ,
53+ #[ serde( rename = "DB_URL" ) ]
54+ pub db_url : Option < String > ,
5355 #[ serde( rename = "PG_SEARCH_PATH" ) ]
5456 pub pg_search_path : Option < String > ,
5557 #[ serde( rename = "POOL_SIZE" , default = "default_pool_size" ) ]
@@ -218,43 +220,73 @@ impl Config {
218220 panic ! ( "" )
219221 } ) ;
220222
221- let db_host = & CLI_ARGS
222- . db_host
223+ let db_url = & CLI_ARGS
224+ . db_url
223225 . clone ( )
224- . or_else ( || dotenv. db_host . clone ( ) )
225- . or_else ( || default_config. map ( |x| x. db_host . clone ( ) ) )
226- . expect (
226+ . or_else ( || dotenv. db_url . clone ( ) )
227+ . or_else ( || default_config. map ( |x| x. db_url . clone ( ) ) . flatten ( ) ) ;
228+
229+ let db_host_chain = || {
230+ CLI_ARGS
231+ . db_host
232+ . clone ( )
233+ . or_else ( || dotenv. db_host . clone ( ) )
234+ . or_else ( || default_config. map ( |x| x. db_host . clone ( ) ) )
235+ } ;
236+
237+ let db_host = match ( db_url. is_some ( ) , db_host_chain ( ) ) {
238+ ( true , Some ( v) ) => v,
239+ ( true , None ) => String :: new ( ) ,
240+ ( false , Some ( v) ) => v,
241+ ( false , None ) => panic ! (
227242 r"
228- Failed to fetch DB host.
229- Please provide it at least through a CLI arg or an environment variable or through
230- file based configuration
231- " ,
232- ) ;
233-
234- let db_port = & CLI_ARGS
235- . db_port
236- . or ( dotenv. db_port )
237- . or_else ( || default_config. map ( |x| x. db_port ) )
238- . expect (
243+ Failed to fetch DB host.
244+ Please provide it at least through a CLI arg or an environment variable or through
245+ file based configuration, or provide a custom DB_URL
246+ "
247+ ) ,
248+ } ;
249+
250+ let db_port_chain = || {
251+ CLI_ARGS
252+ . db_port
253+ . or ( dotenv. db_port )
254+ . or_else ( || default_config. map ( |x| x. db_port ) )
255+ } ;
256+
257+ let db_port = match ( db_url. is_some ( ) , db_port_chain ( ) ) {
258+ ( true , Some ( v) ) => v,
259+ ( true , None ) => 0 ,
260+ ( false , Some ( v) ) => v,
261+ ( false , None ) => panic ! (
239262 r"
240- Failed to fetch DB port.
241- Please provide it at least through a CLI arg or an environment variable or through
242- file based configuration
243- " ,
244- ) ;
245-
246- let db_user = & CLI_ARGS
247- . db_user
248- . clone ( )
249- . or_else ( || dotenv. db_user . clone ( ) )
250- . or_else ( || default_config. map ( |x| x. db_user . clone ( ) ) )
251- . expect (
263+ Failed to fetch DB port.
264+ Please provide it at least through a CLI arg or an environment variable or through
265+ file based configuration, or provide a custom DB_URL
266+ "
267+ ) ,
268+ } ;
269+
270+ let db_user_chain = || {
271+ CLI_ARGS
272+ . db_user
273+ . clone ( )
274+ . or_else ( || dotenv. db_user . clone ( ) )
275+ . or_else ( || default_config. map ( |x| x. db_user . clone ( ) ) )
276+ } ;
277+
278+ let db_user = match ( db_url. is_some ( ) , db_user_chain ( ) ) {
279+ ( true , Some ( v) ) => v,
280+ ( true , None ) => String :: new ( ) ,
281+ ( false , Some ( v) ) => v,
282+ ( false , None ) => panic ! (
252283 r"
253- Failed to fetch DB user.
254- Please provide it at least through a CLI arg or an environment variable or through
255- file based configuration
256- " ,
257- ) ;
284+ Failed to fetch DB user.
285+ Please provide it at least through a CLI arg or an environment variable or through
286+ file based configuration, or provide a custom DB_URL
287+ "
288+ ) ,
289+ } ;
258290
259291 let db_pass = & CLI_ARGS
260292 . db_pass
@@ -286,11 +318,12 @@ impl Config {
286318
287319 DbConnectionConfig {
288320 db_type : db_type. to_owned ( ) ,
289- db_host : db_host . to_owned ( ) ,
290- db_port : db_port . to_owned ( ) ,
291- db_user : db_user . to_owned ( ) ,
321+ db_host,
322+ db_port,
323+ db_user,
292324 db_pass : db_pass. to_owned ( ) ,
293325 db_name : db_name. to_owned ( ) ,
326+ db_url : db_url. to_owned ( ) ,
294327 pg_search_path : pg_search_path. to_owned ( ) ,
295328 pool_size,
296329 connection_timeout,
@@ -332,6 +365,11 @@ impl Config {
332365 /// This is to follow the spec of connection string for MySQL
333366 /// https://dev.mysql.com/doc/connector-j/8.1/en/connector-j-reference-jdbc-url-format.html
334367 pub fn get_mysql_cred_str ( & self , conn : & DbConnectionConfig ) -> String {
368+ // If custom DB_URL is provided, use it directly
369+ if let Some ( db_url) = & conn. db_url {
370+ return db_url. to_owned ( ) ;
371+ }
372+
335373 format ! (
336374 "mysql://{user}:{pass}@{host}:{port}/{db_name}" ,
337375 user = & conn. db_user,
@@ -344,6 +382,11 @@ impl Config {
344382 }
345383
346384 pub fn get_postgres_cred ( & self , conn : & DbConnectionConfig ) -> String {
385+ // If custom DB_URL is provided, use it directly
386+ if let Some ( db_url) = & conn. db_url {
387+ return db_url. to_owned ( ) ;
388+ }
389+
347390 format ! (
348391 "postgresql://{user}:{pass}@{host}:{port}/{db_name}" ,
349392 user = & conn. db_user,
0 commit comments