Added migration tool
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
using HaDbMigration.Configuration;
|
||||
using HaDbMigration.Models;
|
||||
using HaDbMigration.Services.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace HaDbMigration.Services;
|
||||
|
||||
public sealed class ValidationService : IValidationService
|
||||
{
|
||||
private readonly ISqliteReaderService _sqliteReader;
|
||||
private readonly IPostgresWriterService _postgresWriter;
|
||||
private readonly MigrationOptions _options;
|
||||
private readonly ILogger<ValidationService> _logger;
|
||||
|
||||
public ValidationService(
|
||||
ISqliteReaderService sqliteReader,
|
||||
IPostgresWriterService postgresWriter,
|
||||
MigrationOptions options,
|
||||
ILogger<ValidationService> logger)
|
||||
{
|
||||
_sqliteReader = sqliteReader;
|
||||
_postgresWriter = postgresWriter;
|
||||
_options = options;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<TableValidationResult>> ValidateRowCountsAsync(
|
||||
IReadOnlyList<TableDescriptor> tables,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var tasks = tables.Select(table => ValidateTableAsync(table, cancellationToken));
|
||||
var results = await Task.WhenAll(tasks);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private async Task<TableValidationResult> ValidateTableAsync(
|
||||
TableDescriptor table,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var sourceCountTask = _sqliteReader.GetRowCountAsync(table.TableName, cancellationToken);
|
||||
|
||||
long targetCount;
|
||||
|
||||
if (_options.DryRun)
|
||||
{
|
||||
targetCount = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
targetCount = await _postgresWriter.GetRowCountAsync(table.TableName, cancellationToken);
|
||||
}
|
||||
|
||||
var sourceCount = await sourceCountTask;
|
||||
|
||||
var isMatch = _options.DryRun || sourceCount == targetCount;
|
||||
|
||||
_logger.LogDebug(
|
||||
"Validation for {Table}: source={Source}, target={Target}, match={Match}",
|
||||
table.TableName,
|
||||
sourceCount,
|
||||
targetCount,
|
||||
isMatch);
|
||||
|
||||
return new TableValidationResult(table.TableName, sourceCount, targetCount, isMatch);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user