2022-01-21 11:19:51 +02:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using NDB.Infrastructure.DatabaseMigration.Constants;
|
|
|
|
|
using NDB.Infrastructure.DatabaseMigration.DbContexts;
|
|
|
|
|
using NDB.Infrastructure.DatabaseMigration.Models;
|
|
|
|
|
using NDB.Infrastructure.DatabaseMigration.Repositories;
|
|
|
|
|
using NDB.Infrastructure.DatabaseMigration.Services;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace NDB.Infrastructure.DatabaseMigration
|
|
|
|
|
{
|
|
|
|
|
public static class DependencyInjectionExtensions
|
|
|
|
|
{
|
|
|
|
|
private const string _workspacePlaceholder = "{Workspace}";
|
|
|
|
|
|
|
|
|
|
public static void AddMigration(this IServiceCollection services,
|
|
|
|
|
DatabaseType databaseType = DatabaseType.SQLite,
|
|
|
|
|
string connectionName = "DatabaseConnection",
|
2022-01-22 00:48:17 +02:00
|
|
|
|
string workspace = "Workspace",
|
2022-01-21 11:19:51 +02:00
|
|
|
|
string scriptsDirectoryPath = "Scripts")
|
|
|
|
|
{
|
2022-01-22 00:48:17 +02:00
|
|
|
|
var serviceConfiguration = new ServiceConfiguration(databaseType, connectionName, workspace, scriptsDirectoryPath);
|
2022-01-21 11:19:51 +02:00
|
|
|
|
services.AddSingleton(serviceConfiguration);
|
|
|
|
|
services.AddDataAccess(serviceConfiguration);
|
|
|
|
|
services.AddSingleton<IMigrationService, MigrationService>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void AddDataAccess(this IServiceCollection services, ServiceConfiguration serviceConfiguration)
|
|
|
|
|
{
|
|
|
|
|
services.AddScoped<IMigrationRepository, MigrationRepository>();
|
|
|
|
|
services.AddDbContextPool<MigrationDbContext>((serviceProvider, options) =>
|
|
|
|
|
{
|
|
|
|
|
var connectionString = GetDatabaseConnectionString(serviceProvider, serviceConfiguration.ConnectionName, serviceConfiguration.Workspace);
|
|
|
|
|
|
|
|
|
|
switch (serviceConfiguration.DatabaseType)
|
|
|
|
|
{
|
|
|
|
|
case DatabaseType.SQLite:
|
|
|
|
|
options.UseSqlite(connectionString);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case DatabaseType.SQLServer:
|
|
|
|
|
options.UseSqlServer(connectionString);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new NotImplementedException($"Database type {serviceConfiguration.DatabaseType} is not implemented.");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetDatabaseConnectionString(IServiceProvider serviceProvider, string connectionName, string workspace)
|
|
|
|
|
{
|
|
|
|
|
var configuration = serviceProvider.GetService<IConfiguration>();
|
|
|
|
|
var connectionString = configuration.GetConnectionString(connectionName);
|
|
|
|
|
|
|
|
|
|
if (connectionString.Contains(_workspacePlaceholder))
|
|
|
|
|
{
|
|
|
|
|
connectionString = connectionString.Replace(_workspacePlaceholder, workspace);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return connectionString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void UseMigration(this IApplicationBuilder app)
|
|
|
|
|
{
|
|
|
|
|
var migrationService = app.ApplicationServices.GetService<IMigrationService>();
|
|
|
|
|
migrationService.Execute();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|