From 8fcd90e409ab4a160245fc20dd25215c91763282 Mon Sep 17 00:00:00 2001 From: Vitalii Mikhailov Date: Sun, 25 Feb 2024 13:26:48 +0200 Subject: [PATCH] Removed SQLite --- .../Contexts/OldAppDbContext.cs | 76 ------------------- .../Controllers/CrashUploadController.cs | 14 +--- .../Controllers/ReportController.cs | 36 ++------- .../Models/Database/OldFileEntity.cs | 8 -- .../Models/Database/OldIdEntity.cs | 12 --- .../Models/Database/OldJsonEntity.cs | 8 -- .../Services/DatabaseMigrator.cs | 6 +- src/BUTR.CrashReportServer/Startup.cs | 3 +- 8 files changed, 16 insertions(+), 147 deletions(-) delete mode 100644 src/BUTR.CrashReportServer/Contexts/OldAppDbContext.cs delete mode 100644 src/BUTR.CrashReportServer/Models/Database/OldFileEntity.cs delete mode 100644 src/BUTR.CrashReportServer/Models/Database/OldIdEntity.cs delete mode 100644 src/BUTR.CrashReportServer/Models/Database/OldJsonEntity.cs diff --git a/src/BUTR.CrashReportServer/Contexts/OldAppDbContext.cs b/src/BUTR.CrashReportServer/Contexts/OldAppDbContext.cs deleted file mode 100644 index bf846a1..0000000 --- a/src/BUTR.CrashReportServer/Contexts/OldAppDbContext.cs +++ /dev/null @@ -1,76 +0,0 @@ -using BUTR.CrashReportServer.Contexts.Config; -using BUTR.CrashReportServer.Models.Database; - -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; - -namespace BUTR.CrashReportServer.Contexts; - -public class OldFileEntityConfiguration : BaseEntityConfiguration -{ - protected override void ConfigureModel(EntityTypeBuilder builder) - { - builder.Property(nameof(IdEntity.FileId)).HasColumnName("file_id"); - builder.Property(p => p.DataCompressed).HasColumnName("data_compressed"); - builder.ToTable("file_entity").HasKey(x => x.RowId); - builder.HasAlternateKey(nameof(IdEntity.FileId)).HasName("file_entity_pkey"); - - builder.HasOne(x => x.Id) - .WithOne() - .HasForeignKey(nameof(IdEntity.FileId)) - .HasPrincipalKey(x => x.FileId) - .OnDelete(DeleteBehavior.Cascade); - - builder.Navigation(x => x.Id).AutoInclude(); - } -} -public class OldIdEntityConfiguration : BaseEntityConfiguration -{ - protected override void ConfigureModel(EntityTypeBuilder builder) - { - builder.Property(x => x.FileId).HasColumnName("file_id"); - builder.Property(x => x.CrashReportId).HasColumnName("crash_report_id"); - builder.Property(x => x.Version).HasColumnName("version"); - builder.Property(x => x.Created).HasColumnName("created"); - builder.ToTable("id_entity").HasKey(x => x.RowId); - builder.HasAlternateKey(x => x.FileId); - - builder.HasIndex(x => x.CrashReportId).IsUnique(false); - } -} -public class OldJsonEntityConfiguration : BaseEntityConfiguration -{ - protected override void ConfigureModel(EntityTypeBuilder builder) - { - builder.Property(nameof(IdEntity.FileId)).HasColumnName("file_id"); - builder.Property(p => p.CrashReportCompressed).HasColumnName("data_compressed"); - builder.ToTable("json_entity").HasKey(x => x.RowId); - builder.HasAlternateKey(nameof(IdEntity.FileId)); - - builder.HasOne(x => x.Id) - .WithOne() - .HasForeignKey(nameof(IdEntity.FileId)) - .HasPrincipalKey(x => x.FileId) - .OnDelete(DeleteBehavior.Cascade); - - builder.Navigation(x => x.Id).AutoInclude(); - } -} - -public class OldAppDbContext : DbContext -{ - public DbSet IdEntities { get; set; } - public DbSet FileEntities { get; set; } - public DbSet JsonEntities { get; set; } - - public OldAppDbContext(DbContextOptions options) : base(options) { } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - - modelBuilder.ApplyConfiguration(new OldIdEntityConfiguration()); - modelBuilder.ApplyConfiguration(new OldFileEntityConfiguration()); - modelBuilder.ApplyConfiguration(new OldJsonEntityConfiguration()); - } -} \ No newline at end of file diff --git a/src/BUTR.CrashReportServer/Controllers/CrashUploadController.cs b/src/BUTR.CrashReportServer/Controllers/CrashUploadController.cs index c673207..8d24dba 100644 --- a/src/BUTR.CrashReportServer/Controllers/CrashUploadController.cs +++ b/src/BUTR.CrashReportServer/Controllers/CrashUploadController.cs @@ -35,7 +35,6 @@ public sealed record CrashReportUploadBody(CrashReportModel CrashReport, ICollec private readonly CrashUploadOptions _options; private readonly JsonSerializerOptions _jsonSerializerOptions; private readonly AppDbContext _dbContext; - private readonly OldAppDbContext _dbContextOld; private readonly GZipCompressor _gZipCompressor; private readonly HexGenerator _hexGenerator; @@ -48,7 +47,7 @@ public CrashUploadController( AppDbContext dbContext, GZipCompressor gZipCompressor, HexGenerator hexGenerator, - IMeterFactory meterFactory, OldAppDbContext dbContextOld) + IMeterFactory meterFactory) { var meter = meterFactory.Create("BUTR.CrashReportServer.Controllers.CrashUploadController", "1.0.0"); @@ -58,7 +57,6 @@ public CrashUploadController( _jsonSerializerOptions = jsonSerializerOptions.Value ?? throw new ArgumentNullException(nameof(jsonSerializerOptions)); _options = options.Value ?? throw new ArgumentNullException(nameof(options)); _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext)); - _dbContextOld = dbContextOld ?? throw new ArgumentNullException(nameof(dbContextOld)); _gZipCompressor = gZipCompressor ?? throw new ArgumentNullException(nameof(gZipCompressor)); _hexGenerator = hexGenerator ?? throw new ArgumentNullException(nameof(hexGenerator)); } @@ -71,13 +69,9 @@ private string GenerateFileId(CancellationToken ct) { var fileIds = _hexGenerator.GetHex(count, 3); var existing = _dbContext.IdEntities.Select(x => x.FileId).Where(x => fileIds.Contains(x)).ToHashSet(); - var existing2 = _dbContextOld.IdEntities.Select(x => x.FileId).Where(x => fileIds.Contains(x)).ToHashSet(); - //if (existing.Count == count) continue; - //fileId = existing.First(x => !fileIds.Contains(x)); - fileIds.ExceptWith(existing); - fileIds.ExceptWith(existing2); - if (fileIds.Count == 0) continue; - return fileIds.First(); + if (existing.Count == count) continue; + if (existing.Count == 0) return fileIds.First(); + return existing.First(x => !fileIds.Contains(x)); } return fileId; } diff --git a/src/BUTR.CrashReportServer/Controllers/ReportController.cs b/src/BUTR.CrashReportServer/Controllers/ReportController.cs index 5f4cbc2..56f0a4e 100644 --- a/src/BUTR.CrashReportServer/Controllers/ReportController.cs +++ b/src/BUTR.CrashReportServer/Controllers/ReportController.cs @@ -39,15 +39,13 @@ public sealed record GetNewCrashReportsBody private readonly ILogger _logger; private readonly ReportOptions _options; private readonly AppDbContext _dbContext; - private readonly OldAppDbContext _dbContextOld; private readonly GZipCompressor _gZipCompressor; - public ReportController(ILogger logger, IOptionsSnapshot options, AppDbContext dbContext, OldAppDbContext dbContextOld, GZipCompressor gZipCompressor) + public ReportController(ILogger logger, IOptionsSnapshot options, AppDbContext dbContext, GZipCompressor gZipCompressor) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _options = options.Value ?? throw new ArgumentNullException(nameof(options)); _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext)); - _dbContextOld = dbContextOld ?? throw new ArgumentNullException(nameof(dbContextOld)); _gZipCompressor = gZipCompressor ?? throw new ArgumentNullException(nameof(gZipCompressor)); } @@ -78,25 +76,15 @@ private async Task GetHtml(string filename, CancellationToken ct) if (ValidateRequest(ref filename) is { } errorResponse) return errorResponse; - var file = await _dbContext.FileEntities.FirstOrDefaultAsync(x => x.Id!.FileId == filename, ct); - var fileOld = await _dbContextOld.FileEntities.FirstOrDefaultAsync(x => x.Id.FileId == filename, ct); - if (file is null && fileOld is null) + if (await _dbContext.FileEntities.FirstOrDefaultAsync(x => x.Id!.FileId == filename, ct) is not { } file) return StatusCode(StatusCodes.Status404NotFound); if (Request.GetTypedHeaders().AcceptEncoding.Any(x => x.Value.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))) { Response.Headers.ContentEncoding = "gzip"; - if (file is not null) - return File(file.DataCompressed, "text/html; charset=utf-8", true); - if (fileOld is not null) - return File(fileOld.DataCompressed, "text/html; charset=utf-8", true); + return File(file.DataCompressed, "text/html; charset=utf-8", true); } - if (file is not null) - return File(await _gZipCompressor.DecompressAsync(file.DataCompressed, ct), "text/html; charset=utf-8", true); - if (fileOld is not null) - return File(await _gZipCompressor.DecompressAsync(fileOld.DataCompressed, ct), "text/html; charset=utf-8", true); - - return StatusCode(StatusCodes.Status500InternalServerError); + return File(await _gZipCompressor.DecompressAsync(file.DataCompressed, ct), "text/html; charset=utf-8", true); } private async Task GetJson(string filename, CancellationToken ct) @@ -104,26 +92,16 @@ private async Task GetJson(string filename, CancellationToken ct) if (ValidateRequest(ref filename) is { } errorResponse) return errorResponse; - var file = await _dbContext.JsonEntities.FirstOrDefaultAsync(x => x.Id!.FileId == filename, ct); - var fileOld = await _dbContextOld.JsonEntities.FirstOrDefaultAsync(x => x.Id.FileId == filename, ct); - if (file is null && fileOld is null) + if (await _dbContext.JsonEntities.FirstOrDefaultAsync(x => x.Id!.FileId == filename, ct) is not { } file) return StatusCode(StatusCodes.Status404NotFound); if (Request.GetTypedHeaders().AcceptEncoding.Any(x => x.Value.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))) { Response.Headers.ContentEncoding = "gzip"; - if (file is not null) - return File(await _gZipCompressor.CompressAsync(new MemoryStream(Encoding.UTF8.GetBytes(file.CrashReport)), ct), "application/json; charset=utf-8", true); - if (fileOld is not null) - return File(fileOld.CrashReportCompressed, "application/json; charset=utf-8", true); + return File(await _gZipCompressor.CompressAsync(new MemoryStream(Encoding.UTF8.GetBytes(file.CrashReport)), ct), "application/json; charset=utf-8", true); } - if (file is not null) - return File(new MemoryStream(Encoding.UTF8.GetBytes(file.CrashReport)), "application/json; charset=utf-8", true); - if (fileOld is not null) - return File(await _gZipCompressor.DecompressAsync(fileOld.CrashReportCompressed, ct), "application/json; charset=utf-8", true); - - return StatusCode(StatusCodes.Status500InternalServerError); + return File(new MemoryStream(Encoding.UTF8.GetBytes(file.CrashReport)), "application/json; charset=utf-8", true); } [AllowAnonymous] diff --git a/src/BUTR.CrashReportServer/Models/Database/OldFileEntity.cs b/src/BUTR.CrashReportServer/Models/Database/OldFileEntity.cs deleted file mode 100644 index 9650019..0000000 --- a/src/BUTR.CrashReportServer/Models/Database/OldFileEntity.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace BUTR.CrashReportServer.Models.Database; - -public sealed record OldFileEntity : IEntity -{ - public int RowId { get; set; } - public required OldIdEntity Id { get; set; } - public required byte[] DataCompressed { get; set; } -} \ No newline at end of file diff --git a/src/BUTR.CrashReportServer/Models/Database/OldIdEntity.cs b/src/BUTR.CrashReportServer/Models/Database/OldIdEntity.cs deleted file mode 100644 index 13b415d..0000000 --- a/src/BUTR.CrashReportServer/Models/Database/OldIdEntity.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace BUTR.CrashReportServer.Models.Database; - -public sealed record OldIdEntity : IEntity -{ - public int RowId { get; set; } - public required string FileId { get; set; } - public required Guid CrashReportId { get; set; } - public required byte Version { get; set; } - public required DateTime Created { get; set; } -} \ No newline at end of file diff --git a/src/BUTR.CrashReportServer/Models/Database/OldJsonEntity.cs b/src/BUTR.CrashReportServer/Models/Database/OldJsonEntity.cs deleted file mode 100644 index f6c09bf..0000000 --- a/src/BUTR.CrashReportServer/Models/Database/OldJsonEntity.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace BUTR.CrashReportServer.Models.Database; - -public sealed record OldJsonEntity : IEntity -{ - public int RowId { get; set; } - public required OldIdEntity Id { get; set; } - public required byte[] CrashReportCompressed { get; set; } -} \ No newline at end of file diff --git a/src/BUTR.CrashReportServer/Services/DatabaseMigrator.cs b/src/BUTR.CrashReportServer/Services/DatabaseMigrator.cs index c31bb30..c337527 100644 --- a/src/BUTR.CrashReportServer/Services/DatabaseMigrator.cs +++ b/src/BUTR.CrashReportServer/Services/DatabaseMigrator.cs @@ -1,4 +1,5 @@ -using BUTR.CrashReportServer.Contexts; +/* +using BUTR.CrashReportServer.Contexts; using BUTR.CrashReportServer.Models.Database; using Microsoft.EntityFrameworkCore; @@ -89,4 +90,5 @@ protected override async Task ExecuteAsync(CancellationToken ct) GC.Collect(); } } -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/src/BUTR.CrashReportServer/Startup.cs b/src/BUTR.CrashReportServer/Startup.cs index d41b4da..fd783fb 100644 --- a/src/BUTR.CrashReportServer/Startup.cs +++ b/src/BUTR.CrashReportServer/Startup.cs @@ -47,10 +47,9 @@ public void ConfigureServices(IServiceCollection services) services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services.AddHostedService(); + //services.AddHostedService(); services.AddDbContextFactory(x => x.UseNpgsql(_configuration.GetConnectionString("Main"))); - services.AddDbContextFactory(x => x.UseSqlite(_configuration.GetConnectionString("Old"))); services.AddSwaggerGen(opt => {