Class DatabaseAdmin
Database- and table-level administration: create / drop / check databases, and create / drop / truncate / check entity tables. Meant for first-run setup, demos, test fixtures and resets — day-to-day schema changes belong to TableSyncService.
Database operations run on a server-level connection (no default database), so the connection's user needs the matching server privileges. Every method returns a CodeLogic.Core.Results.Result<T> instead of throwing, except for invalid arguments.
public sealed class DatabaseAdmin
- Inheritance
-
DatabaseAdmin
- Inherited Members
Constructors
DatabaseAdmin(ConnectionManager, TableSyncService, ILogger?)
public DatabaseAdmin(ConnectionManager connectionManager, TableSyncService tableSync, ILogger? logger = null)
Parameters
connectionManagerConnectionManagertableSyncTableSyncServiceloggerILogger
Methods
CreateDatabaseAsync(string, string?, string?, bool, string, CancellationToken)
Creates database name. charset and
collation default to the connection's configured
CharacterSet / Collation.
public Task<Result<bool>> CreateDatabaseAsync(string name, string? charset = null, string? collation = null, bool ifNotExists = true, string connectionId = "Default", CancellationToken ct = default)
Parameters
namestringcharsetstringcollationstringifNotExistsboolconnectionIdstringctCancellationToken
Returns
- Task<Result<bool>>
true when the database was created, false when it already existed and
ifNotExistsis set.
CreateTableAsync<T>(bool, string, CancellationToken)
Creates the table for T from its attributes, exactly as a first
SyncTableAsync would, and records it in __schema_state. Unlike a sync it
never alters an existing table.
public Task<Result<bool>> CreateTableAsync<T>(bool ifNotExists = true, string connectionId = "Default", CancellationToken ct = default) where T : class
Parameters
ifNotExistsboolconnectionIdstringctCancellationToken
Returns
- Task<Result<bool>>
true when the table was created, false when it already existed and
ifNotExistsis set.
Type Parameters
T
DatabaseExistsAsync(string, string, CancellationToken)
Returns whether database name exists on the server.
public Task<Result<bool>> DatabaseExistsAsync(string name, string connectionId = "Default", CancellationToken ct = default)
Parameters
namestringconnectionIdstringctCancellationToken
Returns
DropDatabaseAsync(string, bool, string, CancellationToken)
Drops database name, including the one this connection is configured
for. Idle pooled connections to it are cleared first.
public Task<Result<bool>> DropDatabaseAsync(string name, bool ifExists = true, string connectionId = "Default", CancellationToken ct = default)
Parameters
namestringifExistsboolconnectionIdstringctCancellationToken
Returns
DropTableAsync<T>(bool, string, CancellationToken)
Drops the table for T and forgets its __schema_state row,
so the next sync recreates it. Fails if another table's foreign key references it.
public Task<Result<bool>> DropTableAsync<T>(bool ifExists = true, string connectionId = "Default", CancellationToken ct = default) where T : class
Parameters
ifExistsboolconnectionIdstringctCancellationToken
Returns
Type Parameters
T
TableExistsAsync(string, string, CancellationToken)
Returns whether table tableName exists in the connection's database.
public Task<Result<bool>> TableExistsAsync(string tableName, string connectionId = "Default", CancellationToken ct = default)
Parameters
tableNamestringconnectionIdstringctCancellationToken
Returns
TableExistsAsync<T>(string, CancellationToken)
Returns whether the table for T exists in the connection's database.
public Task<Result<bool>> TableExistsAsync<T>(string connectionId = "Default", CancellationToken ct = default) where T : class
Parameters
connectionIdstringctCancellationToken
Returns
Type Parameters
T
TruncateTableAsync<T>(string, CancellationToken)
Removes every row from the table for T and resets its
AUTO_INCREMENT counter (TRUNCATE TABLE). MySQL refuses to truncate a table that
another table's foreign key references.
public Task<Result<bool>> TruncateTableAsync<T>(string connectionId = "Default", CancellationToken ct = default) where T : class
Parameters
connectionIdstringctCancellationToken
Returns
Type Parameters
T