Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/CoreTests/Descriptors/DatabaseDescriptorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,52 @@ public void derive_uri()
descriptor.DatabaseUri().ShouldBe(new Uri("sqlserver://server1/db1/schema1"));
}

[Fact]
public void derive_uri_with_unix_socket_path()
{
var descriptor = new DatabaseDescriptor(this)
{
Engine = "postgresql",
ServerName = "/cloudsql/platform-dev:europe-west4:shared-db",
DatabaseName = "sandbox",
SchemaOrNamespace = "public"
};

// Forward slashes and colons in the server name should be replaced with underscores
var uri = descriptor.DatabaseUri();
uri.ShouldBe(new Uri("postgresql://_cloudsql_platform-dev_europe-west4_shared-db/sandbox/public"));
}

[Fact]
public void derive_uri_with_multi_host_pipeline()
{
var descriptor = new DatabaseDescriptor(this)
{
Engine = "postgresql",
ServerName = "host1,host2,host3",
DatabaseName = "mydb"
};

// Should use only the first host
var uri = descriptor.DatabaseUri();
uri.ShouldBe(new Uri("postgresql://host1/mydb"));
}

[Fact]
public void derive_uri_with_multi_host_pipeline_and_schema()
{
var descriptor = new DatabaseDescriptor(this)
{
Engine = "postgresql",
ServerName = "primary.example.com,replica.example.com",
DatabaseName = "mydb",
SchemaOrNamespace = "myschema"
};

var uri = descriptor.DatabaseUri();
uri.ShouldBe(new Uri("postgresql://primary.example.com/mydb/myschema"));
}

[Fact]
public void database_descriptor_is_serializable()
{
Expand All @@ -31,7 +77,7 @@ public void database_descriptor_is_serializable()
ServerName = "server1",
DatabaseName = "db1"
};

descriptor.ShouldBeSerializable();
}
}
12 changes: 9 additions & 3 deletions src/JasperFx/Descriptors/DatabaseDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,22 @@ public DatabaseDescriptor(object subject, Uri subjectUri) : base(subject)
/// </summary>
public string SchemaOrNamespace { get; set; } = string.Empty;

// TODO -- get a unit test on this
public Uri DatabaseUri()
{
var serverName = ServerName.Contains(',') ? ServerName.Split(',')[0] : ServerName;

// Sanitize the server name for use as a URI hostname. Unix socket paths
// (e.g. /cloudsql/platform-dev:europe-west4:shared-db) contain characters
// that are invalid in URI hostnames.
serverName = serverName.Replace('/', '_').Replace(':', '_');

var parts = new List<string>
{
ServerName.Contains(',') ? ServerName.Split(',')[0] : ServerName,
serverName,
DatabaseName,
SchemaOrNamespace
};

return new Uri($"{Engine.ToLowerInvariant()}://{parts.Where(x => x.IsNotEmpty()).Select(Uri.EscapeDataString).Join("/")}");
}

Expand Down
Loading