forked from ddrilling/AsbCloudServer
Merge branch 'dev' into feature/22340535-export-drill-test
This commit is contained in:
commit
2d4fe109d6
50
AsbCloudDb/EFExtentionsInnitialization.cs
Normal file
50
AsbCloudDb/EFExtentionsInnitialization.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
@ -105,19 +106,18 @@ namespace AsbCloudInfrastructure.Services
|
||||
|
||||
public async Task<WellMapInfoWithTelemetryStat?> GetOrDefaultStatAsync(int idWell, CancellationToken token)
|
||||
{
|
||||
var dto = wellInfoService.FirstOrDefault(well => well.Id == idWell);
|
||||
if (dto is not null)
|
||||
return dto;
|
||||
var well = await GetOrDefaultAsync(idWell, token);
|
||||
|
||||
var request = new WellRequest{Ids = new[] { idWell }};
|
||||
var entities = await GetEntitiesAsync(request, token);
|
||||
var entity = entities.FirstOrDefault();
|
||||
if (entity is null)
|
||||
if (well is null)
|
||||
return null;
|
||||
|
||||
dto = entity.Adapt<WellMapInfoWithTelemetryStat>();
|
||||
var wellInfo = wellInfoService.FirstOrDefault(well => well.Id == idWell);
|
||||
|
||||
return dto;
|
||||
if (wellInfo is null)
|
||||
return well.Adapt<WellMapInfoWithTelemetryStat>();
|
||||
|
||||
wellInfo.IdState = well.IdState;
|
||||
return wellInfo;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<WellDto>> GetAsync(WellRequest request, CancellationToken token)
|
||||
|
@ -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<IAsbCloudDbContext>();
|
||||
context.Database.SetCommandTimeout(TimeSpan.FromMinutes(5));
|
||||
context.Database.Migrate();
|
||||
context.Database.EnshureCreatedAndMigrated();
|
||||
|
||||
// TODO: Сделать инициализацию кеша телеметрии более явной.
|
||||
_ = provider.GetRequiredService<TelemetryDataCache<TelemetryDataSaubDto>>();
|
||||
|
@ -1,4 +1,5 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Exceptions;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
@ -135,7 +136,11 @@ namespace AsbCloudWebApi.Controllers
|
||||
idWell, token).ConfigureAwait(false))
|
||||
return Forbid();
|
||||
|
||||
var dto = wellService.GetOrDefault(idWell)!;
|
||||
var dto = await wellService.GetOrDefaultAsync(idWell, token);
|
||||
|
||||
if (dto is null)
|
||||
return this.ValidationBadRequest(nameof(idWell), $"Скважина с id: {idWell} недоступна");
|
||||
|
||||
dto.IdState = idState;
|
||||
|
||||
var result = await wellService.UpdateAsync(dto, token)
|
||||
|
Loading…
Reference in New Issue
Block a user