Class DatabaseMigrator
Creates or upgrades the schema at start, before the server accepts requests, in write-ahead log mode so reads do not block writes; and flushes the database at stop.
Implements
Inherited Members
Namespace: Uniform.Matrix.Server.Storage
Assembly: Uniform.Matrix.Server.dll
Syntax
public sealed class DatabaseMigrator : IHostedService
Remarks
It holds the DataDirectoryLock, so no other process uses the database meanwhile; a migration lock that a process left behind when it died is therefore removed instead of waited for, as EF Core would wait forever.
Constructors
| Edit this page View SourceDatabaseMigrator(IDbContextFactory<ServerDbContext>, DataDirectoryLock, ILogger<DatabaseMigrator>)
Use contexts for the database, which directoryLock keeps to this process.
Declaration
public DatabaseMigrator(IDbContextFactory<ServerDbContext> contexts, DataDirectoryLock directoryLock, ILogger<DatabaseMigrator> logger)
Parameters
| Type | Name | Description |
|---|---|---|
| IDbContextFactory<ServerDbContext> | contexts | The database. |
| DataDirectoryLock | directoryLock | The lock of the data directory; taken here so that the host keeps it until disposed. |
| ILogger<DatabaseMigrator> | logger | Log of leftover locks and the stop. |
Methods
| Edit this page View SourceStartAsync(CancellationToken)
Remove a leftover migration lock, switch to the write-ahead log, and apply pending migrations; throw with advice when the database has tables the migration history does not know.
Declaration
public Task StartAsync(CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |
StopAsync(CancellationToken)
Write the write-ahead log into the database and close its pooled connections, so that the files are complete when the process ends; other databases of the process, e.g. of other servers in tests, keep theirs.
Declaration
public Task StopAsync(CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |