|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Data; |
| 7 | +using System.Data.SqlClient; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using NuGet.Jobs; |
| 11 | +using NuGet.Jobs.Configuration; |
| 12 | +using NuGet.Jobs.Validation; |
| 13 | +using NuGet.Services.Validation; |
| 14 | + |
| 15 | +namespace NuGet.Services.Revalidate |
| 16 | +{ |
| 17 | + public class PackageRevalidationInserter : IPackageRevalidationInserter |
| 18 | + { |
| 19 | + private const string TableName = "[dbo].[PackageRevalidations]"; |
| 20 | + |
| 21 | + private const string PackageIdColumn = "PackageId"; |
| 22 | + private const string PackageNormalizedVersionColumn = "PackageNormalizedVersion"; |
| 23 | + private const string EnqueuedColumn = "Enqueued"; |
| 24 | + private const string ValidationTrackingIdColumn = "ValidationTrackingId"; |
| 25 | + private const string CompletedColumn = "Completed"; |
| 26 | + |
| 27 | + private readonly ISqlConnectionFactory<ValidationDbConfiguration> _connectionFactory; |
| 28 | + private readonly ILogger<PackageRevalidationInserter> _logger; |
| 29 | + |
| 30 | + public PackageRevalidationInserter( |
| 31 | + ISqlConnectionFactory<ValidationDbConfiguration> connectionFactory, |
| 32 | + ILogger<PackageRevalidationInserter> logger) |
| 33 | + { |
| 34 | + _connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory)); |
| 35 | + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 36 | + } |
| 37 | + |
| 38 | + public async Task AddPackageRevalidationsAsync(IReadOnlyList<PackageRevalidation> revalidations) |
| 39 | + { |
| 40 | + _logger.LogDebug("Persisting package revalidations to database..."); |
| 41 | + |
| 42 | + var table = PrepareTable(revalidations); |
| 43 | + |
| 44 | + using (var connection = await _connectionFactory.OpenAsync()) |
| 45 | + { |
| 46 | + var bulkCopy = new SqlBulkCopy( |
| 47 | + connection, |
| 48 | + SqlBulkCopyOptions.TableLock | SqlBulkCopyOptions.FireTriggers | SqlBulkCopyOptions.UseInternalTransaction, |
| 49 | + externalTransaction: null); |
| 50 | + |
| 51 | + foreach (DataColumn column in table.Columns) |
| 52 | + { |
| 53 | + bulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName); |
| 54 | + } |
| 55 | + |
| 56 | + bulkCopy.DestinationTableName = TableName; |
| 57 | + bulkCopy.WriteToServer(table); |
| 58 | + } |
| 59 | + |
| 60 | + _logger.LogDebug("Finished persisting package revalidations to database..."); |
| 61 | + } |
| 62 | + |
| 63 | + private DataTable PrepareTable(IReadOnlyList<PackageRevalidation> revalidations) |
| 64 | + { |
| 65 | + // Prepare the table. |
| 66 | + var table = new DataTable(); |
| 67 | + |
| 68 | + table.Columns.Add(PackageIdColumn, typeof(string)); |
| 69 | + table.Columns.Add(PackageNormalizedVersionColumn, typeof(string)); |
| 70 | + table.Columns.Add(CompletedColumn, typeof(bool)); |
| 71 | + |
| 72 | + var enqueued = table.Columns.Add(EnqueuedColumn, typeof(DateTime)); |
| 73 | + var trackingId = table.Columns.Add(ValidationTrackingIdColumn, typeof(Guid)); |
| 74 | + |
| 75 | + enqueued.AllowDBNull = true; |
| 76 | + trackingId.AllowDBNull = true; |
| 77 | + |
| 78 | + // Populate the table. |
| 79 | + foreach (var revalidation in revalidations) |
| 80 | + { |
| 81 | + var row = table.NewRow(); |
| 82 | + |
| 83 | + row[PackageIdColumn] = revalidation.PackageId; |
| 84 | + row[PackageNormalizedVersionColumn] = revalidation.PackageNormalizedVersion; |
| 85 | + row[EnqueuedColumn] = ((object)revalidation.Enqueued) ?? DBNull.Value; |
| 86 | + row[ValidationTrackingIdColumn] = ((object)revalidation.ValidationTrackingId) ?? DBNull.Value; |
| 87 | + row[CompletedColumn] = revalidation.Completed; |
| 88 | + |
| 89 | + table.Rows.Add(row); |
| 90 | + } |
| 91 | + |
| 92 | + return table; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments