Table of Contents

Class DatabaseAdmin

Namespace
CL.MySQL2.Services
Assembly
CL.MySQL2.dll

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

connectionManager ConnectionManager
tableSync TableSyncService
logger ILogger

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

name string
charset string
collation string
ifNotExists bool
connectionId string
ct CancellationToken

Returns

Task<Result<bool>>

true when the database was created, false when it already existed and ifNotExists is 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

ifNotExists bool
connectionId string
ct CancellationToken

Returns

Task<Result<bool>>

true when the table was created, false when it already existed and ifNotExists is 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

name string
connectionId string
ct CancellationToken

Returns

Task<Result<bool>>

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

name string
ifExists bool
connectionId string
ct CancellationToken

Returns

Task<Result<bool>>

true when a database was dropped.

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

ifExists bool
connectionId string
ct CancellationToken

Returns

Task<Result<bool>>

true when a table was dropped.

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

tableName string
connectionId string
ct CancellationToken

Returns

Task<Result<bool>>

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

connectionId string
ct CancellationToken

Returns

Task<Result<bool>>

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

connectionId string
ct CancellationToken

Returns

Task<Result<bool>>

Type Parameters

T