49
49
from databricks .labs .dqx .telemetry import telemetry_logger
50
50
from databricks .labs .dqx .utils import TABLE_PATTERN
51
51
from databricks .labs .dqx .checks_serializer import FILE_SERIALIZERS
52
- from urllib .parse import urlparse , unquote
53
52
54
53
55
54
logger = logging .getLogger (__name__ )
@@ -441,74 +440,6 @@ def save(self, checks: list[dict], config: InstallationChecksStorageConfig) -> N
441
440
handler , config = self ._get_storage_handler_and_config (config )
442
441
return handler .save (checks , config )
443
442
444
- def _parse_lakebase_config (
445
- self , config : InstallationChecksStorageConfig
446
- ) -> tuple [str | None , str | None , str | None , str | None ]:
447
- """
448
- Parse PostgreSQL connection string to extract connection parameters.
449
-
450
- Expected format: postgresql://user:password@instance_name:port/database?params
451
- Examples:
452
- User: postgresql://[email protected] :${PGPASSWORD}@instance-1234.database.azuredatabricks.net:5432/databricks_postgres?sslmode=require
453
- Service Principal: postgresql://1234567890:${PGPASSWORD}@instance-1234.database.azuredatabricks.net:5432/databricks_postgres?sslmode=require
454
-
455
- Args:
456
- config: Installation checks storage configuration containing the location URL
457
-
458
- Returns:
459
- Tuple of (user, instance_name, port, database) - any may be None if parsing fails
460
-
461
- Raises:
462
- ValueError: If the URL format is invalid or required components are missing
463
- """
464
- if not config .location :
465
- raise ValueError ("Location field is empty or None - cannot parse Lakebase configuration" )
466
-
467
- try :
468
- parsed = urlparse (config .location )
469
- except Exception as e :
470
- raise ValueError (f"Failed to parse URL '{ config .location } ': { e } " ) from e
471
-
472
- if parsed .scheme != "postgresql" :
473
- raise ValueError (
474
- f"Invalid URL scheme '{ parsed .scheme } '. Expected 'postgresql' for Lakebase connections. "
475
- f"URL: { config .location } "
476
- )
477
-
478
- try :
479
- user = unquote (parsed .username ) if parsed .username else None
480
- except Exception as e :
481
- raise ValueError (f"Failed to decode username from URL: { e } " ) from e
482
-
483
- instance_name = parsed .hostname
484
- if not instance_name :
485
- raise ValueError (f"Missing hostname in URL: { config .location } " )
486
-
487
- port = None
488
- if parsed .port :
489
- try :
490
- port = str (parsed .port )
491
- except (ValueError , TypeError ) as e :
492
- raise ValueError (f"Invalid port '{ parsed .port } ' in URL: { e } " ) from e
493
-
494
- database = None
495
- if parsed .path :
496
- try :
497
- database = parsed .path .lstrip ("/" )
498
- if not database :
499
- raise ValueError ("Database name is missing" )
500
- except Exception as e :
501
- raise ValueError (f"Failed to extract database name from connection string '{ parsed .path } ': { e } " ) from e
502
-
503
- if not database :
504
- raise ValueError (f"Missing required database name in connection string: { config .location } " )
505
-
506
- logger .debug (
507
- f"Parsed Lakebase config - User: { user } , Instance name: { instance_name } , Port: { port } , Database: { database } "
508
- )
509
-
510
- return user , instance_name , port , database
511
-
512
443
def _get_storage_handler_and_config (
513
444
self , config : InstallationChecksStorageConfig
514
445
) -> tuple [ChecksStorageHandler , InstallationChecksStorageConfig ]:
@@ -524,17 +455,16 @@ def _get_storage_handler_and_config(
524
455
525
456
config .location = run_config .checks_location
526
457
527
-
528
458
if TABLE_PATTERN .match (config .location ) and not config .location .lower ().endswith (
529
459
tuple (FILE_SERIALIZERS .keys ())
530
460
):
531
461
return self .table_handler , config
532
462
533
463
if config .location .startswith ("/Volumes/" ):
534
464
return self .volume_handler , config
535
-
465
+
536
466
if config .location .startswith ("postgresql://" ):
537
- user , instance_name , port , database = self ._parse_lakebase_config (config )
467
+ user , instance_name , port , database = config ._parse_lakebase_config (config . location )
538
468
config .user = user
539
469
config .instance_name = instance_name
540
470
config .port = port
0 commit comments