Added migration tool
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
using System.Threading.Channels;
|
||||
using HaDbMigration.Configuration;
|
||||
using HaDbMigration.Models;
|
||||
using HaDbMigration.Services.Interfaces;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace HaDbMigration.Services;
|
||||
|
||||
public sealed class SqliteReaderService : ISqliteReaderService
|
||||
{
|
||||
private readonly MigrationOptions _options;
|
||||
private readonly ILogger<SqliteReaderService> _logger;
|
||||
private readonly string _connectionString;
|
||||
|
||||
public SqliteReaderService(MigrationOptions options, ILogger<SqliteReaderService> logger)
|
||||
{
|
||||
_options = options;
|
||||
_logger = logger;
|
||||
_connectionString = new SqliteConnectionStringBuilder
|
||||
{
|
||||
DataSource = _options.SqlitePath,
|
||||
Mode = SqliteOpenMode.ReadOnly,
|
||||
}.ToString();
|
||||
}
|
||||
|
||||
public async Task ReadTableAsync(
|
||||
TableDescriptor table,
|
||||
ChannelWriter<IReadOnlyList<object?[]>> writer,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var columnNames = string.Join(", ", table.Columns.Select(c => $"\"{c.SqliteName}\""));
|
||||
var sql = $"SELECT {columnNames} FROM \"{table.TableName}\"";
|
||||
|
||||
_logger.LogInformation("Reading table {Table} from SQLite", table.TableName);
|
||||
|
||||
await using var connection = new SqliteConnection(_connectionString);
|
||||
await connection.OpenAsync(cancellationToken);
|
||||
|
||||
await using var command = connection.CreateCommand();
|
||||
command.CommandText = sql;
|
||||
|
||||
await using var reader = await command.ExecuteReaderAsync(cancellationToken);
|
||||
|
||||
var batch = new List<object?[]>(_options.BatchSize);
|
||||
var totalRows = 0L;
|
||||
|
||||
while (await reader.ReadAsync(cancellationToken))
|
||||
{
|
||||
var row = new object?[table.Columns.Count];
|
||||
|
||||
for (var i = 0; i < table.Columns.Count; i++)
|
||||
{
|
||||
row[i] = reader.IsDBNull(i) ? null : reader.GetValue(i);
|
||||
}
|
||||
|
||||
batch.Add(row);
|
||||
|
||||
if (batch.Count >= _options.BatchSize)
|
||||
{
|
||||
totalRows += batch.Count;
|
||||
await writer.WriteAsync(batch, cancellationToken);
|
||||
batch = new List<object?[]>(_options.BatchSize);
|
||||
|
||||
_logger.LogDebug("Read {Count} rows from {Table}", totalRows, table.TableName);
|
||||
}
|
||||
}
|
||||
|
||||
if (batch.Count > 0)
|
||||
{
|
||||
totalRows += batch.Count;
|
||||
await writer.WriteAsync(batch, cancellationToken);
|
||||
}
|
||||
|
||||
writer.Complete();
|
||||
_logger.LogInformation("Finished reading {Count} rows from {Table}", totalRows, table.TableName);
|
||||
}
|
||||
|
||||
public async Task<long> GetRowCountAsync(string tableName, CancellationToken cancellationToken)
|
||||
{
|
||||
await using var connection = new SqliteConnection(_connectionString);
|
||||
await connection.OpenAsync(cancellationToken);
|
||||
|
||||
await using var command = connection.CreateCommand();
|
||||
command.CommandText = $"SELECT COUNT(*) FROM \"{tableName}\"";
|
||||
|
||||
var result = await command.ExecuteScalarAsync(cancellationToken);
|
||||
return Convert.ToInt64(result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user