From a68c91af42c3d043f288de1e6f71373d4e9d9e24 Mon Sep 17 00:00:00 2001 From: Matthew Lieder Date: Tue, 3 May 2022 14:29:49 -0500 Subject: [PATCH] Remove Microsoft.SqlServer.SqlManagementObjects dependency (#92) --- .../DurableTask.SqlServer.csproj | 1 - src/DurableTask.SqlServer/SqlDbManager.cs | 19 +++++++++++-------- .../DurableTask.SqlServer.Tests.csproj | 1 + 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/DurableTask.SqlServer/DurableTask.SqlServer.csproj b/src/DurableTask.SqlServer/DurableTask.SqlServer.csproj index fa821af..dc0273a 100644 --- a/src/DurableTask.SqlServer/DurableTask.SqlServer.csproj +++ b/src/DurableTask.SqlServer/DurableTask.SqlServer.csproj @@ -25,7 +25,6 @@ - diff --git a/src/DurableTask.SqlServer/SqlDbManager.cs b/src/DurableTask.SqlServer/SqlDbManager.cs index bce27b4..1a57c5e 100644 --- a/src/DurableTask.SqlServer/SqlDbManager.cs +++ b/src/DurableTask.SqlServer/SqlDbManager.cs @@ -14,7 +14,6 @@ namespace DurableTask.SqlServer using System.Text.RegularExpressions; using System.Threading.Tasks; using Microsoft.Data.SqlClient; - using Microsoft.SqlServer.Management.Common; using SemVersion; class SqlDbManager @@ -219,7 +218,6 @@ async Task CreateDatabaseAsync(string databaseName, SqlConnection connecti async Task ExecuteSqlScriptAsync(string scriptName, DatabaseLock dbLock) { - // We don't actually use the lock here, but want to make sure the caller is holding it. if (dbLock == null) { throw new ArgumentNullException(nameof(dbLock)); @@ -230,17 +228,22 @@ async Task ExecuteSqlScriptAsync(string scriptName, DatabaseLock dbLock) throw new ArgumentException("This database lock has already been released!", nameof(dbLock)); } - string schemaCommands = await GetScriptTextAsync(scriptName); + string scriptText = await GetScriptTextAsync(scriptName); - // Reference: https://stackoverflow.com/questions/650098/how-to-execute-an-sql-script-file-using-c-sharp - using SqlConnection scriptRunnerConnection = this.settings.CreateConnection(); - var serverConnection = new ServerConnection(scriptRunnerConnection); + // Split script into distinct executeable commands + IEnumerable scriptCommands = Regex.Split(scriptText, @"^\s*GO\s*$", RegexOptions.Multiline | RegexOptions.IgnoreCase) + .Where(x => x.Trim().Length > 0); Stopwatch latencyStopwatch = Stopwatch.StartNew(); try { - // NOTE: Async execution is not supported by this library - serverConnection.ExecuteNonQuery(schemaCommands); + foreach (string commandText in scriptCommands) + { + using SqlCommand command = dbLock.CreateCommand(); + command.CommandText = commandText; + + await command.ExecuteNonQueryAsync(); + } } finally { diff --git a/test/DurableTask.SqlServer.Tests/DurableTask.SqlServer.Tests.csproj b/test/DurableTask.SqlServer.Tests/DurableTask.SqlServer.Tests.csproj index fd06da3..d46946a 100644 --- a/test/DurableTask.SqlServer.Tests/DurableTask.SqlServer.Tests.csproj +++ b/test/DurableTask.SqlServer.Tests/DurableTask.SqlServer.Tests.csproj @@ -11,6 +11,7 @@ +