| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using AutoMapper.Configuration; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | using Microsoft.Extensions.Hosting; |
| | | 9 | | using Microsoft.Extensions.Logging; |
| | | 10 | | using Microsoft.Extensions.Options; |
| | | 11 | | using SVETA.Api.Data.Domain; |
| | | 12 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 13 | | |
| | | 14 | | namespace SVETA.Api.Helpers |
| | | 15 | | { |
| | | 16 | | public static class MigrationManager |
| | | 17 | | { |
| | | 18 | | public static IHost MigrationDatabase(this IHost host) |
| | 0 | 19 | | { |
| | | 20 | | |
| | 0 | 21 | | using (var scope = host.Services.CreateScope()) |
| | 0 | 22 | | { |
| | 0 | 23 | | var env = scope.ServiceProvider.GetRequiredService<IHostEnvironment>(); |
| | 0 | 24 | | var migrateConf = scope.ServiceProvider.GetService<IOptions<MigrateSettings>>(); |
| | 0 | 25 | | string migrateEnv = migrateConf.Value.MigrateEnv; |
| | 0 | 26 | | if (migrateEnv != null && migrateEnv.Split(",").Contains(env.EnvironmentName)) |
| | 0 | 27 | | { |
| | 0 | 28 | | using (var context = scope.ServiceProvider.GetRequiredService<SvetaDbContext>()) |
| | 0 | 29 | | { |
| | 0 | 30 | | var logger = scope.ServiceProvider.GetRequiredService<ILogger<IHost>>(); |
| | | 31 | | try |
| | 0 | 32 | | { |
| | 0 | 33 | | context.Database.Migrate(); |
| | 0 | 34 | | } |
| | 0 | 35 | | catch (Exception e) |
| | 0 | 36 | | { |
| | 0 | 37 | | logger.LogError(e, e.Message); |
| | 0 | 38 | | throw; |
| | | 39 | | } |
| | 0 | 40 | | } |
| | 0 | 41 | | } |
| | 0 | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | return host; |
| | 0 | 45 | | } |
| | | 46 | | } |
| | | 47 | | } |