@@ -549,23 +549,35 @@ def discover(self, key: Optional[str] = None) -> Dict[str, ConfigValue]:
549549 for env_name , env_value in os .environ .items ():
550550 # Check for connection-specific pattern: SNOWFLAKE_CONNECTIONS_<NAME>_<KEY>
551551 if env_name .startswith ("SNOWFLAKE_CONNECTIONS_" ):
552- # Extract connection name and config key
552+ # Extract remainder after the prefix
553553 remainder = env_name [len ("SNOWFLAKE_CONNECTIONS_" ) :]
554- parts = remainder .split ("_" , 1 )
555- if len (parts ) == 2 :
556- conn_name_upper , config_key_upper = parts
557- conn_name = conn_name_upper .lower ()
558- config_key = config_key_upper .lower ()
559-
560- if config_key in _ENV_CONFIG_KEYS :
561- full_key = f"connections.{ conn_name } .{ config_key } "
562- if key is None or full_key == key :
563- values [full_key ] = ConfigValue (
564- key = full_key ,
565- value = env_value ,
566- source_name = self .source_name ,
567- raw_value = f"{ env_name } ={ env_value } " ,
568- )
554+
555+ # Find the longest matching key suffix from known config keys to
556+ # correctly handle underscores both in connection names and keys
557+ match : tuple [str , str ] | None = None
558+ for candidate in sorted (_ENV_CONFIG_KEYS , key = len , reverse = True ):
559+ key_suffix = "_" + candidate .upper ()
560+ if remainder .endswith (key_suffix ):
561+ conn_name_upper = remainder [: - len (key_suffix )]
562+ if conn_name_upper : # ensure non-empty connection name
563+ match = (conn_name_upper , candidate )
564+ break
565+
566+ if not match :
567+ # Unknown/unsupported key suffix; ignore
568+ continue
569+
570+ conn_name_upper , config_key = match
571+ conn_name = conn_name_upper .lower ()
572+
573+ full_key = f"connections.{ conn_name } .{ config_key } "
574+ if key is None or full_key == key :
575+ values [full_key ] = ConfigValue (
576+ key = full_key ,
577+ value = env_value ,
578+ source_name = self .source_name ,
579+ raw_value = f"{ env_name } ={ env_value } " ,
580+ )
569581
570582 return values
571583
0 commit comments