Path: redb/redb.Export/Services/RedbSchemaProbe.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/RedbSchemaProbe.cs
----
using System.Data;
using System.Data.Common;
using redb.Core.Query;
using redb.Export.Providers;
namespace redb.Export.Services;
/// Whether a database holds the redb schema at all: its core table _schemes, as a service start asks.
internal static class RedbSchemaProbe
{
public static async Task ExistsAsync(IDataProvider provider, CancellationToken ct)
{
var (dialect, table, schema) = provider.Name switch
{
"postgres" => ((ISqlDialect)new global::redb.Postgres.Sql.PostgreSqlDialect(), "", ""),
"mssql" => (new global::redb.MSSql.Sql.MsSqlDialect(), "@p0", "@p1"),
"sqlite" => (new global::redb.SQLite.Sql.SqliteDialect(), "$1", "$2"),
_ => throw new ArgumentException($"Unknown provider: {provider.Name}", nameof(provider)),
};
await using var command = provider.Connection.CreateCommand();
command.CommandText = dialect.Maintenance_SelectTableExists();
command.Parameters.Add(Parameter(command, table, "_schemes"));
command.Parameters.Add(Parameter(command, schema, null));
return Convert.ToInt64(await command.ExecuteScalarAsync(ct)) > 0;
}
/// A parameter by the name the dialect's statement uses; an empty name is a positional one (PostgreSQL).
private static DbParameter Parameter(DbCommand command, string name, string? value)
{
var parameter = command.CreateParameter();
parameter.ParameterName = name;
parameter.DbType = DbType.String;
parameter.Value = (object?)value ?? DBNull.Value;
return parameter;
}
}