da669bfc9b
The store was extracted from MxAccessGateway, whose deployed gateway-auth.db is at schema_version=2. The library capped at 1 and threw on a newer on-disk version -> gateway would fail to boot. Final schema is byte-identical since v1; stamp 2 so existing deployed DBs interoperate (no key re-issuance). +2 tests.
134 lines
5.5 KiB
C#
134 lines
5.5 KiB
C#
using System.Globalization;
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
namespace ZB.MOM.WW.Auth.ApiKeys.Sqlite;
|
|
|
|
/// <summary>Thrown when the auth store cannot be migrated to the supported schema.</summary>
|
|
public sealed class AuthStoreMigrationException(string message) : InvalidOperationException(message);
|
|
|
|
/// <summary>
|
|
/// Creates the API-key store schema and records the applied version. Idempotent: it
|
|
/// is safe to run repeatedly. Refuses to run against a database whose on-disk version
|
|
/// is newer than this build supports.
|
|
/// </summary>
|
|
public sealed class SqliteAuthStoreMigrator(AuthSqliteConnectionFactory connectionFactory)
|
|
{
|
|
/// <summary>Applies the schema migration to the auth store.</summary>
|
|
/// <param name="cancellationToken">Cancellation token.</param>
|
|
/// <exception cref="AuthStoreMigrationException">
|
|
/// The on-disk schema version is newer than <see cref="SqliteAuthSchema.CurrentVersion"/>.
|
|
/// </exception>
|
|
public async Task MigrateAsync(CancellationToken cancellationToken)
|
|
{
|
|
await using SqliteConnection connection =
|
|
await connectionFactory.OpenConnectionAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
await using SqliteTransaction transaction =
|
|
(SqliteTransaction)await connection.BeginTransactionAsync(System.Data.IsolationLevel.Serializable, cancellationToken).ConfigureAwait(false);
|
|
|
|
int existingVersion =
|
|
await ReadExistingSchemaVersionAsync(connection, transaction, cancellationToken).ConfigureAwait(false);
|
|
|
|
if (existingVersion > SqliteAuthSchema.CurrentVersion)
|
|
{
|
|
throw new AuthStoreMigrationException(
|
|
$"Auth database schema version {existingVersion} is newer than supported version {SqliteAuthSchema.CurrentVersion}.");
|
|
}
|
|
|
|
await ApplySchemaAsync(connection, transaction, cancellationToken).ConfigureAwait(false);
|
|
await WriteSchemaVersionAsync(connection, transaction, cancellationToken).ConfigureAwait(false);
|
|
|
|
await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
|
|
}
|
|
|
|
private static async Task<int> ReadExistingSchemaVersionAsync(
|
|
SqliteConnection connection,
|
|
SqliteTransaction transaction,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using SqliteCommand tableExistsCommand = connection.CreateCommand();
|
|
tableExistsCommand.Transaction = transaction;
|
|
tableExistsCommand.CommandText = """
|
|
SELECT COUNT(*)
|
|
FROM sqlite_master
|
|
WHERE type = 'table' AND name = $table_name;
|
|
""";
|
|
tableExistsCommand.Parameters.AddWithValue("$table_name", SqliteAuthSchema.SchemaVersionTable);
|
|
|
|
long tableCount =
|
|
(long)(await tableExistsCommand.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false) ?? 0L);
|
|
|
|
if (tableCount == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
await using SqliteCommand versionCommand = connection.CreateCommand();
|
|
versionCommand.Transaction = transaction;
|
|
versionCommand.CommandText = """
|
|
SELECT version
|
|
FROM schema_version
|
|
WHERE id = 1;
|
|
""";
|
|
|
|
object? version = await versionCommand.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
return version is null || version == DBNull.Value
|
|
? 0
|
|
: Convert.ToInt32(version, CultureInfo.InvariantCulture);
|
|
}
|
|
|
|
// Single-shot create of the final schema (all DDL is CREATE ... IF NOT EXISTS, so it is
|
|
// idempotent against an already-provisioned database). The applied version is stamped
|
|
// separately by WriteSchemaVersionAsync.
|
|
private static async Task ApplySchemaAsync(
|
|
SqliteConnection connection,
|
|
SqliteTransaction transaction,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await ExecuteNonQueryAsync(
|
|
connection,
|
|
transaction,
|
|
string.Join(
|
|
"\n",
|
|
SqliteAuthSchema.CreateSchemaVersionTable,
|
|
SqliteAuthSchema.CreateApiKeysTable,
|
|
SqliteAuthSchema.CreateApiKeyAuditTable,
|
|
SqliteAuthSchema.CreateIndexes),
|
|
cancellationToken).ConfigureAwait(false);
|
|
}
|
|
|
|
private static async Task WriteSchemaVersionAsync(
|
|
SqliteConnection connection,
|
|
SqliteTransaction transaction,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using SqliteCommand versionCommand = connection.CreateCommand();
|
|
versionCommand.Transaction = transaction;
|
|
versionCommand.CommandText = """
|
|
INSERT INTO schema_version (id, version, applied_utc)
|
|
VALUES (1, $version, $applied_utc)
|
|
ON CONFLICT(id) DO UPDATE SET
|
|
version = excluded.version,
|
|
applied_utc = excluded.applied_utc;
|
|
""";
|
|
versionCommand.Parameters.AddWithValue("$version", SqliteAuthSchema.CurrentVersion);
|
|
versionCommand.Parameters.AddWithValue("$applied_utc", DateTimeOffset.UtcNow.ToString("O"));
|
|
|
|
await versionCommand.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
|
|
}
|
|
|
|
private static async Task ExecuteNonQueryAsync(
|
|
SqliteConnection connection,
|
|
SqliteTransaction transaction,
|
|
string commandText,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using SqliteCommand command = connection.CreateCommand();
|
|
command.Transaction = transaction;
|
|
command.CommandText = commandText;
|
|
|
|
await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
|
|
}
|
|
}
|