validate_source_target_different

Function validate_source_target_different 

Source
pub fn validate_source_target_different(
    source_url: &str,
    target_url: &str,
) -> Result<()>
Expand description

Validate that source and target URLs are different to prevent accidental data loss

Compares two PostgreSQL connection URLs to ensure they point to different databases. This is critical for preventing data loss from operations like init --drop-existing where using the same URL for source and target would destroy the source data.

§Comparison Strategy

URLs are normalized and compared on:

  • Host (case-insensitive)
  • Port (defaulting to 5432 if not specified)
  • Database name (case-sensitive)
  • User (if present)

Query parameters (like SSL settings) are ignored as they don’t affect database identity.

§Arguments

  • source_url - Source database connection string
  • target_url - Target database connection string

§Returns

Returns Ok(()) if the URLs point to different databases.

§Errors

Returns an error if:

  • The URLs point to the same database (same host, port, database name, and user)
  • Either URL is malformed and cannot be parsed

§Examples

// Valid - different hosts
validate_source_target_different(
    "postgresql://user:pass@source.com:5432/db",
    "postgresql://user:pass@target.com:5432/db"
)?;

// Valid - different databases
validate_source_target_different(
    "postgresql://user:pass@host:5432/db1",
    "postgresql://user:pass@host:5432/db2"
)?;

// Invalid - same database
assert!(validate_source_target_different(
    "postgresql://user:pass@host:5432/db",
    "postgresql://user:pass@host:5432/db"
).is_err());