Path: redb/redb.Export/Services/ImportFinishing.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Export/Services/ImportFinishing.cs
----
using System.Data;
using System.Data.Common;
using redb.Core.Query;
using redb.Export.Providers;
namespace redb.Export.Services;
///
/// What an import does around its load, outside the load's transaction: which type rows the target already holds,
/// and after the load the scheme metadata cache and the note about time values stored as text by an older version.
///
internal static class ImportFinishing
{
/// _types ids of TimeOnly and TimeSpan, the same on every provider (RedbTypeIds).
private const long TimeOnlyTypeId = -9223372036854775685;
private const long TimeSpanTypeId = -9223372036854775684;
/// The _types ids the target holds: its built-in types, kept by the import whatever the file says.
public static async Task> TargetTypeIdsAsync(IDataProvider provider, CancellationToken ct)
{
var ids = new HashSet();
await using var command = provider.Connection.CreateCommand();
command.CommandText = "SELECT _id FROM _types";
await using var reader = await command.ExecuteReaderAsync(ct);
while (await reader.ReadAsync(ct))
ids.Add(reader.GetInt64(0));
return ids;
}
///
/// Builds the scheme metadata cache of every imported scheme through the dialect's per-scheme statement, as the
/// scheme sync of a service does. Without it the first query of a Pro application rebuilt it, which is slow on a
/// large database and needs write rights.
///
public static async Task SyncMetadataCacheAsync(IDataProvider provider, CancellationToken ct)
{
var schemes = new List();
await using (var read = provider.Connection.CreateCommand())
{
read.CommandText = "SELECT _id FROM _schemes";
await using var reader = await read.ExecuteReaderAsync(ct);
while (await reader.ReadAsync(ct))
schemes.Add(reader.GetInt64(0));
}
var (dialect, parameter) = provider.Name switch
{
"postgres" => ((ISqlDialect)new global::redb.Postgres.Sql.PostgreSqlDialect(), ""),
"mssql" => (new global::redb.MSSql.Sql.MsSqlDialect(), "@p0"),
"sqlite" => (new global::redb.SQLite.Sql.SqliteDialect(), "$1"),
_ => throw new ArgumentException($"Unknown provider: {provider.Name}", nameof(provider)),
};
foreach (var scheme in schemes)
{
await using var sync = provider.Connection.CreateCommand();
sync.CommandText = dialect.Schemes_SyncMetadataCache();
sync.CommandTimeout = 0;
var value = sync.CreateParameter();
value.ParameterName = parameter;
value.DbType = DbType.Int64;
value.Value = scheme;
sync.Parameters.Add(value);
await sync.ExecuteNonQueryAsync(ct);
}
return schemes.Count;
}
///
/// PostgreSQL: the planner statistics of the loaded tables, before an application queries them. COPY leaves a
/// table without statistics until autoanalyze reaches it, and a query planned meanwhile takes every table for
/// one row: on a new database of a few hundred objects a grouping ran for minutes. The other providers keep
/// their own (SQL Server creates them on the first query, SQLite plans without them). False when nothing ran.
///
public static async Task AnalyzeAsync(IDataProvider provider, CancellationToken ct)
{
if (provider is not PostgresProvider postgres)
return false;
// Only the redb tables: the database may hold tables of others, and analyzing them is not the import's business.
await postgres.AnalyzeRedbTablesAsync(ct);
return true;
}
///
/// How many imported values of TimeSpan and TimeOnly fields are stored as text: a file of a version before they
/// were stored as ticks. The import leaves them as the file has them; the upgrade notes convert them.
///
public static async Task TimeValuesAsTextAsync(IDataProvider provider, CancellationToken ct)
{
await using var command = provider.Connection.CreateCommand();
command.CommandText =
"SELECT COUNT(*) FROM _values v JOIN _structures s ON s._id = v._id_structure " +
$"WHERE s._id_type IN ({TimeOnlyTypeId}, {TimeSpanTypeId}) AND v._String IS NOT NULL";
command.CommandTimeout = 0;
return Convert.ToInt64(await command.ExecuteScalarAsync(ct));
}
}