Skip to content

Commit 371a32c

Browse files
authored
Gracefully handle schema missing InitialCatalog (#772)
1 parent ce21ea0 commit 371a32c

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/Tests/Tests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,15 @@ await Verify(connection)
833833
.SchemaIncludes(DbObjects.Tables);
834834
}
835835

836+
[Test]
837+
public async Task SchemaMissingInitialCatalog()
838+
{
839+
var connection = new SqlConnection("Server=localhost");
840+
var exception = Assert.ThrowsAsync<Exception>(
841+
async () => await Verify(connection));
842+
Assert.That(exception!.Message, Does.Contain("Initial Catalog"));
843+
}
844+
836845
[Test]
837846
public async Task SchemaIncludesAndFilter()
838847
{

src/Verify.SqlServer/SchemaValidation/SqlScriptBuilder.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,19 @@ public string BuildContent(SqlConnection connection)
4848

4949
string BuildContent(Server server, SqlConnectionStringBuilder builder)
5050
{
51+
var initialCatalog = builder.InitialCatalog;
52+
if (string.IsNullOrWhiteSpace(initialCatalog))
53+
{
54+
throw new("The connection string must specify an Initial Catalog (database name) for schema verification.");
55+
}
56+
5157
server.SetDefaultInitFields(typeof(Table), "Name", "IsSystemObject");
5258
server.SetDefaultInitFields(typeof(View), "Name", "IsSystemObject");
5359
server.SetDefaultInitFields(typeof(StoredProcedure), "Name", "IsSystemObject");
5460
server.SetDefaultInitFields(typeof(UserDefinedFunction), "Name", "IsSystemObject");
5561
server.SetDefaultInitFields(typeof(Trigger), "Name", "IsSystemObject");
5662
server.SetDefaultInitFields(typeof(Synonym), "Name");
57-
var database = server.Databases[builder.InitialCatalog];
63+
var database = server.Databases[initialCatalog];
5864
database.Tables.Refresh();
5965

6066
return GetScriptingObjects(database);

0 commit comments

Comments
 (0)