This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
352b32d
commit 90705f3
Showing
7 changed files
with
130 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/NuGet.Services.Revalidate/Services/IPackageRevalidationInserter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using NuGet.Services.Validation; | ||
|
||
namespace NuGet.Services.Revalidate | ||
{ | ||
public interface IPackageRevalidationInserter | ||
{ | ||
Task AddPackageRevalidationsAsync(IReadOnlyList<PackageRevalidation> revalidations); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/NuGet.Services.Revalidate/Services/PackageRevalidationInserter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using NuGet.Services.Validation; | ||
|
||
namespace NuGet.Services.Revalidate | ||
{ | ||
public class PackageRevalidationInserter : IPackageRevalidationInserter | ||
{ | ||
private const string TableName = "[dbo].[PackageRevalidations]"; | ||
|
||
private const string PackageIdColumn = "PackageId"; | ||
private const string PackageNormalizedVersionColumn = "PackageNormalizedVersion"; | ||
private const string EnqueuedColumn = "Enqueued"; | ||
private const string ValidationTrackingIdColumn = "ValidationTrackingId"; | ||
private const string CompletedColumn = "Completed"; | ||
|
||
private readonly SqlConnection _connection; | ||
private readonly ILogger<PackageRevalidationInserter> _logger; | ||
|
||
public PackageRevalidationInserter(SqlConnection connection, ILogger<PackageRevalidationInserter> logger) | ||
{ | ||
_connection = connection ?? throw new ArgumentNullException(nameof(connection)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
public async Task AddPackageRevalidationsAsync(IReadOnlyList<PackageRevalidation> revalidations) | ||
{ | ||
_logger.LogDebug("Persisting package revalidations to database..."); | ||
|
||
var table = PrepareTable(revalidations); | ||
|
||
await _connection.OpenAsync(); | ||
|
||
var bulkCopy = new SqlBulkCopy( | ||
_connection, | ||
SqlBulkCopyOptions.TableLock | SqlBulkCopyOptions.FireTriggers | SqlBulkCopyOptions.UseInternalTransaction, | ||
externalTransaction: null); | ||
|
||
foreach (DataColumn column in table.Columns) | ||
{ | ||
bulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName); | ||
} | ||
|
||
bulkCopy.DestinationTableName = TableName; | ||
bulkCopy.WriteToServer(table); | ||
|
||
_connection.Close(); | ||
|
||
_logger.LogDebug("Finished persisting package revalidations to database..."); | ||
} | ||
|
||
private DataTable PrepareTable(IReadOnlyList<PackageRevalidation> revalidations) | ||
{ | ||
// Prepare the table. | ||
var table = new DataTable(); | ||
|
||
table.Columns.Add(PackageIdColumn, typeof(string)); | ||
table.Columns.Add(PackageNormalizedVersionColumn, typeof(string)); | ||
table.Columns.Add(CompletedColumn, typeof(bool)); | ||
|
||
var enqueued = table.Columns.Add(EnqueuedColumn, typeof(DateTime)); | ||
var trackingId = table.Columns.Add(ValidationTrackingIdColumn, typeof(Guid)); | ||
|
||
enqueued.AllowDBNull = true; | ||
trackingId.AllowDBNull = true; | ||
|
||
// Populate the table. | ||
foreach (var revalidation in revalidations) | ||
{ | ||
var row = table.NewRow(); | ||
|
||
row[PackageIdColumn] = revalidation.PackageId; | ||
row[PackageNormalizedVersionColumn] = revalidation.PackageNormalizedVersion; | ||
row[EnqueuedColumn] = ((object)revalidation.Enqueued) ?? DBNull.Value; | ||
row[ValidationTrackingIdColumn] = ((object)revalidation.ValidationTrackingId) ?? DBNull.Value; | ||
row[CompletedColumn] = revalidation.Completed; | ||
|
||
table.Rows.Add(row); | ||
} | ||
|
||
return table; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters