2022-02-16 19:09:06 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-01-12 01:01:36 +02:00
|
|
|
|
using Netmash.Infrastructure.DatabaseMigration.Constants;
|
|
|
|
|
using Netmash.Infrastructure.DatabaseMigration.Models;
|
|
|
|
|
using Netmash.Infrastructure.DatabaseMigration.Repositories;
|
|
|
|
|
using Netmash.Infrastructure.DatabaseMigration.Services.Abstractions;
|
2022-02-16 19:09:06 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2023-01-12 01:01:36 +02:00
|
|
|
|
namespace Netmash.Infrastructure.DatabaseMigration.Services
|
2022-02-16 19:09:06 +02:00
|
|
|
|
{
|
|
|
|
|
internal class MetadataLocationService : IMetadataLocationService
|
|
|
|
|
{
|
|
|
|
|
private readonly ServiceConfiguration _configuration;
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
private readonly ILogger<MetadataLocationService> _logger;
|
|
|
|
|
|
|
|
|
|
public MetadataLocationService(ServiceConfiguration configuration, IServiceProvider serviceProvider, ILogger<MetadataLocationService> logger)
|
|
|
|
|
{
|
|
|
|
|
_configuration=configuration;
|
|
|
|
|
_serviceProvider=serviceProvider;
|
|
|
|
|
_logger=logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Check()
|
|
|
|
|
{
|
|
|
|
|
switch (_configuration.MetadataLocation)
|
|
|
|
|
{
|
|
|
|
|
case MetadataLocation.XmlFile:
|
|
|
|
|
CheckWorkspace();
|
|
|
|
|
break;
|
|
|
|
|
case MetadataLocation.Database:
|
|
|
|
|
await CheckMigrationTables();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new NotImplementedException($"Metadata location {_configuration.MetadataLocation} is not implemented.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckWorkspace()
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(_configuration.Workspace))
|
|
|
|
|
throw new Exception($"Workspace path is empty! Check 'Workspace' parameter.");
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(_configuration.Workspace))
|
|
|
|
|
Directory.CreateDirectory(_configuration.Workspace);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task CheckMigrationTables()
|
|
|
|
|
{
|
2022-02-18 08:56:07 +02:00
|
|
|
|
if (_configuration.DatabaseType == DatabaseType.SQLite)
|
|
|
|
|
CheckWorkspace();
|
|
|
|
|
|
2022-02-16 19:09:06 +02:00
|
|
|
|
using (var scope = _serviceProvider.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
var _repository = scope.ServiceProvider.GetRequiredService<IMigrationRepository>();
|
|
|
|
|
var migrationTablesAreSet = await _repository.MigrationTablesAreSet(_configuration.DatabaseType);
|
|
|
|
|
if (migrationTablesAreSet)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
|
|
|
var allEmbeddedResources = assembly.GetManifestResourceNames();
|
|
|
|
|
|
|
|
|
|
var sqlResources = GetSqlResources();
|
|
|
|
|
foreach (var resource in sqlResources)
|
|
|
|
|
{
|
|
|
|
|
if (!allEmbeddedResources.Contains(resource))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning($"Manifest resource {resource} does not exists in assembly {assembly.FullName} and will be ignored.");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var resourceContent = GetManifestResourceContent(assembly, resource);
|
|
|
|
|
await _repository.ExecuteSqlRaw(resourceContent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string[] GetSqlResources()
|
|
|
|
|
{
|
|
|
|
|
var result = _configuration.DatabaseType switch
|
|
|
|
|
{
|
|
|
|
|
DatabaseType.SQLServer => ManifestResources.SqlServer.Select(resource => $"{ManifestResourcesPath.SqlServer}{resource}"),
|
|
|
|
|
DatabaseType.SQLite => ManifestResources.Sqlite.Select(resource => $"{ManifestResourcesPath.Sqlite}{resource}"),
|
|
|
|
|
_ => throw new NotImplementedException($"DatabaseMigration type {_configuration.DatabaseType} is not implemented"),
|
|
|
|
|
};
|
|
|
|
|
return result.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetManifestResourceContent(Assembly assembly, string resourceName)
|
|
|
|
|
{
|
|
|
|
|
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
|
|
|
|
|
using (StreamReader reader = new StreamReader(stream))
|
|
|
|
|
{
|
|
|
|
|
string result = reader.ReadToEnd();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|