Skip to content

Commit

Permalink
Removes SqlManagementObject references (#1130)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankowitz authored Jan 23, 2025
1 parent 2168b5b commit 9e40508
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 29 deletions.
5 changes: 1 addition & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project>

<ItemGroup>
<PackageVersion Include="Azure.Core" Version="1.44.1" />
<PackageVersion Include="Azure.Identity" Version="1.13.2" />
Expand Down Expand Up @@ -70,7 +69,6 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="Microsoft.SqlServer.DacFx" Version="162.5.57" />
<PackageVersion Include="Microsoft.SqlServer.SqlManagementObjects" Version="172.52.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Newtonsoft.Json.Schema" Version="4.0.1" />
<PackageVersion Include="NSubstitute" Version="5.3.0" />
Expand All @@ -92,5 +90,4 @@
<PackageVersion Include="xunit.extensibility.core" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.1" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
using Microsoft.Extensions.Options;
using Microsoft.Health.SqlServer.Configs;
using Microsoft.Health.SqlServer.Features.Client;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

namespace Microsoft.Health.SqlServer.Features.Schema.Manager;

Expand Down Expand Up @@ -44,8 +42,6 @@ public async Task ExecuteScriptAndCompleteSchemaVersionAsync(string script, int
using SqlConnectionWrapper sqlConnectionWrapper = await _sqlConnectionWrapperFactory.ObtainSqlConnectionWrapperAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
using SqlCommandWrapper sqlCommandWrapper = sqlConnectionWrapper.CreateRetrySqlCommand();

var serverConnection = GetServerConnectionWithTimeout(sqlCommandWrapper.Connection);

try
{
// FullSchemaSnapshot script(x.sql) inserts 'started' status into the SchemaVersion table itself.
Expand All @@ -54,17 +50,16 @@ public async Task ExecuteScriptAndCompleteSchemaVersionAsync(string script, int
await UpsertSchemaVersionAsync(sqlCommandWrapper.Connection, version, SchemaVersionStatus.started.ToString(), cancellationToken).ConfigureAwait(false);
}

var server = new Server(serverConnection);
var watch = Stopwatch.StartNew();
_logger.LogInformation("Script execution started at {UtcTime}", DateTime.UtcNow);

server.ConnectionContext.ExecuteNonQuery(script);
await ExecuteWithGoSupport(script, sqlCommandWrapper, cancellationToken).ConfigureAwait(false);

watch.Stop();
_logger.LogInformation("Script execution time is {ElapsedTime}", watch.Elapsed);
await UpsertSchemaVersionAsync(sqlCommandWrapper.Connection, version, SchemaVersionStatus.completed.ToString(), cancellationToken).ConfigureAwait(false);
}
catch (Exception e) when (e is SqlException || e is ExecutionFailureException)
catch (Exception e) when (e is SqlException)
{
await UpsertSchemaVersionAsync(sqlCommandWrapper.Connection, version, SchemaVersionStatus.failed.ToString(), cancellationToken).ConfigureAwait(false);
throw;
Expand Down Expand Up @@ -124,8 +119,19 @@ public async Task ExecuteScriptAsync(string script, CancellationToken cancellati
using SqlConnectionWrapper sqlConnectionWrapper = await _sqlConnectionWrapperFactory.ObtainSqlConnectionWrapperAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
using SqlCommandWrapper sqlCommandWrapper = sqlConnectionWrapper.CreateRetrySqlCommand();

var server = new Server(GetServerConnectionWithTimeout(sqlCommandWrapper.Connection));
server.ConnectionContext.ExecuteNonQuery(script);
await ExecuteWithGoSupport(script, sqlCommandWrapper, cancellationToken).ConfigureAwait(false);
}

private async Task ExecuteWithGoSupport(string script, SqlCommandWrapper sqlCommandWrapper, CancellationToken cancellationToken)
{
sqlCommandWrapper.CommandTimeout = (int)_sqlServerDataStoreConfiguration.StatementTimeout.TotalSeconds;
_logger.LogInformation("SqlCommandWrapper timeout sets to {StatementTimeout} seconds", sqlCommandWrapper.CommandTimeout);

foreach (string statement in script.Split(["\nGO"], StringSplitOptions.RemoveEmptyEntries))
{
sqlCommandWrapper.CommandText = statement;
await sqlCommandWrapper.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
}
}

/// <inheritdoc />
Expand Down Expand Up @@ -157,12 +163,4 @@ public async Task<bool> InstanceSchemaRecordExistsAsync(CancellationToken cancel

return (int)await sqlCommandWrapper.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false) != 0;
}

private ServerConnection GetServerConnectionWithTimeout(SqlConnection sqlConnection)
{
var serverConnection = new ServerConnection(sqlConnection);
serverConnection.StatementTimeout = (int)_sqlServerDataStoreConfiguration.StatementTimeout.TotalSeconds;
_logger.LogInformation("ServerConnection timeout sets to {TimeoutSeconds} seconds", serverConnection.StatementTimeout);
return serverConnection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Microsoft.Health.SqlServer.Features.Schema.Extensions;
using Microsoft.Health.SqlServer.Features.Schema.Manager.Exceptions;
using Microsoft.Health.SqlServer.Features.Schema.Manager.Model;
using Microsoft.SqlServer.Management.Common;
using Polly;

namespace Microsoft.Health.SqlServer.Features.Schema.Manager;
Expand Down Expand Up @@ -175,7 +174,7 @@ await Policy.Handle<SchemaManagerException>()
}
catch (Exception ex)
{
if (ex is SqlException || ex is ExecutionFailureException)
if (ex is SqlException)
{
_logger.LogError(ex, "Script execution has failed.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Health.SqlServer.Features.Client;
using Microsoft.Health.SqlServer.Features.Schema.Manager;
using Microsoft.SqlServer.Management.Common;

namespace Microsoft.Health.SqlServer.Features.Schema;

Expand Down Expand Up @@ -63,7 +62,7 @@ public async Task ApplySchemaAsync(int version, bool applyFullSchemaSnapshot, Ca

_logger.LogInformation("Completed applying schema {Version}", version);
}
catch (Exception e) when (e is SqlException || e is ExecutionFailureException)
catch (Exception e) when (e is SqlException)
{
_logger.LogError(e, "Failed applying schema {Version}", version);
await FailSchemaVersionAsync(version, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Options" />
<PackageReference Include="Microsoft.SqlServer.SqlManagementObjects" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Polly" />
<PackageReference Include="Polly.Contrib.WaitAndRetry" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using Microsoft.Health.SqlServer.Features.Schema;
using Microsoft.Health.SqlServer.Features.Schema.Manager;
using Microsoft.Health.SqlServer.Features.Storage;
using Microsoft.SqlServer.Management.Common;
using NSubstitute;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -96,7 +95,7 @@ public async Task ApplyFullSchemaAndDiffScript_OnPreviouslyFailedAttempt_Succeed
await _schemaDataStore.ExecuteScriptAsync("Insert into SchemaVersion values (2, 'started')", CancellationToken.None);

// attempt 1 : To apply schemaVersion-2 fails
await Assert.ThrowsAsync<ExecutionFailureException>(() => _runner.ApplySchemaAsync(2, applyFullSchemaSnapshot: true, CancellationToken.None));
await Assert.ThrowsAsync<SqlException>(() => _runner.ApplySchemaAsync(2, applyFullSchemaSnapshot: true, CancellationToken.None));

// attempt 2 : To apply schemaVersion-2 passes
await _runner.ApplySchemaAsync(2, applyFullSchemaSnapshot: true, CancellationToken.None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<PackageReference Include="Microsoft.Data.SqlClient" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Microsoft.SqlServer.SqlManagementObjects" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" PrivateAssets="All" />
Expand Down

0 comments on commit 9e40508

Please sign in to comment.