Browse Source

Add detailed documentation for the Db Migrator in Single Layer Web Application template

pull/21728/head
ahmetfarukulu 2 years ago
parent
commit
9ebc7017ed
  1. 59
      docs/en/solution-templates/single-layer-web-application/db-migrator.md

59
docs/en/solution-templates/single-layer-web-application/db-migrator.md

@ -18,4 +18,61 @@ Unlike the Layered solution template, the Single Layer solution template does no
![Single Layer Solution: Db Migrator](images/single-layer-db-migrator.png) ![Single Layer Solution: Db Migrator](images/single-layer-db-migrator.png)
After the migration completes, a message will appear in the console. You can verify the success of the migration by checking the database. After the migration completes, a message will appear in the console. You can verify the success of the migration by checking the database.
## Database Migration Service
Under the `Data` folder of the project, the `BookstoreDbMigrationService` class is responsible for database migration and seed data operations. The `MigrateAsync` method is called in the `Program` class to migrate the database when the application starts with the `--migrate-database` argument.
First, it checks if the database is created and applies the pending migrations. Then, it seeds the initial data using the `SeedAsync` method.
```csharp
public async Task MigrateAsync()
{
var initialMigrationAdded = AddInitialMigrationIfNotExist();
if (initialMigrationAdded)
{
return;
}
Logger.LogInformation("Started database migrations...");
await MigrateDatabaseSchemaAsync();
await SeedDataAsync();
Logger.LogInformation($"Successfully completed host database migrations.");
var tenants = await _tenantRepository.GetListAsync(includeDetails: true);
var migratedDatabaseSchemas = new HashSet<string>();
foreach (var tenant in tenants)
{
using (_currentTenant.Change(tenant.Id))
{
if (tenant.ConnectionStrings.Any())
{
var tenantConnectionStrings = tenant.ConnectionStrings
.Select(x => x.Value)
.ToList();
if (!migratedDatabaseSchemas.IsSupersetOf(tenantConnectionStrings))
{
await MigrateDatabaseSchemaAsync(tenant);
migratedDatabaseSchemas.AddIfNotContains(tenantConnectionStrings);
}
}
await SeedDataAsync(tenant);
}
Logger.LogInformation($"Successfully completed {tenant.Name} tenant database migrations.");
}
Logger.LogInformation("Successfully completed all database migrations.");
Logger.LogInformation("You can safely end this process...");
}
```
The `BookstoreDbSchemaMigrator` class is used in the `MigrateDatabaseSchemaAsync` method for the database migration process. It is responsible for applying migrations to the database.
Loading…
Cancel
Save