From 4f4556b62b3821d408e610b6faea15144b5eed45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Tue, 17 Oct 2023 12:13:00 +0500 Subject: [PATCH 1/4] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D1=82=D1=83=D1=81=D0=B0=20=D1=81=D0=BA=D0=B2=D0=B0=D0=B6=D0=B8?= =?UTF-8?q?=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudInfrastructure/Services/WellService.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 97bf9e06..1a907725 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -105,18 +105,17 @@ namespace AsbCloudInfrastructure.Services public async Task GetOrDefaultStatAsync(int idWell, CancellationToken token) { - var dto = wellInfoService.FirstOrDefault(well => well.Id == idWell); - if (dto is not null) - return dto; - - var request = new WellRequest{Ids = new[] { idWell }}; - var entities = await GetEntitiesAsync(request, token); - var entity = entities.FirstOrDefault(); + var entity = await GetOrDefaultAsync(idWell, token); + if (entity is null) return null; + + var dto = wellInfoService.FirstOrDefault(well => well.Id == idWell); - dto = entity.Adapt(); - + if (dto is null) + return entity.Adapt(); + + dto.IdState = entity.IdState; return dto; } From 1fd00256e94cd400cd33403315550377c57223ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Fri, 20 Oct 2023 15:46:44 +0500 Subject: [PATCH 2/4] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Возник баг если обновлять скважину из состояния в работе -> завершена и обратно возникает ошибка, что скважина отслеживается контекстом. 2. Поправил метод контроллера UpdateWellStateAsync --- AsbCloudInfrastructure/Services/WellService.cs | 3 ++- AsbCloudWebApi/Controllers/WellController.cs | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 1a907725..105cb5b3 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -33,7 +33,8 @@ namespace AsbCloudInfrastructure.Services .Include(w => w.Telemetry) .Include(w => w.WellType) .Include(w => w.RelationCompaniesWells) - .ThenInclude(r => r.Company); + .ThenInclude(r => r.Company) + .AsNoTracking(); public WellService(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService, ITimezoneService timezoneService, WellInfoService wellInfoService) : base(db, memoryCache, MakeQueryWell) diff --git a/AsbCloudWebApi/Controllers/WellController.cs b/AsbCloudWebApi/Controllers/WellController.cs index 3ed37d65..51e215ca 100644 --- a/AsbCloudWebApi/Controllers/WellController.cs +++ b/AsbCloudWebApi/Controllers/WellController.cs @@ -135,8 +135,8 @@ namespace AsbCloudWebApi.Controllers idWell, token).ConfigureAwait(false)) return Forbid(); - var dto = wellService.GetOrDefault(idWell)!; - dto.IdState = idState; + var dto = await wellService.GetOrDefaultAsync(idWell, token); + dto!.IdState = idState; var result = await wellService.UpdateAsync(dto, token) .ConfigureAwait(false); From e9b79bb0d6990ce90a5509e5665df153a4dfb05a Mon Sep 17 00:00:00 2001 From: Frolov-Nikita Date: Sun, 22 Oct 2023 17:57:39 +0500 Subject: [PATCH 3/4] Well service rename some variables --- AsbCloudInfrastructure/Services/WellService.cs | 14 +++++++------- AsbCloudWebApi/Controllers/WellController.cs | 7 ++++++- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 105cb5b3..6a805560 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -106,18 +106,18 @@ namespace AsbCloudInfrastructure.Services public async Task GetOrDefaultStatAsync(int idWell, CancellationToken token) { - var entity = await GetOrDefaultAsync(idWell, token); + var well = await GetOrDefaultAsync(idWell, token); - if (entity is null) + if (well is null) return null; - var dto = wellInfoService.FirstOrDefault(well => well.Id == idWell); + var wellInfo = wellInfoService.FirstOrDefault(well => well.Id == idWell); - if (dto is null) - return entity.Adapt(); + if (wellInfo is null) + return well.Adapt(); - dto.IdState = entity.IdState; - return dto; + wellInfo.IdState = well.IdState; + return wellInfo; } public async Task> GetAsync(WellRequest request, CancellationToken token) diff --git a/AsbCloudWebApi/Controllers/WellController.cs b/AsbCloudWebApi/Controllers/WellController.cs index 51e215ca..2a0ad437 100644 --- a/AsbCloudWebApi/Controllers/WellController.cs +++ b/AsbCloudWebApi/Controllers/WellController.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data; +using AsbCloudApp.Exceptions; using AsbCloudApp.Services; using AsbCloudDb.Model; using Microsoft.AspNetCore.Authorization; @@ -136,7 +137,11 @@ namespace AsbCloudWebApi.Controllers return Forbid(); var dto = await wellService.GetOrDefaultAsync(idWell, token); - dto!.IdState = idState; + + if (dto is null) + return this.ValidationBadRequest(nameof(idWell), $"Скважина с id: {idWell} недоступна"); + + dto.IdState = idState; var result = await wellService.UpdateAsync(dto, token) .ConfigureAwait(false); From d63e6a7ba6e6cdc5f754a222af71efc5cd97e6a1 Mon Sep 17 00:00:00 2001 From: Frolov-Nikita Date: Sun, 22 Oct 2023 19:37:23 +0500 Subject: [PATCH 4/4] Restore database initialization script --- AsbCloudDb/EFExtentionsInnitialization.cs | 50 +++++++++++++++++++++++ AsbCloudInfrastructure/Startup.cs | 6 ++- 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 AsbCloudDb/EFExtentionsInnitialization.cs diff --git a/AsbCloudDb/EFExtentionsInnitialization.cs b/AsbCloudDb/EFExtentionsInnitialization.cs new file mode 100644 index 00000000..110a75ae --- /dev/null +++ b/AsbCloudDb/EFExtentionsInnitialization.cs @@ -0,0 +1,50 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Linq; +using Microsoft.EntityFrameworkCore.Infrastructure; + +namespace AsbCloudDb +{ + public static class EFExtentionsInnitialization + { + public static void EnshureCreatedAndMigrated(this DatabaseFacade db) + { + db.SetCommandTimeout(TimeSpan.FromMinutes(5)); + if (db.EnsureCreated()) + { + db.CreateMigrationTable(); + db.WriteMigrationsInfo(); + } + else + { + db.SetCommandTimeout(TimeSpan.FromMinutes(20)); + db.Migrate(); + } + } + + private static void CreateMigrationTable(this DatabaseFacade db) + { + var sqlCreateMigrationTable = + $"CREATE TABLE public.\"__EFMigrationsHistory\" " + + $"(\"MigrationId\" varchar(150) NOT NULL, " + + $" \"ProductVersion\" varchar(32) NOT NULL, " + + $" CONSTRAINT \"PK___EFMigrationsHistory\" PRIMARY KEY (\"MigrationId\"));"; + db.ExecuteSqlRaw(sqlCreateMigrationTable); + } + + private static void WriteMigrationsInfo(this DatabaseFacade db) + { + var efVersion = db.GetType().Assembly.GetName().Version!; + var efVersionString = $"{efVersion.Major}.{efVersion.Minor}.{efVersion.Build}"; + + var migrations = db.GetPendingMigrations() + .Select(migration => $" ('{migration}', '{efVersionString}')"); + + var sqlAddLastMigration = + $"INSERT INTO public.\"__EFMigrationsHistory\" " + + $"(\"MigrationId\", \"ProductVersion\") " + + $"VALUES {string.Join(',', migrations)};"; + db.ExecuteSqlRaw(sqlAddLastMigration); + } + } +} diff --git a/AsbCloudInfrastructure/Startup.cs b/AsbCloudInfrastructure/Startup.cs index 9aff9ca9..240c8892 100644 --- a/AsbCloudInfrastructure/Startup.cs +++ b/AsbCloudInfrastructure/Startup.cs @@ -12,6 +12,9 @@ using AsbCloudInfrastructure.Background; using AsbCloudApp.Data.SAUB; using AsbCloudInfrastructure.Services.SAUB; using AsbCloudInfrastructure.Services.Subsystems; +using System.Linq; +using DocumentFormat.OpenXml.InkML; +using AsbCloudDb; namespace AsbCloudInfrastructure { @@ -23,8 +26,7 @@ namespace AsbCloudInfrastructure var provider = scope.ServiceProvider; var context = provider.GetRequiredService(); - context.Database.SetCommandTimeout(TimeSpan.FromMinutes(5)); - context.Database.Migrate(); + context.Database.EnshureCreatedAndMigrated(); // TODO: Сделать инициализацию кеша телеметрии более явной. _ = provider.GetRequiredService>();